Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when and where do we use session_id()

I can't understand the code below and I don't know when we use 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 it ! not just copying the description of php.net !
on the other hand , where is session_id() used ?! what's its usage ?! thank you in advance !

like image 948
Mehdi Avatar asked Jul 19 '15 22:07

Mehdi


People also ask

Why do we need a session ID?

A session ID is a unique number that a Web site's server assigns a specific user for the duration of that user's visit (session). The session ID can be stored as a cookie, form field, or URL (Uniform Resource Locator). Some Web servers generate session IDs by simply incrementing static numbers.

Where can I find session ID?

Session IDs are typically found in the Request (NCSA) field, the URI-Query (W3C), or the Cookie field: If the ID is found in the URL itself, it will be in the Request field for Apache servers and in the URI Query field for IIS servers.

What is session ID example?

The session ID can be defined by a command line option or a resource. The session ID can be a single value; for example “Smith". A set of session Ids can be defined; for example, Smith+n where n is 3 would make 3 session Ids available, “Smith1", “Smith2", and “Smith3".

What is the function Session_name () used for?

The session_name() function is used to name the current session or, retrieve the name of the session.


1 Answers

finally i understood ! 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 omitted and the next time you enter the site , you can manage two action to occur : 1. start a new session with previous session_id 2. or start a new session witt a new id as the usual , the action num.2 will happen but if you want num.1 to happen you have to embed session_id before session_start . look at the code below :

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

?>

and here we're starting a new session with previous session id .

and

the usage of Session_id()

you can easily write a online visitors counter -- each time a session starts(use is online ) , its id will store in database . so we can find out how many users are online.

like image 161
Mehdi Avatar answered Oct 26 '22 00:10

Mehdi