Reputation: 146310
I right now have a comet page, that if i call session_start
at the top then the session freezes, my work-around that do is this:
function getTables($sessionID = null){
if(!isset($this->output)){
$this->output = array();
}
if(!isset($this->output[$this->sessID])){
$this->output[$this->sessID] = array();
}
$hostname = php_uname("n");
$sess_ini = session_save_path();
chdir($sess_ini);
if(false === ($save = @file_get_contents("sess_$this->sessID"))){
$cwd = getcwd();
chdir($this->location);
return (isset($this->returnJSON[$this->sessID])?$this->returnJSON[$this->sessID]
:json_encode(array(
'sess_local'=>"$sess_ini/sess_$this->sessID",
'save'=>$save,
'hostname'=>$hostname,
'cwd'=>$cwd,
'reg_local'=>$this->location,
)
)
);
}
chdir($this->location);
//session_id($sessionID);
$sessions = explode("|",$save);
$_SESSION['all'] = array();
foreach($sessions as $key=>$sess){
if(trim($sess)=='all' && isset($sessions[$key+1])){
$_SESSION['all'] = unserialize(trim(urldecode($sessions[$key+1])));
break;
}
}
...
Is there a better way for me to be getting the session vars thats not going into the session files?
Upvotes: 2
Views: 7237
Reputation: 70500
Your work around can be much simpler:
<?php
//yes, start normally.
session_start();
//now, immediately harvest the variables you need to remember from the $_SESSION
$somevar_you_want_to_remember = $_SESSION['somevar'];
//close the session, so it won't lock:
session_write_close();
//disable some errors which aren't really errors:
ini_set('session.use_cookies',false);
session_cache_limiter(false);
//you are now free to do anything you like:
while(true){
echo "<script>window.parent.test_function('".time().' sessionvar: '.$somevar_you_want_to_remember."');</script>";
flush_buffers();
sleep(1);
//if you need to refresh your variable, you can just reopen the session:
session_start();
$somevar_you_want_to_remember = $_SESSION['somevar'];
//and immediately close again
session_write_close();
}
You could write some helper functions, like:
function SaveQuickSessionVar($name,$value){
session_start();
$_SESSION[$name] = $value;
session_write_close();
}
function GetQuickSessionVar($name){
session_start();
$var = $_SESSION[$name];
session_write_close();
return $var;
}
Upvotes: 3
Reputation: 360762
PHP's default session handler locks the session file while it's in use by a script. Comet keeps the handler script active for long periods, which keeps the session locked. You can work around it by doing
session_start();
session_write_close();
in your Comet script. It'll populate $_SESSION with the stored data, and then close the session file and relinquish the lock on it. The data in $_SESSION will still be present and usable, and you can even change it. The only difference is that unless you do a subsequent session_start()
in your script, those changes will not be written out to the on-disk session file.
So, you CAN use regular PHP sessions in your script, you just have to take care that any long-running sections have the session closed before that section starts executing.
Upvotes: 3