int setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])
This defines a cookie to be sent along with the rest of the header
information. Cookies must be sent before any other headers are sent
(this is a restriction of cookies, not PHP). This requires you to place
calls to this function before any <html> or <head>
tags.
All the arguments except the name argument are optional. If only the name argument is present, the cookie by that name will be deleted from the remote client. You may also replace any argument with an empty string ("") in order to skip that argument. The expire and secure arguments are integers and cannot be skipped with an empty string. Use a zero (0) instead. The expire argument is a regular Unix time integer as returned by the time() or mktime() functions. The secure indicates that the cookie should only be transmitted over a secure HTTPS connection.
Once the cookies have been set, they can be accessed on the next page load with the "autoglobal" array named $_COOKIE.
Common pitfalls:
The term "session" refers to the time that a user is at a particular web site. For example, a typical online shopping session might include logging in, putting an item into the shopping cart, going to the checkout page, entering address and credit card data, submitting the order, and closing the browser window.
Unfortunately, HTTP has no mechanism to maintain state: individual requests aren't related to each other. Therefore, the web server can't easily distinguish between single users and doesn't know about user sessions.
Session management is a server-side mechanism to save data about visits from the same user. PHP 4 includes native session management functions to ease the task of managing user sessions.
To associate session data with a user, you need a session identity number
saved on the client, which the client sends to the server with each request.
When you start a session either way, the following happens:
Either the session_register() function, or assigning to an element of the array $_SESSION , creates a new global variable which is stored throughout the session. All variables you want to preserve across page requests must be registered. Don't mix both methods for registering session variables.
This is an example of a counter that increments for each page of a session. You would put this code in each script that might be executed as part of the session.
session_start();Note that session_register() or $_SESSION takes a string that is the name of a variable as argument, not the variable itself.
print($_SESSION["counter"]);
$_SESSION["counter"]++;
It's as easy to use session variables in a script as it is to use GET/POST
variables. If you register a variable named "foo" then
$foo is accessible automatically after calling session_start().
You can also register objects as session variables.
script.php?<session-name>=<session-id>.You can create a URL like this explicitly using the PHP global constant named SID:
printf('<a href="script.php?%s">Link</a>', SID);In the above, %s is a format that controls how the value of SID is printed.
With automatic URL rewriting, the session ID in the GET form is added automatically to all relative links within PHP-parsed pages (not to absolute links to external web sites).
Automatic URL rewriting should be used with caution on high-performance
sites. PHP has to analyze whether each page contains relative links,
then add the ID to the links. This introduces a performance penalty.
Cookies are set only once, and avoid the overhead of URL rewriting.
Therefore, when dealing with sensitive information, there should always be additional methods to decide whether it is a valid session. Sessions are not completely reliable as a secure authentication mechanism.
Sessions rely on the session ID, meaning an attacker can 'steal' a session
by stealing the session ID. This can be made harder by using a cookie,
but security then relies on the user closing all browser windows, to expire
the session cookie. Even then session cookies can be sniffed on a
network or logged by a proxy server.