Worth2Read
Home

Forum

PHP Tutorials
Blogs
Children
Article Directory

Contact Us

Disclaimer
Reliable $1 Web Hosting by 3iX
 

If you have any doubts with your PHP code please discuss it in the PHP Forum

Sessions in PHP

Sessions allow us to store information in the server for future use. A session starts once the visitor enters the site and ends once he leaves the site. If we want to store the data permanently we have to store in database.

For the websites, which ask for users to register and login, session variables are necessary to keep the information about the visitors.

For example after login, the visitor may be addressed like "Hi visitorname" in all the pages he visits in that session...

To use sessions in php we have to start the session in all pages in which we read/write session variables.

Syntax :

<?php session_start(); ?>

This session_start(); statement should be put before the <html> tag.

Writing A Session variable :

Example : <?php $_SESSION['userid'] = 'Worth2Read'; ?>

This creates a session variable userid.

Reading a session variable :

We can read the session variable in other pages. For example,

<?php echo "Hi ".$_SESSION['userid']; ?> will display

Hi Worth2Read

Deleting the Session variable :

When a user decides to Logout or Signout , the session variables associated with him need to be deleted.

<?php unset($_SESSION['userid']); ?> will delete the userid. The entire session can be deleted by using the

function

session_destroy();

Copyright © 2008 www.worth2read.org. All rights Reserved. Use of this Site is subject to our Privacy Statement

Untitled Document
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
Programming PHP
Beginning PHP and MySQL: From Novice to Professional, Third Edition (Beginning from Novice to Professional)
Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
PHP and MySQL Web Development (3rd Edition) (Developer's Library)
PHP & MySQL For Dummies 3rd edition (For Dummies (Computer/Tech))
PHP Solutions: Dynamic Web Design Made Easy (Solutions)
Build Your Own Website The Right Way Using HTML & CSS
Learning Web Design: A Beginner's Guide to (X)HTML, StyleSheets, and Web Graphics
Sams Teach Yourself HTML and CSS in 24 Hours (7th Edition) (Sams Teach Yourself)