Depiction- Webmaster Resources
Stock Image


Contributors: Advertise Here  |  Advertise Here  |  Advertise Here


Misc Links: Submit Tutorials (Earn $$) l Advertising l Privacy Policy | Site Map

Tutorials > PHP > PHP Cookie and Session Variables


 
 

An introduction to PHP cookies and session variables. PHP server variables are mainly using for sending the data from one page to another. PHP is one of the best languages for sending data from one page to another.

By Blacksoft


Cookie Variable

Definition: A cookie variable is a variable that stores in the users computer. We can put a cookie variable to the web browser of the visitor any time using a php code. That code is given below.

Code:
------------------------------------------------------------------------
<?php
setcookie("name","data want to store as a cookie",time()+3600);
?>
------------------------------------------------------------------------

The above given code will store the data to the svisitors web browser in the name "name" . Then we want to know how can we retrive the data stored in a visitors browser. The code should looks like below.

Code:
------------------------------------------------------------------------
<?php
echo $_COOKIE['name'];
?>
------------------------------------------------------------------------

Session Variables

Session variables allow you to store "global" data for use by all your pages and are specific or unique to each visitor to your site.

First we can see how a session variable is used. Session variable is not storing to the users computers it will be stored in the server and it will be removed after the visitors session.

Code:
------------------------------------------------------------------------
<?php
session_start();
$_SESSION['name']='data you want to store';
?>
------------------------------------------------------------------------

The session variable should be used before the <html> tag in a webpage for best results. Session variables can be retrived using the the following code:

Code:
------------------------------------------------------------------------
<?php
echo $_SESSION['name'] ;
?>
------------------------------------------------------------------------

You can destroy a session at any time using the code below to delete the data from the session variable.

Code:
------------------------------------------------------------------------
<?php
unset($_SESSION['name']);
?>
------------------------------------------------------------------------

This will delete the specified session only. but we can delete all the session variables by using the codes given below.

Code:
------------------------------------------------------------------------
<?php
session_destroy();
?>
------------------------------------------------------------------------

 

About the Author
Blacksoft is the owner of Pixelspot Free Web Hosting.


 

Affiliates

Heathrowe Photoshop Tutorials
Tutorial Man Wallpaper Stock  More

Resources