Now a user can log in we can use this to restrict access to a page.
Here in the index page we're detecting if a user is logged in by checking the is logged in value exists
in the session and if it's set.
True.
Let's extract this line out into a function so we can use it.
First let's create a new file in the includes folder called Path dot p s p for our fanti case and related
functions in here we'll create a new function kalid is logged in and in here will add the code we just
saw from the index script returning the boolean value that results from this expression and will add
a p p doc comment at the top like that.
Then in our new article script first we require this new file and as we are using the session First
we need to call session start.
Then we can call the is logged in method which returns true if a user is logged in we want to take action.
If they use isn't logged in so we'll use the negation operator
which negates the result returned from this function.
This means the f block will be executed if this function returns false.
In this block lets just print out a message and stop the script for now.
Let's try that out.
We're not currently logged in and if I click on the new article link were shown the message and we can't
view the form to add a new article.
If I go back and log in
then I click the new article link we can access it.
Now we've extracted this code here in the index page out into a function.
Let's use it.
First we need to require the file where the function is.
Then we can replace this line with a call to the new function like that.
And in the browser.
This still works as before.
One more thing we can improve is to not show this link to users.
If they're not logged in as all that's going to happen when they click it is they're going to get an
unauthorized message.
So let's move this link to inside the f block here.
And while we're at it wrap it inside a paragraph element too.
So now when we're not logged in the link to the new article page isn't shown.
If I log in.
There's the link which we can click on and add a new article if we want.
When I log out the link is no longer displayed.
So by adding this code to a page access can be restricted to logged in users only before we develop
this further and restrict access to other pages.
First let's learn about classes and objects.