Writers' Community!
Home Page Two Columnists Q&A Submit an Article FAQs Contact Author Login
Article Submission
We Need YOUR Articles!
We'll Promote Them for FREE!

Author Login

New Authors
Register Here


Now Serving 7,768 Authors
70,440 Quality Articles
& 6,329 Current Users Online!
Featured Authors
Connor Davidson (5,131)
Joel Hendon (16,285)
Ben Morrish (7,936)
Steve Kovacs (4,545)
Sandra E. Graham (7,883)
Fran Larson (2,271)
Shari Vaudo (418)
David Tanguay (9,577)
Michael Ramzy (633)
Missing Link (766)
E. Raymond Rock (3,068)
Gregory Lewis (1,603)
Nancy Daniels (1,550)
Mark Parsec (15,056)

View All Featured Authors
Most Recent
Creating the Perfect Website Design

How To Create An HTML Image Background For Your Webpage

Analyze Keywords Using Adwords Keyword Tool and Excel Macro Function

If You're Making These Web Site Mistakes, You're Scaring Prospects Away

Perl of Wisdom, Perl of laughter

PHP’s Paamayim Nekudotayim: The secret’s in the code.

The Macromedia Flash man looks to LEGO for a System.

Your Website Concept

What Makes A Good Website?

How To Build a Basic Web Page

Home » Categories » Website Technologies » Website Design » PHP Session Handling » Printer Friendly

PHP Session Handling

Rated 4 out of 5
No Reader Ratings Available ?
Rate It  /  View Comments  /  View All Articles submitted by SEOXpert
Submitted Wednesday, December 15, 2004
SEOXpert (164)
SEO
Log in to become a member of SEOXpert's Fan Club!


Installment 3 - PHP Session Handling - Cookies Enabled

Instead of storing session information at the browser through the use of cookies, the information can instead be stored at the server in session files. One session file is created and maintained for each user session. For example, if there are three concurrent users browsing the website, three session files will be created and maintained - one for each user. The session files are deleted if the session is explicitly closed by the PHP script or by a daemon garbage collection process provided by PHP. Good programming practice would call for sessions to be closed explicitly in the script.

The following is a typical server-browser sequence of events that occur when a PHP session handling is used:
1. The server knows that it needs to remember the State of browsing session
2. PHP generates a sssion ID and creates a session file to store future information as required by subsequent pages
3. A cookie is generated wih the session ID at the browser
4. This cookie that stores the session ID is transparently and automatically sent to the server for all subsequent requests to the server

The following PHP session-handling example accomplishes the same outcome as the previous cookie example. Copy the code below (both the php and the html) into a file with the .php extension and test it out.

Code:
The current session id is: This page has been displayed: times.


A summary of the functions that PHP provides for session handling are:
1. boolean start_session() - initializes a session
2. string session_id([string id]) - either returns the current session id or specify the session id to be used when the session is created
3. boolean session_register(mixed name [, mixed ...]) - registers variables to be stored in the session file. Each parameter passed in the function is a separate variable
4. boolean session_is_registered(string variable_name) - checks if a variable has been previously registered to be stored in the session file
5. session_unregister(string varriable_name) - unregisters a variable from the session file. Unregistered variables are no longer valid for reference in the session.
6. session_unset() - unsets all session variables. It is important to note that all the variables remain registered.
7. boolean session_destroy() - destroys the session. This is opposite of the start_session function.

The next installment discusses how to manage sessions using PHP session handling functions when cookies are disabled...
 

Installment 4 - PHP Session Handling - Without Cookies

If cookies are disabled at the browser, the above example cannot work. This is because although the session file that stores all the variables is kept at the server, a cookie is still needed at the browser to store the session ID that is used to identify the session and its associated session file. The most common way around this would be to explicitly pass the session ID back to the server from the browser as a query parameter in the URL.

For example, the PHP script generates requests subsequent to the start_session call in the following format:

Code:
http://www.yourhost.com/yourphpfile.php?PHPSESSID=


The following are excerpts that illustrate the discussion:

Manually building the URL:
Code:
$url = "http://www.yoursite.com/yourphppage.php?PHPSESSID=" . session_id()

Building the URL using SID:
Code:


End of series. There are 4 installments in this series

Resource From Free Webmaster Resources






Reprint Rights

Log in to become a member of SEOXpert's Fan Club!

Comments on this article: (1 total)


» left by Gregory Lewis (302)
Gregory Lewis
(1 day 5 hours ago.)

Reader Rating: 4 out of 5
Wow, another PHP coder! Your explanation of how to use the session id is spot on, and right down to the use of passing the session in the h t t p string ($url = $someUrl . "?sid=" . $_SESSION['id'] ).
 
This is a very useful instruction. Still, I use cookies because they are so easy to track across multiple sessions, and give a sense of identity to the user. Did this guy log in as a user, a power user, an admin, etc.
 
Nice work,
 
- G

Respond to this comment

Was this article helpful to you? Leave a Public Comment or Question:

This Article has been viewed 2,121 times.
Article added to SearchWarp.com on 12/15/2004 12:05:25 PM.
View other articles written by SEOXpert (164)


If you found this article interesting, you may want to check out:

Disclaimer:  All information on this site is provided for informational purposes only! By no means is any information presented herein intended to substitute for the advice provided to you by any health care or other professional or organization.


Today's Most Popular
How to Create a Splash Page for Your Website?

Choosing An Effective Website Colour Combination

How To Create An HTML Image Background For Your Webpage

Creating Dynamic Website Content with PHP - MySQL

Build A Successful Website For Dummies

Mastering Photoshop Shapes In 3 Easy Steps

Create a Professional Website Using Ready-Made Templates

10 Web Design Tips For A Professional Looking Niche Web Site

Analyze Keywords Using Adwords Keyword Tool and Excel Macro Function

Photoshop’s Pen Tool for Tracing Vectors

Viewed from Cache. Load Time: 0.031.

Home  |  Page Two  |  FAQ's  |  Contact  |  Terms of Service  |  Article Submission Guidelines  |  Questions & Answers  |  Privacy  |  Mission / About
Copyright © 1999-2009 SearchWarp.com, All Rights Reserved - SearchWarp.com is an IcoLogic, Inc. Company