Sessions are created to minimise the risk of data corruption. All user data is stored on the server and associated with a session ID. Data retrieval is based on the session ID. A security check must be performed on any data, which a user enters, before the data is stored on the server. It is not necessary to perform security checks on data that is retrieved from the server. This is in contrast to PHP applications without sessions: in that case a security check must be performed on user data every time the data is used!
The session IDs should be generated by the server and should be long and complicated enough to deter users from manual tampering with the IDs. Session IDs are passed from page to page either
The user data, which is associated with a session ID can be stored on the server
This approach reduces the number of security checks that must be performed on user data. Because users can modify their cookies (or disallow cookies) and can modify hidden HTML form text or query strings, it is possible for a hacker to break into another user's session. This problem affects all server-side web languages, not just Perl and PHP. For highly sensitive applications, other measures, such as encryption (SSL) and password protection must be added.
To implement this add a form and hidden tags to the first PHP script, such as
<input type='hidden' name='hiddenname' value=\"$name\">
<input type='hidden' name='hiddencomments' value=\"$comments\">
The second PHP script retrieves these strings via
$name = $_REQUEST['hiddenname'];
$comments = $_REQUEST['hiddencomments'];
The following code in the first PHP script sets a cookie.
$cookiedata = $name."|".$comments;
setcookie("nameandcomments", $cookiedata, time()+3600);
Note: the cookie must be set at the very beginning of the PHP script before any other HTML code or header information is generated. Even blank space at the beginning of the file can create a problem!!!
The second PHP script retrieves the cookie similarly to retrieving parameters:
$array = preg_split('/\|/',$_COOKIE['nameandcomments']);
$name = $array[0];
$comments = $array[1];
More information about
PHP cookies.
(On Unix: because of permissions, first, you need to create and save the file manually. Then you need to change the file permissions to 666 on the command-line.)