PHP How to Create Multiple Sessions

PHP How can I create multiple sessions?

What you need to use is session_id() instead of session_name()

<?php

session_id("session1");
session_start();
echo session_id();
$_SESSION["name"] = "1";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();

session_id("session2");
echo session_id();
session_start();
$_SESSION["name"] = "2";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();

session_id("session1");
echo session_id();
session_start();
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();

session_id("session2");
echo session_id();
session_start();
echo "<pre>", print_r($_SESSION, 1), "</pre>";

This will print:

session1

Array
(
[name] => 1
)

session2

Array
(
[name] => 2
)

session1

Array
(
[name] => 1
)

session2

Array
(
[name] => 2
)

session_id is an identifier for a session, which helps in distinguishing sessions. session_name is only a named alias for the current session

Can I have multiple sessions at the same in PHP?

I would like to know if it's possible to have two sessions at the same time.

Well... no. You can't have multiple simultaneous sessions. But you can start one, then close it with session_write_close and start another session... if it's really required.

session_id("player"); // are you sure that you don't want use `session_name` here?
session_start();

// ... work with session id "player"

session_write_close();

session_id("team");
session_start();

// ... work with session id "team"

Also, you may want use session_name instead of session_id. One sets the current session name, the other the SID.

Multiple PHP Sessions

There is an easier way: session_name.

Prior to calling session_start(); call session_name("something"); (where you change something to whatever you want it to be called).

PHP: session_start() called 'simultaneously' in multiple tabs creates multiple sessions

The best solution I could come up with was to move the session "creation" (i.e., setting session variables) into the ajax.php file, executed only after a user has successfully sent their uname/pwd and so they are about to be redirected to a new page anyway (i.e., welcome.php). This means that login.php cannot be guaranteed to have access to any session variables set by ajax.php whatsoever, so it's just a dumb page that relies solely on its ajax calls to know what's going on. As it turns out, this isn't such a hassle after all.

Multiple sessions in one instance using PHP?

If you want to share information between users, using a session is not the best idea as it uses the file system. You would be better off using the database which handles all the issues of locking, concurrency etc.

Although what you ask for is technically possibly, I would strongly recommend against it.

EDIT

Assuming I have understood your requirement correctly, here is how I would do it:

  1. Use session only to store session data related to that user. It could include something like:

    $_SESSION['name'] = 'test name';
    $_SESSION['groupid'] = 2;
  2. A MySQL DB and table with fields groupid, XXXXX (data you want to store), timestamp

Whenever anyone updates information for a particular group id, you update the timestamp.

Then run a simple cronjob to check if any current time - timestamp > 3600 (one hour) and you can consider that as stale and delete those records.



Related Topics



Leave a reply



Submit