Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Client Clones and Server Sessions

By Tony Marston
2005-05-06


Starting a new session

As I stated earlier it is impossible for the server to know that a new browser window has been created on the client device, so it is up to the user to instruct the application to start a new session. To do this each page must include a hyperlink similar to the following:

<a href="HTTP://whatever/script.php?action=newsession&session_name=menu0">new session</a>
When this is pressed it will execute the following application code on the server:

  if (isset($_GET['action'])) {

if ($_GET['action'] == 'newsession') {
// get and register a new session name
$session_name = getNewSession('menu');
session_name($session_name);
// generate a new id to go with this name
session_regenerate_id();
// save this session data NOW!
session_write_close();
// now restart the current script
$location = 'http://' .$_SERVER['HTTP_HOST']
.$_SERVER['PHP_SELF']
.'?session_name=' .session_name();
header('Location: ' .$location);
exit;
} // if
} // if
This is executed after the previous code, so the $_SESSION array has already been populated with the existing session data. The session_name() and session_regenerate_id() functions have the effect of telling PHP at the server end to store this session data with a new session id, and the browser at the client end to refer to the session with a different name.

As a result of this operation the user should see the current page being refreshed, but with a different session name in the URL.

Although the new session starts with the same session data as the session from which it was cloned, from this point on the activities in each of the browser windows will be kept separate.

If you examine the cookie data after this operation you should see something like the following:

menu0 = a3a73430fe22e103638bf916b7fffe3e
menu1 = 726ffac3032f201350331bb7955d1848


Note that this procedure will allow up to 100 different sessions in different browser windows to exist on any one client at any one time. That should be more than enough for anybody.

Tutorial Pages:
» Introduction
» Session Identities
» Session Names and Session Cookies
» Dynamically changing the Session Name
» Starting a new session


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1