Baljeet
Member
- Jan 31, 2011
- 76
- 0
Sessions can transfer data between two or more pages. It`s somehow like a variable that keep`s it`s value even if you call it from an another file. Let`s say that we have a website and in the index.php file we are asking for the name of the visitor and we store it in the $name variable (e.g.:$name = 'Tom').
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:
NOTE: The session_start(); function must be the at the very beginning of your code, even before the <html> tag.
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
page2.php
Will output
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
So if we use the isset function our code will look like this:
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:
This is useful when you want to delete only a single session.
If you want to delete all the sessions use the following code:
NOTE: After this function you can`t use more sessions on the page.
Example of using the session_destroy(); function:
log-out.php
By destroying the sessions the user is logged out.
Here is the original tutorial:
This is what you need to know about sessions... If you have any questions please ask me! :w00t:
All Credits goes to one who really made this...
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:
PHP:
<?php
session_start();
?>
A session variable look`s and works like an element of an associative array:
PHP:
$_SESSION['name'] = 'Tom';
echo $_SESSION['name'];
page1.php
PHP:
<?php
session_start();
$_SESSION['name'] = 'Tom';
?>
PHP:
<?php
session_start();
echo $_SESSION['name'] ;
?>
PHP:
Tom
You must be registered for see links
. 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:
PHP:
isset($_SESSION['username']); //Returns True if exists and False if not
PHP:
<?php
session_start();
if(isset($_SESSION['username'])){
echo 'Welcome ' . $_SESSION['username'];
}
else{
echo 'You are not logged in!';
}
?>
PHP:
unset($_SESSION['username']);
If you want to delete all the sessions use the following code:
PHP:
session_destroy();
Example of using the session_destroy(); function:
log-out.php
PHP:
session_start();
session_destroy();
Here is the original tutorial:
You must be registered for see links
This is what you need to know about sessions... If you have any questions please ask me! :w00t:
All Credits goes to one who really made this...