作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我现在有一个 cometd 页面,如果我在顶部调用 session_start
那么 session 就会卡住,我的解决方法是这样的:
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;
}
}
...
有没有更好的方法让我获取不进入 session 文件的 session 变量?
最佳答案
您的解决方法可以更简单:
<?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();
}
您可以编写一些辅助函数,例如:
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;
}
关于php - 如何在不调用 session_start 的情况下获取 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5748587/
我是一名优秀的程序员,十分优秀!