views:

70

answers:

3

I am creating a session and I want a variable to be available for the entire site, so for example:

Joe Soap lands on a page called red-widgets.php, - Joe's session variable is let's say 'red-widgets'. Joe then clicks on another page called blue-widgets.php. I would like the session variable to be still the original 'red-widgets'.

How can this be done with the url?

+4  A: 

You are not limited to one session variable, you can have as many as you need.

session_start();

if ( !isset($_SESSION['widget']) ) {
  $_SESSION['widget'] = 'i am changed only if there isnt already a widget session var';
}

$_SESSION['someOtherVar'] = 'i am being changed on every page';
code_burgar
+1: I was just writing the same...
jeroen
Sorry guys, complete rookie question. Just tested there and it works as planned. So basically session variables are global?, but where do they 'stay'?
Keith Donegan
On the server. If you haven't changed anything the php-files handler saves the session data to files in the directory specified by session.save_path, see http://docs.php.net/session.configuration#ini.session.save-path
VolkerK
Ah gotcha. Thanks ... again VolkerK
Keith Donegan
+2  A: 

Whenever a PHP page loads, just call session_start();. This will either 1. start a new session if one doesn't exist, or 2. restart whatever session was previously established. To make a new session variable, do this:

session_start(); // start or restart the session
$_SESSION['mySessionVariableName'] = "My session variable value.";

If you want to kill a session so that you can start a new one, you can do:

session_destroy();

If you want to erase all session variables but not kill the current session, you can do:

session_unset();
John Nicely
+1  A: 

Simple: on red-widgets.php:

 session_start(); 
 $_SESSION['red-widgets'] = 1;

on blue-widgets.php:

session_start();
 if(!$_SESSION['red-widgets']){
  //Joe weren't on red-widget.php, diffrent action here...
}
Rin