0 members and 15,994 guests
No Members online

» Site Navigation
» Stats
Members: 35,442
Threads: 103,075
Posts: 826,688
Top Poster: cc.RadillacVIII (7,429)
|
-
In this tutorial I will introduce PHP cookies to you and provide a real-life example; You should have some knowledge of PHP...
__________________________________________________ ___________________
What are cookies?
Cookies are bits of information that Websites leave on your computer so they can later recognize you or remember your preferences.
Cookies also logg you in and out of your email account and websites. I use cookies on my site to remember the color scheme you chose.
What will we do in this tutorial?: We'll go over how to set cookies and use cookie information with PHP I will also provide an example and a downloadable file so you can take a look at the PHP code yourself...
__________________________________________________ ___________________
PHP cookies
Cookies in PHP are set using the setcookie() function and are retrieved using the global variable $_COOKIE["cookiename"]. Cookies in PHP must be sent with the headers, this means that no output must be sent to the browser(not even whitespace); This can be gotten around using output buffering, but I won't go into that...
To unset cookies you call setcookie() with the same values as the cookie was set with, but for the value you set FALSE
setcookie() needs four basic things:
> string name - The name of the cookie
> string value - The value stored on the user's computer
> int expire - When the cookie expires(this is a Unix timestamp so it will be better to use time() to define it)
> string path - Then path in the server where the cookie will be avaliable ("/" for the whole server, "/tree/" for the "tree" directory... etc)
Setcookie can also be passed two more values: domain(which defines the domains in which the cookie will be avaliable[for all subdomains of example.com use ".example.com" for the "hello" subdomain use "hello.example.com"]).... and secure(if set to TRUE the cookie will only be set over a https connection)
Let's look at a basic example that sets a cookie named "name" with a value "joe" that will expire in 3 years and is avaliable to the whole domain:
Code:
<?php
//------------ set the cookie
setcookie("name", "joe", time()+60*60*24*366*3, "/");
?>
<html>
<body>
The cookie has been set
</body>
</html>
Notice the 60*60*24*366*3 part, this is because time() is in seconds, so you basicly get 60 seconds times 60 which makes an hour, times 24 which makes a day, times 366 which makes a year, times 3, which makes 3 years.... You can also see how setcookie was called before <html> and all the output sent to the browser...
So, How do we use this?: Cookie information can't be used in the same page they were set in... so in a nother page you could have:
Code:
<?php
//------------ get the cookie "name" into the variable $usern
$usern = $_COOKIE["name"];
?>
<html>
<body>
<?php echo "Hello " . $usern . ", How's it going?"; ?>
</body>
</html>
Let's delete the cookie now:
Code:
<?php
//------------ unset the cookie
setcookie("name", false, time()+60*60*24*366*3, "/");
?>
<html>
<body>
The cookie has been unset
</body>
</html>
As I mentioned earlier, to unset a cookie you call setcookie() with the same values as the cookie was set, but for value you use false
__________________________________________________ ___________________
Example
The example file is called ephpcookies.php it just asks you for a name and then sets the cookie, it also unsets the cookie when the user chooses to, here is the content of the file:
Code:
line #1 * <?php
line #2 * //define variables
line #3 * $setnew = $_GET["setnew"];
line #4 * $readcookie = $_GET["readcookie"];
line #5 * $unsetcookie = $_GET["unsetcookie"];
line #6 * $value = $_GET["value"];
line #7 * $cookiename = "namec";
line #8 *
line #9 * if (!isset($_COOKIE[$cookiename]) && $setnew == "yes" && isset($value)){
line #10 *
line #11 *
line #12 * setcookie($cookiename, $value, time()+60*60*24*366*5, "/");
line #13 * ?>
line #14 * <html>
line #15 * <body>
line #16 *
line #17 * Your cookie was not set but it is set now<br />
line #18 *
line #19 * <a href="ephpcookies.php?readcookie=yes">read cookie</a><br />
line #20 * <a href="ephpcookies.php?unsetcookie=yes">unset cookie</a><br />
line #21 *
line #22 * </body>
line #23 * </html>
line #24 * <?php
line #25 *
line #26 *
line #27 * }
line #28 * elseif (isset($_COOKIE[$cookiename]) && $readcookie=="yes"){
line #29 * $usern = $_COOKIE[$cookiename];
line #30 * echo"<html>
line #31 * <body>";
line #32 * echo "hello " . $usern . ", cookie was set succesfuly...";
line #33 * echo "<a href=\"ephpcookies.php?unsetcookie=yes\">unset cookie</a><br />
line #34 * </body>
line #35 * </html>";
line #36 *
line #37 * }
line #38 *
line #39 * elseif(isset($_COOKIE[$cookiename]) && $unsetcookie == "yes"){
line #40 * setcookie($cookiename, false, time()+60*60*24*366*5, "/");
line #41 * ?>
line #42 * <html>
line #43 * <body>
line #44 *
line #45 * Your cookie was set but it is unset now<br />
line #46 *
line #47 * <a href="ephpcookies.php">return to set cookie</a><br />
line #48 *
line #49 * </body>
line #50 * </html>
line #51 * <?php
line #52 * }
line #53 *
line #54 * elseif(!isset($_COOKIE[$cookiename])){
line #55 * ?>
line #56 * <html>
line #57 * <body>
line #58 *
line #59 * Your cookie is unset <br />
line #60 * <form action="ephpcookies.php" method="get">
line #61 * <input type="hidden" name="setnew" value="yes" />
line #62 * <input type="text" name="value" />
line #63 * <input type="submit" name="send" value="set cookie" />
line #64 * </form>
line #65 *
line #66 * </body>
line #67 * </html>
line #68 * <?php
line #69 * }
line #70 *
line #71 * ?>
you can download the file here...
__________________________________________________ ___________________
Well... I hope this all kinda made sense to you don't hesitate to contact me if you have problems.....
*** original: http://zonik.lazyminds.com/tutorials...phpcookies.htm ***
Similar Threads
-
By .burgy in forum Introductions
Replies: 6
Last Post: 08-03-2005, 11:57 AM
-
By Maverick in forum Introductions
Replies: 7
Last Post: 06-25-2005, 01:18 AM
-
By demo in forum Other Tutorials
Replies: 2
Last Post: 05-14-2005, 01:28 AM
-
By Jakska in forum Introductions
Replies: 4
Last Post: 03-13-2005, 08:48 PM
-
By Kimberly in forum Introductions
Replies: 7
Last Post: 01-07-2005, 12:10 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|