Multiple PHP 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

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).

In PHP, is there any harm in running session_start() multiple times?

From the docs:

As of PHP 4.3.3, calling session_start() after the session was previously started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.

So no, it won't "cause harm", but it'll throw an error. And the fact that it's happening is probably an indicator that you're doing something incorrectly, and might need to re-think how your code is laid out.

Removing / Timing / Destroying out multiple php sessions variables at once

NewLeaner as your aware from my last question i answered for you, I'm still learning the basics myself so i am far from experienced however i overcome a problem like this during the coding of my own application and as Darren mentioned in a previous comment, Having a list of variable values within an array will allow you to unset the relevant sessions accordingly based on the array values matching the session variable values.

Below is an example of your session variable values:

  1. Fullname Session: $_SESSION['fullname']
  2. Phonenumber Session: $_SESSION['Phonenumber']
  3. Emailaddress Session: $_SESSION['Emailaddress']

$unset = array('Fullname','Phonenumber','Emailaddress'); 
foreach( $unset as $sessions ) {
unset($_SESSION[$sessions]);
}

This will unset and remove the above sessions such as Fullname, Phonenumber, Emailaddress and any others you wish to add to the array.

You can add more Values to the array using single quotes and comma seperated, Here is an example to add further values.

$unset = array('Fullname','Phonenumber','Emailaddress', 'Variable_4', 'Variable_5', 'Variable_6', 'Variable_7', 'And so on...');

Hope this helps once again!

PHP $_SESSION for multiple users at once

No. There is a seperate $_SESSION created for each user. This is all done by the server, you don't have to worry about it. When writing your code, treat the $_SESSION as if there was only one user on the site.

Edit: Actually, on thinking about it, it is a very good question to ask. It is good to ask these sorts of questions, it means you are seriously thinking about how your code truly works. Keep asking these things, and keep testing. I have a feeling that one day you will be writing some amazing code.

So on that note, here is some info from the apache site:

What is a session?

At the core of the session interface is a table of key and value pairs that are made accessible across browser requests. These pairs can be set to any valid string, as needed by the application making use of the session.

Keeping sessions on the server

Apache can be configured to keep track of per user sessions stored on a particular server or group of servers. This functionality is similar to the sessions available in typical application servers.

If configured, sessions are tracked through the use of a session ID that is stored inside a cookie, or extracted from the parameters embedded within the URL query string, as found in a typical GET request.

And from the PHP docs on Sessions:

Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

The session support allows you to store data between requests in the $_SESSION superglobal array. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

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.



Related Topics



Leave a reply



Submit