Login Cookies???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lip9000
Forum Newbie
Posts: 11
Joined: Tue Jul 04, 2006 3:38 am

Login Cookies???

Post by lip9000 »

I have setup a website and in the login page, currently users just login, and when they exit the browser, and then open it back up, they have to log in again.

How do I set it up so that when users log in, it creates a cookie on the computer remembering that they are logged in, so that when they close the browser and reopen it, they are still logged in?

Also how do I set it so that the cookie expires in 1 hour?

Thanks heaps.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

setcookie('whatever', 'whatever', time()+60*60);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

setcookie("User_Login", $User, time()+3600); // Current Time + 3600 Seconds = 1 hr

edit: Dang you...


-NSF
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

NonStopableForce wrote:

Code: Select all

setcookie("User_Login", $User, time()+3600); // Current Time + 3600 Seconds = 1 hr

edit: Dang you...


-NSF
hehe it's kinda cool. the "dang you" response because i helped someone a little bit quicker. shows a good spirit in the helping community! keep up the good work =]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
lip9000
Forum Newbie
Posts: 11
Joined: Tue Jul 04, 2006 3:38 am

Post by lip9000 »

thanks! yeh i am new here sorry. ok so that goes in the login, and i should put in the users username and password variables into that cookie right??

then next how do i check if that cookie exists next time they come to the site??
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Do not put the users password in the cookie! Setting cookies isn't necessarily secure.

In the scheme I've got set up - after the user logs in, they're given a session id. That session id along with their username is what's stored in a cookie. Then on every page load, I check that session id & see if its active & in the database.

To check if the cookie exists, just reference the $_COOKIE array:

Code: Select all

if(isset($_COOKIE['whatever I named my cookie']))
...
Though you should do more than just check if that cookie exists - you should also verify it's contents.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply