If the visitor navigates to an another page the value of $name will be lost, so we would need to ask for the name on each page so in this case is way better to use sessions.
In order to use a session first we need to "start" it:
A session variable look`s and works like an element of an associative array:
And because sessions are keeping their value even if you go to an another page:
page1.php
Let`s say that we have a page that is displayed only to members. If somebody is logged in than it`s username is stored in a session. In order to show the content of the page to the visitor we need to check if the username session exists or not. This can be easily done using this function:
When the user leaves your website or closes the browser the sessions are automatically deleted, but if you want to delete a session yourself use the following code:
If you want to delete all the sessions use the following code:
Example of using the session_destroy(); function:
log-out.php
This is enough about sessions, but if you any question related to this session please ask!
In order to use a session first we need to "start" it:
<?php
session_start();
?>
NOTE: The session_start(); function must be the at the very beginning of your code, even before the <html> tag.session_start();
?>
A session variable look`s and works like an element of an associative array:
$_SESSION['name'] = 'Tom';
echo $_SESSION['name'];
echo $_SESSION['name'];
And because sessions are keeping their value even if you go to an another page:
page1.php
<?php
session_start();
$_SESSION['name'] = 'Tom';
?>
page2.phpsession_start();
$_SESSION['name'] = 'Tom';
?>
<?php
session_start();
echo $_SESSION['name'] ;
?>
Will outputsession_start();
echo $_SESSION['name'] ;
?>
Tom
Let`s say that we have a page that is displayed only to members. If somebody is logged in than it`s username is stored in a session. In order to show the content of the page to the visitor we need to check if the username session exists or not. This can be easily done using this function:
isset($_SESSION['username']); //Returns True if exists and False if not
So if we use the isset function our code will look like this:<?php
session_start();
if(isset($_SESSION['username'])){
echo 'Welcome ' . $_SESSION['username'];
}
else{
echo 'You are not logged in!';
}
?>
session_start();
if(isset($_SESSION['username'])){
echo 'Welcome ' . $_SESSION['username'];
}
else{
echo 'You are not logged in!';
}
?>
When the user leaves your website or closes the browser the sessions are automatically deleted, but if you want to delete a session yourself use the following code:
unset($_SESSION['username']);
This is useful when you want to delete only a single session.If you want to delete all the sessions use the following code:
session_destroy();
NOTE: After this function you can`t use more sessions on the page.Example of using the session_destroy(); function:
log-out.php
session_start();
session_destroy();
By destroying the sessions the user is logged out.session_destroy();
This is enough about sessions, but if you any question related to this session please ask!
HTML Code
Direct Link
6 comments:
Very useful.Thanks
There's one thing i dont really get,
Should'nt this "session" codes be associated with a list of users stored in a database?
I think i understand the logic in this explanation, but i dont really understand how to make use of the session...
Or should i just mix this with the "authentication" tutorial, including this "session" scripts when ever the authentication process is completed?
thanks a lot in advance!!!
Jose.
Nice tutorial for the beginners..gud keep it up.
Sessions can be used in many ways, for example:
Have you ever seen a website where you can select the font size or the text color ? Well, in these websites you don`t need to select again the text size if you navigate to another page because your selection is stored in a session (or for sure it could be a cookie)
or
the shopping carts are usually are working with sessions to store the selected products
used 1 .. bt have to add some more
thanks
Post a Comment