Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-29-2005, 11:55 PM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.

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.

Last edited by Element; 11-30-2005 at 07:08 PM..
Element is offline   Reply With Quote
Old 11-30-2005, 09:05 AM   PM User | #2
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
This looks appropriate for this forum, and I'm sure many people will find it useful as well.
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
WA is offline   Reply With Quote
Old 11-30-2005, 07:04 PM   PM User | #3
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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. Just gotta play it cool, bre. lol.
Element is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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


All times are GMT +1. The time now is 02:20 PM.


Advertisement
Log in to turn off these ads.