Passing the Session ID
There are two methods to propagate a session id:
- Cookies
- URL parameter
The session module supports both methods. Cookies are optimal, but because they are not always available, we also provide an alternative way. The second method embeds the session id directly into URLs.
PHP is capable of transforming links transparently. If the run-time option session.use_trans_sid is enabled, relative URIs will be changed to contain the session id automatically.
Note:
The arg_separator.output php.ini directive allows to customize the argument separator. For full XHTML conformance, specify & there.
Alternatively, you can use the constant SID
which is
defined if the session started. If the client did not send an appropriate session
cookie, it has the form session_name=session_id.
Otherwise, it expands to an empty string. Thus, you can embed it
unconditionally into URLs.
The following example demonstrates how to register a variable, and
how to link correctly to another page using SID
.
Example #1 Counting the number of hits of a single user
<?php
session_start();
if (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
?>
<p>
Hello visitor, you have seen this page <?php echo $_SESSION['count']; ?> times.
</p>
<p>
To continue, <a href="https://www.gaudry.be/en/php-rf-nextpage.html?<?php echo htmlspecialchars(SID); ?>">click
here</a>.
</p>
The htmlspecialchars() may be used when printing the SID
in order to prevent XSS related attacks.
Printing the SID
, like shown above, is not necessary if
--enable-trans-sid was used to compile PHP.
Note:
Non-relative URLs are assumed to point to external sites and hence don't append the
SID
, as it would be a security risk to leak theSID
to a different server.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-session.idpassing.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.