Use PHP sessions cookies

Sessions and Cookies

Sessions and cookies allow To save variables across all requests.


session

Sessions allow variables save within a session. The session extends from the explicit start of the session to the explicit destruction of the session or the closing of the browser. To a Start session, the function must be called. The function must be called in every file in which the session variables must be read or written to. Of course, individual session variables only exist from the point in time at which they were assigned (initialized) for the first time. If you want to check whether a session variable exists, you can use the function. Would you like single Delete session variables again, so you can use the function to which you have to transfer the variable to be deleted. Should all session variables and thus the complete Session deleted you can call the function.

When the function is called for the first time, a so-called Session ID generated. This is then used to create the Identify visitors. The ID can either be given in the URL or saved for the visitor using a cookie. In most cases, the cookie variant is used because the other brings some problems with it, which is why we will not go into these in more detail. With the cookie variant, when the cookie is set, the browser is informed that the cookie only valid during this browser session is why the browser does not necessarily have to save the cookie as a file. The cookie for the session ID is named by default. The values ​​of the session variables cannot be viewed or manipulated by the user, as these stored on the server are. The session variables are accessed using the superglobal associative array. In the following example, the form entries (first name and surname) are saved in a session. Go to the page, fill out the form, close the page, go back to it and you will find that your form entries are still there. If you close the browser and call up the page again, you will find that the form fields are empty again.

<?php session_start(); if (isset($_POST['vorname']) && isset($_POST['nachname'])) { $_SESSION['vorname'] = $_POST['vorname']; $_SESSION['nachname'] = $_POST['nachname']; } ?><form action="session.php" method="post"> <table> <tr> <td style="width: 100px;">Vorname:</td> <td><input type="text" name="vorname" value="<?php echo (isset($_SESSION['vorname']) ? $_SESSION['vorname'] : ''); ?>" /></td> </tr> <tr> <td style="width: 100px;">Nachname:</td> <td><input type="text" name="nachname" value="<?php echo (isset($_SESSION['nachname']) ? $_SESSION['nachname'] : ''); ?>" /></td> </tr> <tr> <td></td> <td><input type="submit" value="Session schreiben" /></td> </tr> </table> </form>

Cookie

In simplified terms, a cookie is a cookie Text file, Which stored on the visitor's computer becomes. A cookie is valid for as long as specified, this can only be the current browser session (e.g. with the session ID cookie) or until a certain specified time. Cookies are set by the web server and can be set in PHP using the function. The function is assigned the cookie name, the cookie value, the validity, the path, the domain, a value for specifying whether the cookie should only be sent with HTTPS, and a value for specifying whether the cookie can only be reached via HTTP is passed. All parameters, apart from the first parameter for the cookie name, are optional. Usually, however, the parameters for the cookie value and the validity are also transferred. The other parameters are required less often. The Validity parameters a UNIX timestamp is transferred to allow the Expiration time of the cookie. If 0 is passed here, the cookie is only valid until the end of the browser session. To a Delete cookie, transfer an expired timestamp (e.g.) as the expiration time. With the path and domain parameters it is possible to set the Validity of the cookie. By default, a cookie only applies to the current directory and its subdirectories. Calling the function, like calling the function, must be done before any kind of output is sent. After a cookie has been set, its value is used when a request sent from the browser to the server. In PHP, the cookie value can be accessed using the superglobal associative array. The following example is similar to the session example above. However, cookies and, which are valid for 1 hour, are used to save the form data.

<?php if (isset($_POST['vorname']) && isset($_POST['nachname'])) { // Aktuelle Werte lokal merken $vorname = $_POST['vorname']; $nachname = $_POST['nachname']; // Cookies schreiben setcookie('vorname', $vorname, time() + 3600); setcookie('nachname', $nachname, time() + 3600); } else { // Aktuelle Werte wenn verfügbar aus Cookie laden $vorname = isset($_COOKIE['vorname']) ? $_COOKIE['vorname'] : ''; $nachname = isset($_COOKIE['nachname']) ? $_COOKIE['nachname'] : ''; } ?><form action="cookie.php" method="post"> <table> <tr> <td style="width: 100px;">Vorname:</td> <td><input type="text" name="vorname" value="<?php echo $vorname; ?>" /></td> </tr> <tr> <td style="width: 100px;">Nachname:</td> <td><input type="text" name="nachname" value="<?php echo $nachname; ?>" /></td> </tr> <tr> <td></td> <td><input type="submit" value="Cookie schreiben" /></td> </tr> </table> </form>