0

i am work on a project and use php sessions to store some informations. In one script i start the session with session_start() and create some $_SESSION['var']s. In another script i want to get this variables, but there will create a new session with session_start() with a new sessionid. I am not sure whats happend there. Why there will create two different sessions in one project. How to fix that ?

Some php configuration parameters:

session.use_cookies = 1
session.use_only_cookies = 1
session.use_trans_sid = 0
expose_phps= On

The sessionid use cookies.

Best regards,

  • 4
    There's a lot of reasons this can happen. If some of your pages are prefixed with `www` and some are not, the server interprets those as different websites, and assigns different sessions. If part of your site uses SSL and part doesn't, they also can have different sessions. If the end user uses different browsers or devices, they are going to have different sessions. Can't tell which if any of those apply from your question. – mopsyd Nov 21 '17 at 09:53
  • Thank you for support, it is only one domain, different directories and SSL is at the moment not in use. On some other server with other domain the code works well, maybe there is some configuration problem ? – UepsilonZädd Nov 21 '17 at 10:44
  • In your browser console, check the cookies, and see if they have different hash values for `PHPSESSID`. This will reveal whether your problem is likely a server configuration issue (if they are different), or a code logic issue (if they are not). It may still be a problem in the code logic if they are the same, but it is much less likely. – mopsyd Nov 21 '17 at 17:52
  • As a general rule, it's usually a good idea to always use www or always exclude it, so you can entirely rule out that from being a cause in all cases. For example if you want to always include it, you might use something [like this](https://stackoverflow.com/a/4159124/1288121). – mopsyd Nov 21 '17 at 17:54

1 Answers1

0
<?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>"; ?>

Output:

  session1

Array
(
    [name] => 1
)

session2

Array
(
    [name] => 2
)

session1

Array
(
    [name] => 1
)

session2

Array
(
    [name] => 2
)

You can try that way

Nims Patel
  • 1,048
  • 9
  • 19