php 传入session id,PHP: session_id - Manual

Get a shared session.

Sometimes is good can interchange messages and vars between one session and another, but PHP dont support this. I create this script that allows with session_id() change from current session to shared session (this is, info with scope to all sessions) for read and write info and back in to user session. The code:

ini_set('display_errors',1);error_reporting(E_ALL);

functionget_global($key){//Get current sessionif(session_status()!=PHP_SESSION_ACTIVE)session_start();$current_id=session_id();session_write_close();//Set a global session with session_id=1session_id(1);session_start();//Get superglobal value$value=null;

if(isset($_SESSION[$key]))$value=$_SESSION[$key];session_write_close();//Set the before sessionsession_id($current_id);session_start();

return$value;

}

functionset_global($key,$value){//Get current sessionif(session_status()!=PHP_SESSION_ACTIVE)session_start();$current_id=session_id();session_write_close();//Set a global session with session_id=1session_id(1);session_start();//Set superglobal value$_SESSION[$key]=$value;session_write_close();//Set the before sessionsession_id($current_id);session_start();

}//Example

//Begin my session normallysession_start();

if(empty($_SESSION['count'])){$_SESSION['count']=0;$_SESSION['color']="rgb(".rand(0,255).",".rand(0,255).",".rand(0,255).")";

}$_SESSION['count']++;$id=session_id();//Get the superglobal$test=get_global("test");//Set the superglobal test with empty array if this dont setif($test==null)$test=array();//Get the superglobal$test=get_global("test");//Set values for each reload page and save the session_id that create it$test[]="Value: ".rand(0,100)." SessionID:$id
";//Save the superglobalset_global("test",$test);//Show the superglobalforeach($testas$t){

echo$t;

}

echo"Reloads = ".$_SESSION['count'].", This my color";

exit;?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值