Jump to content
xisto Community
Sign in to follow this  
iGuest

How To Use Cookie In Your Web Site ? this semple code to use and get cookie

Recommended Posts

what is the cookie ?
the cookis it is some info sent and save in user computer

whare i can use the cookies?
becouse the cookies it like the header you can not send it after any output
wes sent so you must send the cookies before any output like as
<html > ,echo and any other code

i well make an E.X. to use the cookies
you must have 2 file
index.php
update.php
----------
in the index.php add this code

<?     // This section must go at the top of the page that will display     // the users favorites.  These are the 'default' URLs that the user     // will see if they have not edited any.     if (strlen ($favorite[1]) < 3) setcookie("favorite[1]",  "robscripts.com;, time()+30000);     if (strlen ($favorite[2]) < 3) setcookie("favorite[2]",  "digitaledge.com;, time()+30000);     if (strlen ($favorite[3]) < 3) setcookie("favorite[3]",  "penpalseek.com;, time()+30000);     if (strlen ($favorite[4]) < 3) setcookie("favorite[4]",  "budgetideas.com;, time()+30000);     if (strlen ($favorite[5]) < 3) setcookie("favorite[5]",  "cardfan.com;, time()+30000); ?> <html> <head> <title>Favorites</title> </head> <body> Current Favorites: <p> <!-- Start favorites code: This can be put on any php page, anywhere. --> <?   for ($x = 1; $x <= 5; print "$x: <a href='http://$favorite[$x]' target='_BLANK'>$favorite[$x]</a><br>", $x++); ?> <BR><BR> <a href='update.php'>Update Favorites</a>. <!-- End favorites code --> </body> </html>
in the update.php add this code
<? // Location of the page where the users links are displayed. // Example: [url]http://http://ww38.yoursite.com/index.php/; $loc = "http://forums.xisto.com/no_longer_exists/;; // How long do you want the users cookies to save their // favorite links? In seconds. Day=86400 Week=604800 // month=2419200 year=29030500 $exp = 29030500;   if ($state == "update") {      setcookie("favorite[1]",  $fav1, time()+$exp);      setcookie("favorite[2]",  $fav2, time()+$exp);      setcookie("favorite[3]",  $fav3, time()+$exp);      setcookie("favorite[4]",  $fav4, time()+$exp);      setcookie("favorite[5]",  $fav5, time()+$exp);      $state = "done";      header("Location: $loc");   } else {     if (strlen ($favorite[1]) < 3) setcookie("favorite[1]",  "robscripts.com;, time()+300000);     if (strlen ($favorite[2]) < 3) setcookie("favorite[2]",  "digitaledge.com;, time()+30000);     if (strlen ($favorite[3]) < 3) setcookie("favorite[3]",  "penpalseek.com;, time()+300000);     if (strlen ($favorite[4]) < 3) setcookie("favorite[4]",  "budgetideas.com;, time()+300000);     if (strlen ($favorite[5]) < 3) setcookie("favorite[5]",  "cardfan.com;, time()+300000);   } ?> <html> <head> <title>Favorites</title> </head> <body> Update Favorites: <form method="POST" action="update.php"> <?   for ($x = 1; $x <= 5; print "$x: <input type='text' name='fav$x' size='29' value='$favorite[$x]'><br>", $x++); ?> <input type="hidden" value="update" name="state"> <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> </body> </html>
now run the index.php in your loclhost
and test the cookies have fun

Share this post


Link to post
Share on other sites

All the arguments except the name argument are optional. You may also replace an argument with an empty string ("") in order to skip that argumentNote: As of PHP 4, you can use output buffering to send output prior to the call of this function, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.