![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Junior Member (25+)
|
I get on average about 5 emails, private messages, etc. a week asking about cookies. Whether it be PHP, Visual basic, Delphi, etc. This tutorial will explain to you how to set, check, and use cookies with PHP.
Remember! If you like this tutorial, I can always use some reputation I. What are cookies? Cookies are small text files that a website can store on your computer to store useful data such as a username, password, ID, last visit time, etc. i. Server to Client All cookies are sent from the server via headers and the header always looks like this: Code:
Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure Let me go through each part: Set-Cookie: - This always precedes the rest of the parameters of the cookie. It basically tells your web browser (IE, Mozilla, etc.) that the following information regards setting a cookie Name=Value; - Name is the name of the cookie. This tells your browser that after the equals sign, the name of the cookie is coming. You may notice the semicolon at the end, this tells the browser that it is the end of that parameter. Every other parameter also ends with a semicolon. The name of the cookie is useful because your browser stores your cookies by name and the website you just visited can open those cookies using the name. expires=date; - expires is optional and it tells the browser when the cookie expires. If expires is not given, the cookie will automatically expire when the user leaves the website. Here is the syntax of "date": Code:
Wdy, DD-Mon-YYYY HH:MM:SS GMT path=PATH; - Path tells the browser where the cookie is valid. If set to "/", the cookie is valid anywhere on the website, if set to "/foo", the cookie is only valid at "/foo", "/foobar", "/foo/bar.html", etc. Path is also optional and if no path is given then it is set to "/". domain=DOMAIN_NAME; - Domain is the domain in which the cookie is valid. If the domain is set at "neopets.com", then only at "neopets.com" will the cookie be sent to the website. If the domain is ".neopets.com", then it is valid at "foo.neopets.com", "bar.neopets.com", "neopets.com", etc. This parameter is required. secure - If secure is present, the cookie will only be sent through a secure connection (HTTPS) This is optional. When a web browser goes to a website, it automatically sends cookies for you to the website. Before sending the cookie, it checks a number of things: Domain - First it checks if the domain matches with the cookies domain. So a cookie set at "nhacks.com" wont be sent to "neopets.com" expires - It checks if the cookie is expired. If so, it deletes it and does not send it. path - It checks if the path is valid relative to the domain. If a cookie passes all three of these, then it is sent to the website via headers like so: Quote:
The last cookie doesnt need a semicolon and a space after it. After the cookies are sent, the website can then use them to its own desire. II. Setting Cookies Setting cookies is simple with PHP. PHP gives you a nice built in function to do it so you dont have to set the header using header(). The function is setcookie(). Here it is: Code:
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]]) Most of the time, you'll only use 3 of the parameters of setcookie. By looking at the parameters you should understand what each is after reading the above explanation on how a server sends a set-cookie header. Setcookie returns a boolean value TRUE if setting the cookie was successful or FALSE otherwise. REMEMBER! To set a cookie, you must NOT have sent headers yet. This means that you can not output any data before setting a cookie. THAT means that you can't "print" or "echo" anything. Here is some simple code to set a username in a cookie: PHP Code:
The above assumes that a value is set to $username with your username. Setting cookies is simple, much like reading them! On to the next section! III. Reading Cookies Again, PHP makes reading cookies very easy. When a user visits a PHP page, PHP will automatically parse all the cookies and put them into an easy-to-use autoglobal: $_COOKIE. Lets say that you set the cookie 'username' as you did in the above section. To read it you'd merely do this: PHP Code:
Of course, you can set a variable to it also. To check if a cookie EXISTS you can do this: PHP Code:
Of course, debugging while setting cookies can be a pain in the arse, so to easily view all cookies sent to your page you can use the print_r function: PHP Code:
I'd also like to point out that you can not delete a cookie, but can reset it. PHP Code:
Also, cookies must be sent before any output from your script (as you pointed out), you can get around this by adding ob_start() at the beginning of your script, and ob_end_flush(), even though it is probably not necessary. PHP Code:
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Cookies | evadtevol | Internet Explorer | 1 | 29-08-2007 12:11 AM |
| Cookies | evadtevol | Internet Explorer | 2 | 29-08-2007 12:06 AM |
| Cookies | Shadow Doc | Windows XP | 10 | 27-08-2007 11:39 PM |
| Cookies | Shadow Doc | Windows XP | 0 | 27-08-2007 11:32 PM |
| Cookies - Where did the go? | ADKR | Windows XP | 5 | 16-08-2007 05:33 PM |
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |