![]() | | ||||||||||
| |||||||||||
| |||||||
| Notices |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | |
| New Member ![]() Join Date: May 2008
Posts: 14
Credits: 1 ![]() | 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); Now to call a cookie to a script: You can use: Code: echo "Your Cookies Value: ".$_COOKIE[Name]; Quote:
Code: if (isset($_COOKIE[name])){
echo "You have a cookie!"; }
else {
echo "You have no cookies!"; }
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!"; }
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!"; } }
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 />";
Code: echo "Previous Attempts: ";
if (isset($_COOKIE[id])){
echo $_COOKIE[id]; }
else {
echo "None"; }
Code: ?> 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"; }
?>
| |
| | |
| | #2 |
| Counting 10,000 :D Join Date: Mar 2008 Location: India
Posts: 494
Credits: 0 ![]() ![]() | Oh very nice! Good insight into cookies! ++ reps for you mate :D - Swastik |
| | |
| | #3 |
| Contributing Member Join Date: Apr 2008 Location: Hellas, EU
Posts: 445
Credits: 10 ![]() ![]() | 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 |
| | |
| | #4 |
| Pro Drop Kicker Join Date: Apr 2008 Location: Im 2-64 my home
Posts: 813
Credits: 0 ![]() | i reped you up I got happy when i saw the word cookie
__________________ All My Sites GTA Gamers | Cool Directory | Football Forums Hot Electronics | Get-Firefox | Make Easy Money I Hate Stupid SiteTalkZone The Ardit |
| | |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |