Back to Top
The nuts and bolts for
Creative web development

SESSION variables cheat-sheet (PHP)

Need to know, or get a handle on SESSION variables in your dynamic sites for your PHP?

This is a refrence "cheat sheet," not a tutorial , so I provide tutorial links at the end.

SESSION variables, no matter what Server Model language you use, must first be declared (login) and destroyed at the end (logout). Assuming you have PHP 4.1 or higher. 

 

Initialize Session

This must be at the top of every page that will call/use a Session variable.

<?php session_start(); ?>

Register (declare) a Session variable

To add a Session variable that will be carried till the browser is closed or you kill it yourself.

<?php session_register("mySessionVariable"); ?>

Assigning value to a Session variable

Values added to a Session variable can be letters (ABCabc), numbers (1, 200, 999.99, etc.) making them good for usernames/userID's when logged into a site.

<?php $_SESSION["mySessionVariable"] = "MEMBER"; ?>

Unregister a Session variable

Logging out requires we unregister any and all Session variables used for our application and the users' account.

<?php session_unregister("mySessionVariable"); ?>

Destroy a Session

We should always destroy a Session to help complete a logout.

<?php session_destroy(); ?>


DMXzone Tutorials & Articles to learn more

PHP Documentation

Chris Charlton

Chris CharltonChris, Los Angeles' CSS & ActionScript guru, successfully cannonballed into web development in the late 90's. Always caught up with the latest in Flash, Dreamweaver, Fireworks, and XML, Chris authored premium articles for the largest Dreamweaver/Flash community (www.DMXzone.com) and produced WebDevDesign (iTunes featured), a popular Web Design & Development Podcast. Somewhere, Chris finds time to run an authorized Adobe user group focused around open source and Adobe technologies. Being a big community leader, Chris Charlton remains a resident faculty member of the Rich Media Insitute and lends himself to speak at large industry events, like JobStock, NAB, and FITC Hollywood.

Brain cycles from Chris are always Web Standards, Flash Platform, and accessibility.

See All Postings From Chris Charlton >>

Share this Article

Comments

help

why am i not able to use the session_start at the top of my page?? it jus gives me error "Warning: session_start(): open(c:/tmp\sess_3d51bee27893ca8b7a1ab84e4d55fa44, O_RDWR) failed: No such file or directory (2) in c:\program files\apache group\apache\htdocs\vhoreg\vho\vhohome.php on line 1

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at c:\program files\apache group\apache\htdocs\vhoreg\vho\vhohome.php:1) in c:\program files\apache group\apache\htdocs\vhoreg\vho\vhohome.php on line 1

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at c:\program files\apache group\apache\htdocs\vhoreg\vho\vhohome.php:1) in c:\program files\apache group\apache\htdocs\vhoreg\vho\vhohome.php on line 1
" wad does it mean????

Comments please

Wow, such a low score for a fast cheat-sheet... Comments please.