Passing Session IDs in the Query String

So far, you have relied on a cookie to save the session ID between script requests. On its own, this is not the most reliable way of saving state because you cannot be sure that the browser will accept cookies. You can build in a failsafe, however, by passing the session ID from script to script embedded in a query string. PHP makes a name/value pair available in a constant called SID if a cookie value for a session ID cannot be found. You can add this string to any HTML links in session-enabled pages:

<a href="anotherpage.html?<?php print SID; ?>">Another page</a>

will reach the browser as

<a href="anotherpage.html?
PHPSESSID=08ecedf79fe34561fa82591401a01da1">Another page</a>

The session ID passed ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.