Newgrounds.com — Everything, By Everyone.

Checking login status…

USERNAME:

PASSWORD:

Logging in…

Logged in as:
.
Logging out…
Inbox My Account Log Out


Forum Topic: Php: Setting And Returning Cookies

(882 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

Jams44

Reply To Post Reply & Quote

Posted at: 1/17/06 11:32 PM

Jams44 LIGHT LEVEL 07

Sign-Up: 11/08/04

Posts: 714

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


None

DFox

Reply To Post Reply & Quote

Posted at: 1/18/06 12:54 AM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,216

Very cool tutorial!


None

Taylor

Reply To Post Reply & Quote

Posted at: 1/18/06 01:39 AM

Taylor LIGHT LEVEL 09

Sign-Up: 08/19/03

Posts: 8,527

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

Didn't I teach you this, Joe?


None

Jams44

Reply To Post Reply & Quote

Posted at: 1/18/06 08:00 AM

Jams44 LIGHT LEVEL 07

Sign-Up: 11/08/04

Posts: 714

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!


None

BlAcKjAcK

Reply To Post Reply & Quote

Posted at: 1/18/06 10:17 AM

BlAcKjAcK EVIL LEVEL 26

Sign-Up: 10/22/03

Posts: 2,631

Outstanding. I like this alot!


None

zabar

Reply To Post Reply & Quote

Posted at: 9/15/06 04:19 PM

zabar DARK LEVEL 04

Sign-Up: 08/30/06

Posts: 8

Very Helpfull


None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 9/15/06 08:42 PM

Pilot-Doofy LIGHT LEVEL 37

Sign-Up: 09/13/03

Posts: 12,297

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

holy jesus what are these goddamn animals


None

Zendra

Reply To Post Reply & Quote

Posted at: 9/16/06 02:30 AM

Zendra NEUTRAL LEVEL 37

Sign-Up: 09/07/03

Posts: 11,979

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.

NG Review & BBS Moderator // PM Review & BBS Abuse


All times are Eastern Daylight Time (GMT -4) | Current Time: 04:31 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!