PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

HOW TO USE SESSIONS IN PHP

Using sessions in PHP allows you to store temporary information of a user on the server which can be passed from one page to another whilst the user is surfing your site. It is automatically destroyed once the user has left your site.

In other words, once a session is declared and a session variable is created by assigning a value, that value is remembered throughout any pages of your site that user may browse to. If the user leaves your site completely, these session variables are deleted.

To use sessions, you need to first start a session as follows:

<?
session_start();
?>

Note that session start must be the first thing on your page and come before anything else.

Then you can create a session variable by giving it a name and assigning a value to it as follows:

$_SESSION['car']="Bmw";

The $_SESSION['car'] now has a value of Bmw stored in it. If you echo $_SESSION['car'], Bmw will be outputted. Now, say this code was in your homepage that a user visits, then if a user goes to another page in your site (in the same visit) that has session_start() at the beginning of the page, the $_SESSION['car'] variable and it's value is retained so that if you echo $_SESSION['car'], you will get the value of Bmw outputted.

All subsequent pages on your site that you want to retain session variables and values must have session_start() at the very beginning of the page else the session won't be carried over to that page.

Complete code:

<?
session_start();
$_SESSION['car']="Bmw";
?>

If you want to delete some session variables, then you can do this by session name:

unset($_SESSION['car']);

You can also unset all session variables at one go by destroying the entire session as follows:

session_destroy();

Home | Privacy Policy | Terms Of Use | Contact Us