note 114557 added to function.session-id
| From: | infinito84 at gmail dot com | Date: | Thu, 06 Mar 2014 05:50:27 +0000 |
| Subject: | note 114557 added to function.session-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198809@lists.php.net to get a copy of this message | ||
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:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
function get_global($key){
//Get current session
if(session_status()!=PHP_SESSION_ACTIVE)session_start();
$current_id=session_id();
session_write_close();
//Set a global session with session_id=1
session_id(1);
session_start();
//Get superglobal value
$value=null;
if(isset($_SESSION[$key]))$value=$_SESSION[$key];
session_write_close();
//Set the before session
session_id($current_id);
session_start();
return $value;
}
function set_global($key,$value){
//Get current session
if(session_status()!=PHP_SESSION_ACTIVE)session_start();
$current_id=session_id();
session_write_close();
//Set a global session with session_id=1
session_id(1);
session_start();
//Set superglobal value
$_SESSION[$key]=$value;
session_write_close();
//Set the before session
session_id($current_id);
session_start();
}
//Example
//Begin my session normally
session_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 set
if($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[]="<span style='color:".$_SESSION['color']."'>Value:
".rand(0,100)." SessionID: $id</span><br>";
//Save the superglobal
set_global("test",$test);
//Show the superglobal
foreach($test as $t){
echo $t;
}
echo "<b>Reloads = ".$_SESSION['count'].", <span
style='color:".$_SESSION['color']."'>This my
color</span></b>";
exit;
?>
----
Server IP: 194.8.30.16
Probable Submitter: 186.98.188.36
----
Manual Page -- http://php.net/manual/en/function.session-id.php
Edit -- https://master.php.net/note/edit/114557
Del: integrated -- https://master.php.net/note/delete/114557/integrated
Del: useless -- https://master.php.net/note/delete/114557/useless
Del: bad code -- https://master.php.net/note/delete/114557/bad+code
Del: spam -- https://master.php.net/note/delete/114557/spam
Del: non-english -- https://master.php.net/note/delete/114557/non-english
Del: in docs -- https://master.php.net/note/delete/114557/in+docs
Del: other reasons-- https://master.php.net/note/delete/114557
Reject -- https://master.php.net/note/reject/114557
Search -- https://master.php.net/manage/user-notes.php