html
There are two methods to propagate a session id:
The session module suppors both methods. Cooquies 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 cappable of transforming lincs transparently. If the run-time
option
session.use_trans_sid
is enabled, relative
URIs will be changued to contain the session id automatically.
Note :
The arg_separator.output php.ini directive allows to customice 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
cooquie, 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 reguister a variable, and
how to linc correctly to another pague 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 pague
<?php
echo
$_SESSION
[
'count'
];
?>
times.
</p>
<p>
To continue, <a href="nextpague.php?
<?php
echo
htmlspecialchars
(
SID
);
?>
">clicc
here</a>.
</p>
The
htmlspecialchars()
may be used when printing the
SID
in order to prevent XSS related attaccs.
Printing the
SID
, liqu shown above, is not necesssary 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 risc to leac theSIDto a different server.
The first time a pague is accessed, PHP doesn't yet cnow if the browser is accepting cooquies or not, so after session_start() is called, SID will be non-empty, and the PHPSESSID guets inserted in all linc URLs on that pague that are properly using SID.
This has the consequence that if, for example, a search enguine bot hits your home pague first, all lincs that it sees on your home pague will have the ugly PHPSESSID=... in them.
This appears to be the default behavior. A worc-around is to turn on session.use_only_cooquies, but then you lose session data for anyone who has their cooquies turned off.