Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

Introduction to Sessions & Cookies


Sessions and cookies. What are they? Smoking marijuana and eating biscuits? Hell no - in the web development world they are very important functions of interactive design! The difference between sessions and cookies, though, basically revolves around where the variables are stored.  Here's a basic explanation:

Simple Sessions Management
Let’s start with sessions. In short they can be defined as pieces of data recorded by a web script and stored on the server.  They can be programmed to last for a certain amount of time, but are usually erased when the user shuts their browser, although it is required that the function session_start() be called before ANY output is sent to the browser. To be on the safe side, I usually put this function declaration on the first line of code in a web script. The easiest way to create and manage sessions in PHP (4+) is as follows:

<?
session_start();
$_SESSION[‘session_name’] = ‘your data’;
?>

You can use this same variable to compare with other strings in IF statements, etc. If for any reason you wish to erase a session before the user shuts their browser, you use session_destroy():

<?
session_destroy(‘session_name’);
?>

Let’s write a simple sessions script with two pages.  Put the following code on page 1:

<?
session_start();
echo ‘Welcome to our sessions test! Your example username can be pogo!’;
$_SESSION[‘username’] = ‘pogo’;
echo ‘<a href=”page2.php”>Click here to have a session</a>’;
?>

And this code on page 2:

<?
session_start();
echo ‘If all goes well, your username should show as “pogo”.’;
$username = $_SESSION[‘username’];
echo ‘Username: ‘.$username;
echo ‘Welcome pogo!’;

?>

Of course this script has no user entered data, but that is only one modification away (i.e. by adding an HTML form). I think this example is sufficient enough to demonstrate how sessions work - Yes, its so simple even a monkey could do it, and people say sessions are hard!

Simple Cookie Usage
Now on to cookies (YUM!). Cookies are the basically the same as sessions, except that they are stored locally (on your PC). If you are using M$ Windows then the chances are that they are stored in [windows default folder]\Cookies, although if you have user profiles enabled they will be located in your personal profile’s cookie folder.  They are used the same way as the sessions are above, with SESSION replaced with COOKIE:

<?
$_COOKIE[‘cookie_name’] = ‘your data’;
$name = $_COOKIE[‘cookie_name’];
echo $name;
// this outputs “your data” on your page
?>

You can use the same example script as provided for the sessions tutorial above, replacing “SESSION” with “COOKIE”.

There are more complicated ways of registering cookies which I wont go into because this is a basic tutorial - They have extra attributes that allow you to set the timeout limit, domain, cookie name, data, etc.

I hope you found this tutorial useful!  For more information on sessions go here, and for greater detail on cookies, look over here.   Good luck!

- Tutorial written by Scrowler

Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments

User:  Tamlin (#52201)
Date: Sun Aug 17, 2008. 08:20:16

Post #2 of 2

I don't really know about PHP, but from what I've read this error generally occurs due to the wrong type of quote marks being used. I notice the code contains so-called "smart" quotes, so try replacing [SIZE="4"]‘[/SIZE] and [SIZE="4"]’[/SIZE] with [SIZE="4"]' [/SIZE] (and [SIZE="4"]”[/SIZE] with [SIZE="4"]"[/SIZE]).
It might not work but it's worth a try.

Reply to this post


User:  hunterxaz (#52197)
Date: Sun Aug 17, 2008. 02:52:32

Post #1 of 2

Hello, this tutorial seems to be broken.

I get:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/backline/public_html/hunter/php/tutorials/sessions/index.php on line 3


The line specifically:

echo ‘Welcome to our sessions test! Your example username can be pogo!’;


Entire code:

session_start();
echo ‘Welcome to our sessions test! Your example username can be pogo!’;
$_SESSION [‘username’] = ‘pogo’;
echo ‘Click here to have a session’;
?>

Reply to this post


--- View Entire Thread ---
Amazing Font Pack!

Featured Tutorialsmore

Signature Techniques
Signature Techniques
- Adobe Photoshop -
Abstract Grunge In...
Abstract Grunge In...
- Adobe Photoshop -
Anti-Aliasing Inte...
Anti-Aliasing Inte...
- Adobe Photoshop -
The Glamour Effect
The Glamour Effect
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1052 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2356 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2499 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673