GFXVoid.com Graphic Design Forums  

Go Back   GFXVoid.com Graphic Design Forums > Tutorials > Web Development / Coding Tutorials > PHP / MySQL Tutorials

Register Now for FREE!
Our records show you have not yet registered to our forums. To sign up for your FREE account INSTANTLY fill out the form below!

Username: Password: Confirm Password: E-Mail: Confirm E-Mail:
 
Image Verification
Please enter the six letters or digits that appear in the image opposite.

  I agree to forum rules 

» Online Users: 29
3 members and 26 guests
Abstrakshin, Delite, |Sky


» Stats
Members: 28,698
Threads: 54,526
Posts: 479,629
Welcome to our newest member, Akarui-hoshi
Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2005, 04:14 PM
soniclnd soniclnd is offline
Voidster
 
Join Date: Jan 2005
Posts: 279
soniclnd is a helpful member!
Thumbs down

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 ***
__________________
GFXVoid!
Reply With Quote
  #2  
Old 05-28-2006, 12:54 PM
Atolar Atolar is offline
Voidster
 
Join Date: May 2005
Posts: 275
Atolar is a helpful member!
Send a message via AIM to Atolar
Default

Nice tut, very useful for beginers.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
.introduction .burgy Introductions 6 08-03-2005 11:57 AM
Re-Introduction. Maverick Introductions 7 06-25-2005 01:18 AM
Simple Cookies demo PHP / MySQL Tutorials 2 05-14-2005 01:28 AM
Re-Introduction Jakska Introductions 4 03-13-2005 08:48 PM
Another Introduction Kimberly Introductions 7 01-07-2005 12:10 AM

Powered by vBadvanced CMPS v3.2.0

All times are GMT -5. The time now is 02:06 AM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
HTML Help provided by HTML Help Central