Suhail Gupta
Suhail Gupta

Reputation: 23216

How does session_start know, whether to resume or start the session?

I don't exactly understand how does session_start work ? For example, here is a script named :

tester.php

<html>
 <body> <head> <title> tester.php </title> </head>

   <?php
     session_start();
     $_SESSION['Mir Taqi Mir'] = "Dekh toh dil ke jaan se uthta hai,yeh dhuan sa kahaan se uthta hai";
   ?>

   <a href='./try.php'>Click to follow</a>

 </body>
</html>

and a script named try.php :

session_start();

if(isset($_SESSION['Mir Taqi Mir']))
 {
   echo "Value of the session variable :".$_SESSION['Mir Taqi Mir'];
 }

When I follow down to try.php from tester.php how does session_start know that it has to resume a session and not start a new session ?

The documentation states : session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. What identifier ? I do not understand this.

Upvotes: 1

Views: 2579

Answers (1)

GManz
GManz

Reputation: 1659

If you view your cookies, there should be a PHPSESSID cookie that will contain a random string, which PHP uses to identify a session. If it doesn't exist, it will create a new one and set that cookie (providing it is able to do so, i.e. headers aren't sent yet amongst other things). Try doing a var_dump($_COOKIE); on try.php.

Upvotes: 4

Related Questions