CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Post a PHP snippet (http://www.codingforums.com/forumdisplay.php?f=41)
-   -   Cookies (http://www.codingforums.com/showthread.php?t=73661)

Element 11-29-2005 11:55 PM

Cookies
 
This thread is intended to be a tutorial, or article if you will. It is not ment to be some sort of example requiring comments on scurity or whatever, it is part of PHP and people may seek to use it with whatever they want. I am mearly showing them how they can use cookies if they choose to. If they are looking for something more secure I'm sure they'll seek you out. :thumbsup:

If this is not the right forum for a such a post just delete it. It isn't much of a snippet.

To begin with. We will go over how to set a cookie.

PHP Code:


// Its good to use pseudo code to remember what your doing.
$cookie_name"hello_test"// This is the cookies name which will be used to call it back.
$cookie_data "Hello World!"// This is the content that will be put inside our cookie
$expiration_time time()+3600// Set the expiration time: +3600 seconds equals 1 hour on current time.
setcookie($cookie_name$cookie_data$expiration_time); // Now we'll set the cookie! 

When we use cookies, we already are assuming the visitor using this script has cookies enabled. Though some people arn't aware of requirments until it is too late. To get around this we'll make sure the cookie is set before we do anything else.

PHP Code:


// Add our variables for the cookie above.
if(!(setcookie($cookie_name$cookie_data$expiration_time))) { // If setcookie() is false, else continue.
  
die("You <b>must</b> have cookies enabled to continue running this program further!"); // Return error and exit program.


Next we'll take a look at calling the data.

PHP Code:


if(isset($_COOKIE['hello_test'])) { // Check to see if our cookie we made exists
  
echo $_COOKIE['hello_test']; // Should echo the cookie data.


That is the more simpler grasp of cookies. Another tip is if you want to have multiple information in cookies, compile it into one. For example:

PHP Code:


// Its good to use pseudo code to remember what your doing.
$cookie_name"hello_test"// This is the cookies name which will be used to call it back.
$var1 "Hello";
$var2 "World!";
$cookie_data $var1 "|" $var2// This is the content that will be put inside our cookie. Seperated by '|' for later use.
$expiration_time time()+3600// Set the expiration time: +3600 seconds equals 1 hour on current time.
if(!(setcookie($cookie_name$cookie_data$expiration_time))) { // If setcookie() is false, else continue.
  
die("You <b>must</b> have cookies enabled to continue running this program further!"); // Return error and exit program.


Too call this information and add it to seperate variables we'll use the fallowing example:

PHP Code:


if(isset($_COOKIE['hello_test'])) { // Make sure our cookie exists.
  
$cookie_info explode("|"$_COOKIE['hello_test']); // Explode the cookie by '|' into an array.
  
echo $cookie_info[0] . "&nbsp;" $cookie_info[1]; // $cookie_info[0] = "Hello" and $cookie_info[1] = "World!" with a result of "Hello World!"


Thats all I have time for right now. Be sure to see also setcookie() for more information and methods.

WA 11-30-2005 09:05 AM

This looks appropriate for this forum, and I'm sure many people will find it useful as well. :)

Element 11-30-2005 07:04 PM

Quote:

Originally Posted by WA
This looks appropriate for this forum, and I'm sure many people will find it useful as well. :)

Thank you, and I hope so. It just might if I don't get forum bashed with security nerds. :cool: Just gotta play it cool, bre. :) lol.


All times are GMT +1. The time now is 06:13 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.