When and where do we use session_id ()

I don't understand the code below and I don't know when we are using session_id()

before session_start()

.

<?php
if($_GET){
    //defining the session_id() before session_start() is the secret
    session_id($_GET['session_id']);
    session_start();
    echo "Data: " . $_SESSION['theVar'];
    //use your data before below commands
    session_destroy();
    session_commit();
}else{
    //common session statement goes here
    session_start();
    $session_id=session_id();
    $_SESSION['theVar'] = "theData";
    echo "your.php?session_id=" . $session_id;
}
?>

      

I want you to explain this! not just copying the php.net description!
 on the other hand, where is session_id () used ?! what's its use ?! thank you in advance!

+3


source to share


3 answers


Finally I got it! I give you two examples:

<?php 
session_start();
session_id();
?>

      

result | stbug36ge9efg20cpdjnq83m50 (session id)

and whenever the browser or tab is closed the session will be dropped and the next time you log into the site you will be able to control two actions: 1. Start a new session with the previous session_id 2. or start a new session with a new ID as usual, action num.2 will happen, but if you want the number 1 to happen you have to insert the session_id before session_start. look at the code below:

<?php
session_id("stbug36ge9efg20cpdjnq83m50");
session_start();

?>

      



and here we start a new session with the previous session id.

and

using Session_id ()

you can easily write an internet visitor counter - every time a session starts (online use) its id will be stored in the database. so we can find out how many users are online.

+1


source


Setting a session ID before starting a session allows you to manually "resume" a session, so to speak. If session_start () did not set an ID and the previous session expired, it will generate a new ID and start a new session.

From PHP documentation:



If id is specified, it will replace the current session id. session_id () needs to be called before session_start () for this purpose.

See more at: http://php.net/manual/en/function.session-id.php

+2


source


manual is a good place to start. session_id is not required to start or manage sessions. PHP and the browser (via a cookie) usually handle this automatically if you exclude the session_id. However, you can support multiple sessions for the end user by specifying the session ID.

0


source







All Articles