Page 1 of 1
Session Variables
Posted: Tue Jan 24, 2006 12:29 pm
by spacebiscuit
Hi,
I am having a little trouble with my session variables which do not appear to be working correctly. Here is a simplified version of my code:
Code: Select all
<?
session_start();
session_register ("test1");
session_register ("test2");
require 'quote_routines.php';
if (isset($_POST['submit2'])) {
$HTTP_SESSION_VARS["test2"]=$HTTP_POST_VARS['test2'];
echo $HTTP_SESSION_VARS["test1"];
echo $HTTP_SESSION_VARS["test2"];
}
if (isset($_POST['submit1'])) {
$HTTP_SESSION_VARS["test1"]=$HTTP_POST_VARS['test1'];
echo $HTTP_SESSION_VARS["test1"];
procedure2();
}
else{
procedure1();
}
?>
In short the first time the script is called the function 'procedure1' runs, this is a simple form that asks for a string. The data is then posted back to the main script via a submit button (submit1).
The second if meets the condition this time and I copy the posted value into the session variable, I then echo the variable and it outputs the correct data. So far so good.
I then call procedure2 which does exactly the same as procedure1, ie. collects a string. Again this is submitted and this time the first if condition is met.
Again I copy the submitted data into a session variable and then try to output the data from both of the session variables. I only get output for the 2nd newly created, I get nothing for test1.
Any suggestions, the only way I can see to get this towork is to pass the variables as paramaters but I do not want to have to do this and surely this defeats the object of session variables?
Thanks in advance.
Rob.
Posted: Tue Jan 24, 2006 12:51 pm
by feyd
this is caused by the variable being overwritten with nothing (empty data) .. I'm pretty sure the reason why is because you're mixing deprecated code and newer styles of code.
Lose session_register(), use $_POST instead of $HTTP_POST_VARS and $_SESSION instead of $HTTP_SESSION_VARS for starters..
Posted: Tue Jan 24, 2006 12:53 pm
by spacebiscuit
Hi,
Thanks for the reply, I just tried the script as it wason another server and it worked fine.
Could it be my php config?
This is from my phpinfo file:
Session Support enabled
Registered save handlers files user
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off
Any ideas, I will try the newer variables too though.
Thanks,
Rob.
Posted: Tue Jan 24, 2006 12:59 pm
by feyd
Some servers are set to clean the temporary directory (where sesions are stored) very often.. it may be that the time between submissions is enough to fall between cleanings by the server. For instance, I used to work on a project where the server was set to clean the temporary directory every 10 seconds... so anything over 10 seconds old was thrown out. To work around that, I used database sessions, where I can control everything, how I want them.
It may be good to use them here, or talk to your host to find out how often the temporary directory is cleaned..
Posted: Tue Jan 24, 2006 1:05 pm
by spacebiscuit
The server causing the issues is my own, I configured it and therefore it is not much more than the default standard install of Apache.
Does that shed anylight?
Rob.
Posted: Tue Jan 24, 2006 5:38 pm
by raghavan20
Code: Select all
session.gc_maxlifetime 1440 1440 (from robburne php.ini)
feyd, I do not think the session life time is low, it's the default.
rob, it would be better if you replace those old format of accessing global variables and provide access to your files on the Internet and at the same time post codes of two files.
Posted: Wed Feb 01, 2006 9:00 am
by spacebiscuit
Hi guys,
I tried switching to the new system of referring to sessions and post variables but it has made no difference the same problem is occurring!
It must be a php config issue as on another server it works fine, however it does not on my own server. Any session variable options I should look for in the php.ini file?
Thanks,
Rob.
Posted: Wed Feb 01, 2006 9:09 am
by raghavan20
Is it possible for you to compare the default php.ini settings with the settings on the php.ini file on your server?
EDIT: I should post my session settings...
Code: Select all
; Handler used to store/retrieve data.
session.save_handler = files
; session.save_path = "N;MODE;/path"
;session.save_path = "/tmp"
; Whether to use cookies.
session.use_cookies = 1
; This option enables administrators to make their users invulnerable to
; attacks which involve passing session ids in URLs; defaults to 0.
; session.use_only_cookies = 1
; Name of the session (used as cookie name).
session.name = PHPSESSID
; Initialize session on request startup.
session.auto_start = 0
; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 0
; The path for which the cookie is valid.
session.cookie_path = /
; The domain for which the cookie is valid.
session.cookie_domain =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 1440
session.bug_compat_42 = 1
session.bug_compat_warn = 1
session.referer_check =
; How many bytes to read from the file.
session.entropy_length = 0
; Specified here to create the session id.
session.entropy_file =
;session.entropy_length = 16
;session.entropy_file = /dev/urandom
session.cache_limiter = nocache
; Document expires after n minutes.
session.cache_expire = 180
Posted: Wed Feb 01, 2006 9:10 am
by feyd
my theory has nothing to do with php's settings. I've been on several hosts that have their the clear done via cron...
Posted: Thu Feb 02, 2006 8:00 am
by spacebiscuit
Here is the phpinfo for the server thatis working correctly:
Session Support enabled
Registered save handlers files user
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off
Posted: Thu Feb 02, 2006 8:02 am
by spacebiscuit
They look indentical to me, this is very odd isn't it?
Rob.
Posted: Thu Feb 02, 2006 8:25 am
by Jenk
The clue (more like obvious answer) is in feyd's post.. looks like your host has a cron job that clears the temporary/cache folder quite frequently.
Posted: Thu Feb 02, 2006 9:00 am
by spacebiscuit
The 3rd party paid hosting is the server on which the session variables work fine!
It is my inhouse server that is causing the problems. It is a server I set up and package which I installed comprising Apache and PHP, I have not installed any extras to clear any temp or cache files.
Odd !
Rob.