Be a Supporter!

Php: Setting And Returning Cookies

  • 1,661 Views
  • 6 Replies
New Topic Respond to this Topic
Jams44
Jams44
  • Member since: Nov. 8, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Php: Setting And Returning Cookies 2006-01-17 23:32:07 Reply

PHP: Setting and Returning Cookies

PHP: Main

In this tutorial, you will learn not only how to set a cookie but to use it to a persons advantage. The best way I can explain this is by using a simple login script which I will completely show at the end. Let's get started.

setcookie() would be the way to obviously set a cookie. There are many options when setting a cookie like: string name, string value[, int expire, string path, string domain.

The order to set these options are the same:

setcookie ( string name [, string value [, int expire [, string path [, string domain]]]] )

What do these options mean, well PHP.net gives us a more exact answer so I'll just cite them.

Name: The name of the cookie.
Example: 'cookiename' is called as $_COOKIE['cookiename']

Value: The value of the cookie. This value is stored on the clients computer; do not store sensitive information.
Example: Assuming the name is 'cookiename', this value is retrieved through $_COOKIE['cookiename']

Expire: The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime().
Example: time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes).

Path and Domain are really not important at this time, unles you are using multiple subdomains. So on to a real example. It's easy to explain, it's another thing to actually put these explanations to use. So now we will be using part of a login script to set our cookies.

Code:

if ( $username1 == $username && $password1 == $password) {
////If the Inputed Username matches the Real Username, same with password
setcookie("bloggedin", "bloggedin", time()+32090932093);
///Now as an action for the login, we will set the user with a cookie so they do not have to continuously sign in. We are calling the cookie "bloggedin" as well as the value. We want the expiration date to be a long ways away, so I put some crazy figure. After the person has logged in and the have a cookie, be sure to tell them.
echo 'Username and Password Correct and a Cookie has been Set!';
}
/////Like So!

Now that we have set the cookie, we would like to put it to use so now we will have the same person enter the login screen and come to find otu that they don't need to because they already have a cookie set.

Code:

if( isset($_COOKIE['bloggedin']) ) {
///If the cookie has been set they will get the next action
echo 'A Cookie Is Upon You!';
}
/// A good old message telling them they have a cookie

Now as long as it does take to explain, hopefully you have a better grip on how to set and return cookie, this would be considered a beginner lesson and can easily be used for more complicated things.

REMEMBER:

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.

This means that you must put
<?
ob_start()
?>
at the top of your page
and
<?
ob_end_flush()
?>
at the bottom so you dont get header warnings.

Thank you.

Written by Jams 44 for PHP: Main

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Setting And Returning Cookies 2006-01-18 00:54:18 Reply

Very cool tutorial!


BBS Signature
Taylor
Taylor
  • Member since: Aug. 19, 2003
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Php: Setting And Returning Cookies 2006-01-18 01:39:57 Reply

At 1/18/06 12:54 AM, GamesCool wrote: Very cool tutorial!

Didn't I teach you this, Joe?

Jams44
Jams44
  • Member since: Nov. 8, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Php: Setting And Returning Cookies 2006-01-18 08:00:08 Reply

At 1/18/06 01:39 AM, PillowBiter wrote:
At 1/18/06 12:54 AM, GamesCool wrote: Very cool tutorial!
Didn't I teach you this, Joe?

Maybe a long time ago!

zabar
zabar
  • Member since: Aug. 30, 2006
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Php: Setting And Returning Cookies 2006-09-15 16:19:39 Reply

Very Helpfull

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Setting And Returning Cookies 2006-09-15 20:42:09 Reply

I wish you had covered on the path and domain optional parameters, because you overlooked them by large. They're very useful.

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Moderator
Level 51
Blank Slate
Response to Php: Setting And Returning Cookies 2006-09-16 02:30:31 Reply

At 9/15/06 08:42 PM, Pilot-Doofy wrote: I wish you had covered on the path and domain optional parameters, because you overlooked them by large. They're very useful.

Indeed. That would be useful.
Another I always notice: people are always combining it with a user system, seems everyone wants to make their own NG.