Page 1 of 1

sessions question

Posted: Sun Oct 11, 2009 5:36 am
by mitchphp
I am trying to grock sessions and have a question re multiple users / sessions:

I am building a multiplayer game and I would like to use php to do it using sessions (with session_start() at the top of the home page and the actual code in an include file). I want to use sessions so that during stage 1, stage 2 and stage 3 of the game, the players only have access to their session variables but then at stage 4, the session variables will be available to all the players. I have looked into it and most suggest the use of php and MySQL (which i do in fact plan to use in the end) ... but for now, I am just trying to understand the use of sessions and how they are stored / manipulated on the server.... they show up as arrays, so can I have an include file, with an array variable that pushes session information onto the array stack as new clients join the program?

My question is basically:

If clientA hits the page and a session_start() is initiated, and then clientB goes to the web site and their session_start() is initiated, is there a way for me to create an array that contains the session information for both (and subsequent) clients?

Basically the code I have to test this would be something like:

<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<?php
include("players.inc");
?>
</body>
</html>

and players.inc file would be something like:

<?php
$sessarray = array();
array_push($sessarray, $_SESSION);
?>

(obviously, with a way to see if the session array already exists before its created, so it doesn't get reset every time)

When I try to echo out the $_SESSION after a session_start() has been initiated, i get > Array
But if I set an array variable, such as $sessarray = $_SESSION;
and then if I try to echo $sessarray[0], i get nothing.

Sorry for the longwindedness. Your patience and any help is greatly appreciated.

Re: sessions question

Posted: Sun Oct 11, 2009 8:04 am
by Eric!
There are probably other people with a deeper understanding of how these processes are running on your server, but I would guess you need to create a common place on the server to share this information between running processes.

You could try the shared memory functions to share data with other PHP processes
http://hell.org.ua/Docs/oreilly/webprog ... h05_07.htm
http://us2.php.net/manual/en/book.shmop.php

Or you could just create a database and store the session_id, variables, levels, etc. for each player and use the database as a temporary storage space between PHP processes.

I assume that for good reasons, one PHP process can not access memory data of another PHP process. This is why people are recommending you use some form of database, or you could try the block of shared memory (I've never tried it). Perhaps there is a trick to share your array data between processes other than shared memory...I don't know.

FYI -- you probably aren't accessing the $sessarray properly with your simple index of [0] because $_SESSION itself is an array, so $sessarray is an array of arrays. Take a deeper look at the $sessarray with something like this echo:

Code: Select all

echo "<pre>".print_r ($sessarray)."</pre>;