PHP |
Values |
Operators |
Functions |
Arrays |
Forms |
Sessions-Cookies |
Classes
A session is a small file that a server stores on the user's browser to maintain state until the browser is closed. A session can maintain a username and password, shopping cart items, and other types of information. Session statements are coded on the first line of a PHP file before the XHTML declaration. The session_start() function declares the session...
The session_start() function automatically creates a unique id number when a user visits the website and can be tracked from page to page. When the browser refreshes or goes to another page, data is sent back to the server and accessed in a global array called $_SESSION...
The output is...
Pageviews = 1
The isset() function evaluates if a session has been declared. Each time the browser refreshes, the if() function evaluates true and the counter increments by one...
If the browser is refreshed again, the output is...
Pageviews = 2
The unset() function removes the contents of a session...
The session_destroy() function deletes a session completely...
A cookie is like a session, but is stored on the user's computer instead of the browser and can maintain state over a specified length of time even after the browser is closed. Cookie statements are coded on the first line of a PHP file before the XHTML declaration and passed in the URL. A cookie stores a name and value that becomes available as a variable after the browser is refreshed or goes to another page. The setcookie() function declares the cookie name and value, as well as an expiration time. This cookie uses the time() function to expire one hour from activation...
Cookie data is accessed in a global array called $_COOKIE...
The output is...
This is a value.
The isset() function evaluates if a cookie has been set...
If the cookie is not set, the output is...
Welcome stranger!
A cookie can store multiple names and values...
A cookie can be deleted by declaring a negative expiration time...
An alternative method to deleting a cookie is to declare another cookie statement with the same name...
The mktime() function returns a date and time in standard format in the order of hour, minute, second, month, day, and year. If a value is not declared, it will not be included. This cookie expires at 3 a.m. on July 1, 2008...
Sessions are recommended over cookies. Sessions work even if the user has cookies disabled on their computer. A MySQL database is recommended for storing information over a long period of time.