#native_company# #native_desc#

Using sessions on various versions of php

By Daniel
on November 24, 2002

This is my first tip so please go easy on me 🙂

I came across a problem when using php on my local machine that Im sure some others have also come across,
that is that on my test machine I was coding according to the newer standards (php 4.2.3),but my scripts were
not working when I loaded them onto a server because the server still had an older version of php.

This tutorial I hope will help others to overcome a similar problem.

The beginning

To start first visualise the situation. You are coding a website that needs to use sessions, you have downloaded and
are coding for higher than PHP 4.0.6 so you use the $_SESSION[] variable and you get everything to work.
The next step is to upload all of your work to the server where you could get an ndefined variable error if you
havent checked the version running on the server (guilty :-)). You then have to change your code to use the
$HTTP_SESSION_VAR[] instead of $_SESSION[]. This could cause a lot of extra work not only for us but also
for the technicians involved in fixing the machines from violent outbursts. It can be avoided however,
with a little forethought.

So how can we fix it, well it just needs a 2 small classes to determine your version of php and to read/write
accordingly and then we use the classes to set our sessions according to the version. The following is an
example that can probably be cleaned up a lot but that does work. Ill explain the code in detail afterwards.

The code

class add_to_session{

	var $ver;
	// constructor method for the class
	function add_to_session($name, $value){
		$this->ver = phpversion();

		If ($this->ver <= "4.0.6"){

			$HTTP_SESSION_VARS[$name] = "$value";

		}elseif($this->ver > "4.0.6"){

			$_SESSION[$name] = "$value";



} // end class add_to_session

class get_from_session{
	var $ver;

	function read_session($name){
		$this->ver = phpversion();

		if ($this->ver <= "4.0.6"){
			echo $HTTP_SESSION_VARS[$name];

		}elseif ($this->ver > "4.0.6"){

			echo $_SESSION[$name];

			} // end if -elseif

	} // end function read_sessions

}// end class read_session


The explanation

Youll notice that there are two classes here which Ill explain one at a time line by line

The first class:

As the name of the class suggests, this class is used to add a value to the session array.

Start the php and define the first class

class add_to_session{

//declare the variable $ver that will be used in the class

var $ver;

//start the constructor method of the class with two arguments one for the name of the session
//array value and one for the value itself function add_to_session($name, $value){ //get the version of php being used and assign it to the defined variable $ver $this->ver = phpversion() /* now comes the part that decides which of the styles of session you are going to use.
If you still have an older version of PHP (< php 4.0.6) then you want sessions
variables to be placed in the older
style of $HTTP_SESSION_VARS[] */ if ($this->ver <= "4.0.6"){ //Whoops nearly forgot to set the older HTTP_SESSION_VARS as global global $HTTP_SESSION_VARS; $HTTP_SESSION_VARS[$name] = "$value"; } //or else you would want to use the newer auto global array of $_SESSION[] elseif($this->ver > "4.0.6"){ $_SESSION[$name] = "$value"; } // end if - elseif } // end method //now we end of our first class } // end class add_to_session /*And start our second which reads the info, that you choose with the argument, out of
the session array and echos the value. It starts the same as the first class
which is where the code could be optimized. There is only
one method with one argument.*/ class get_from_session{ var $ver; function read_session($name){ //now we again get the version of php and if it is older than version 4.1.0 then it reads and
//echos the old style $HTTP_SESSION_VARS[] or else it reads and echos the newer $_SESSION[] $this->ver = phpversion(); if ($this->ver <= "4.0.6"){ global $HTTP_SESSION_VARS; echo $HTTP_SESSION_VARS[$name]; }elseif ($this->ver > "4.0.6"){ echo $_SESSION[$name]; } // end if -elseif } // end function read_sessions //now we end off the second class }// end class read_session //close the php ?>

and save as session_class.php

Using the classes

Now whenever you need to use sessions you would do something like this. Remember too you need to make
sure you have a subdirectory (default is /tmp in php) in your root directory for your sessions to work since php 4.0.6.
Please note that you still have to start the session at the beginning of each page


// add  a session variable fname

$new_session_var1 = new add_to_session("fname", "First");

// add  a session variable sname

$new_session_var2 = new add_to_session("sname", "Name");

// read the session and print its variables

$read_session = new get_from_session();

echo ;


echo "&nbsp;";


echo n;


The result:

First Name

The conclusion

Sessions should now be set and read easily no matter what version of php you or your server is running.
Of course we dont want to forget to destroy the sessions when were finished.

I hope that this tutorial is helpful to someone, if not well as I said it is my first, please go easy.