Webmaster Forum

Go Back   Webmaster Forum > Webmaster Discussion Forums > Web Design and Graphics

Notices

Reply
 
LinkBack Thread Tools Display Modes
Old 05-17-2008, 03:46 PM   #1
New Member
 
Join Date: May 2008
Posts: 14
Credits: 1
Sarunas is on a distinguished road
Default Cookies

In this tutorial you will learn how to:
Make Cookies.
Use Cookies.
Check for of Cookies.
Delete Cookies.


Remember: You cannot put Any html code before the code of the cookies!

Firstly you must learn how to set a cookie.
The order of it is: setcookie(name, value, expire);.
example:
Code:
$time = time();
$text = "This is what the cookie will hold";
setcookie(Name, $text, $time+3600);
And that is how you set a cookie!

Now to call a cookie to a script:
You can use:
Code:
echo "Your Cookies Value: ".$_COOKIE[Name];
That will then output:
Quote:
Your Cookies Value: This is what the cookie will hold
You can also use it to check for values.
Code:
if (isset($_COOKIE[name])){
echo "You have a cookie!"; }
else {
echo "You have no cookies!"; }
That will either return TRUE, if you have a cookie or FALSE if you dont.
Now you know how to use cookies.

Now onto deleting cookies.
Its a good idea to check if there is a cookie first before deleting.
Example:
Code:
$time = time();
if (isset($_COOKIE[name])){
setcookie(Name, '', $time-3600);  }
else {
echo "You have no cookies to delete!"; }
It may look like we are making another cookie, but we are just adjusting to cookie to expire immediately!
You just you - to set the cookie back to expire.

Now we will make a test script using what you have just learned.

We open the script and then check if the form is posted.
if the post is "Submit" create a cookie, else if the post is "Reset" be delete it.
Code:
<?php
if ($_POST['Submit']){
setcookie(id,'kis works',time()+3600);
echo "Cookie Made!"; }
if ($_POST['Reset']){
if (isset($_COOKIE[id])){
setcookie(id,'',time()-3600);
echo "Deleted!"; }
else {
echo "You have no such cookie to delete!"; } }
Now we will make a form with two buttons.
Code:
echo "<form name='form1' method='post' action=''>
  <label></label>
  <label>
  <input type='submit' name='Submit' value='make'>
    <input type='submit' name='Reset' value='delete'>
  </label>
</form>
<br />";
Now we will display the cookie's value if it exists.
Code:
echo "Previous Attempts: ";
if (isset($_COOKIE[id])){
echo $_COOKIE[id]; }
else {
echo "None"; }
Now we close the script.
Code:
?>
You now know how to make, use and delete cookies!

The complete script:
Code:
<?php
/*
>> Basic Cookies Tutorial
*/
if ($_POST['Submit']){
setcookie(id,'kis works',time()+3600);
echo "Cookie Made!"; }
if ($_POST['Reset']){
if (isset($_COOKIE[id])){
setcookie(id,'',time()-3600);
echo "Deleted!"; }
else {
echo "You have no such cookie to delete!"; } }
echo "<form name='form1' method='post' action=''>
  <label></label>
  <label>
  <input type='submit' name='Submit' value='make'>
    <input type='submit' name='Reset' value='delete'>
  </label>
</form>
<br />";
echo "Previous Attempts: ";
if (isset($_COOKIE[id])){
echo $_COOKIE[id]; }
else {
echo "None"; } 
?>
?Sarunas
Sarunas is offline   Reply With Quote
Old 05-17-2008, 04:18 PM   #2
Counting 10,000 :D
 
Swastik's Avatar
 
Join Date: Mar 2008
Location: India
Posts: 494
Credits: 0
Swastik has a spectacular aura aboutSwastik has a spectacular aura about
Send a message via MSN to Swastik Send a message via Yahoo to Swastik
Default

Oh very nice! Good insight into cookies!

++ reps for you mate :D

- Swastik
Swastik is offline   Reply With Quote
Old 05-17-2008, 05:40 PM   #3
Contributing Member
 
Thanoz's Avatar
 
Join Date: Apr 2008
Location: Hellas, EU
Posts: 445
Credits: 10
Thanoz has a spectacular aura aboutThanoz has a spectacular aura about
Send a message via MSN to Thanoz Send a message via Skype™ to Thanoz
Default

Very good share but i have to admit i wished this was a recipe for cookied cause i was a bit hungry -.-

+rep!
__________________
I'm moving to another country :001_rolleyes: and i will be inactive for some time :sleep:
-|- A Webmaster's Blog Exclusive! -|- Check your IP now! -|- Free WordPress Themes -|- Basketball Forum
Thanoz is offline   Reply With Quote
Old 05-17-2008, 11:27 PM   #4
Pro Drop Kicker
 
ardit's Avatar
 
Join Date: Apr 2008
Location: Im 2-64 my home
Posts: 813
Credits: 0
ardit will become famous soon enough
Send a message via AIM to ardit Send a message via MSN to ardit
Default

i reped you up

I got happy when i saw the word cookie
ardit is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On



vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios