PHP: Cookies
Posted: Tue Jul 15, 2014 1:30 pm
Hello everyone,
Another PHP tutorial on cookies this time. No I don't mean cookies you could eat unless you were a digital entity in a PC with...I'm going off topic. I mean the type of cookies that are stored on your PC when you visit websites that hold data.
A cookie can be used to identify a user, it's a file that is saved on your computer. When you visit a website, it's sends the cookie to the website as well. In this tutorial you will learn how to read and write cookies.
Cookies is a data type (Along with $_REQUEST, $_SERVER and $_GET, $_POST).
Retrieving cookies:
If you want to display all cookies use:
When you set your cookie you must set it before the <html> in your web page.
Use the setcookie function.
Why you should use setcookie before <html>:
Let's now set the cookie
1 week:
time()+604800
Deleting cookies:
It's kind of strange as to how we delete the cookies, but all we do is set the expiry time to a time that's already passed.
If I wasn't clear on something or I've missed something let me know, and of course if you liked the tutorial +rep
If you need any help or have any questions leave a reply to the thread.
Thanks!
Another PHP tutorial on cookies this time. No I don't mean cookies you could eat unless you were a digital entity in a PC with...I'm going off topic. I mean the type of cookies that are stored on your PC when you visit websites that hold data.
A cookie can be used to identify a user, it's a file that is saved on your computer. When you visit a website, it's sends the cookie to the website as well. In this tutorial you will learn how to read and write cookies.
Cookies is a data type (Along with $_REQUEST, $_SERVER and $_GET, $_POST).
Retrieving cookies:
Code: Select all
the "name" is the name of the cookie you are retrieving. This code won't work if the cookie doesn't actually exist. <?php
//Display the cookie in the browser
echo $_COOKIE["name"];
If you want to display all cookies use:
Code: Select all
Setting cookies:<?php
echo $_COOKIE;
When you set your cookie you must set it before the <html> in your web page.
Use the setcookie function.
Why you should use setcookie before <html>:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.The syntax of setcookie is: setcookie(name, value, expire, path, domain);
Let's now set the cookie
Code: Select all
Our cookie is named "theme" and it's value is "default" and will expire in 24 hours time. The 86400 is seconds, so we add 24 hours on the current time, and that is when our cookie will expire. <?php
setcookie("theme", "default", time()+86400);
?>
<html>
<!-- Now put everything else in here, like..your web page :P -->
1 week:
time()+604800
Deleting cookies:
It's kind of strange as to how we delete the cookies, but all we do is set the expiry time to a time that's already passed.
Code: Select all
3600 is 1 hour, we set our cookie 'theme' to expire 1 hour ago, and there, the cookie is gone. setcookie("theme", "", time()-3600);
If I wasn't clear on something or I've missed something let me know, and of course if you liked the tutorial +rep

Thanks!