00:00
00:00
Newgrounds Background Image Theme

Vaittus just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Php: Setting And Returning Cookies

2,782 Views | 4 Replies
New Topic Respond to this Topic

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

Response to Php: Setting And Returning Cookies 2006-01-18 00:54:18


Very cool tutorial!

Response to Php: Setting And Returning Cookies 2006-01-18 08:00:08


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!

Response to Php: Setting And Returning Cookies 2006-09-15 16:19:39


Very Helpfull

Response to Php: Setting And Returning Cookies 2006-09-15 20:42:09


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


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.