PDA

View Full Version : [PHP/MySQL] How does the computer "remember" if you are logged in or not?



goldy2006
November 24th, 2004, 08:58 PM
I wanted to create a login and a user database in php and mysql.
I successfully created a php page which can add a username and password to the "users" table in a my_db database and then recall it and every other screenname on the next page.

But I don't understand how the computer knows if you are "logged in". :m:
For example, I do not have to log in every time I click on a link to a post here at kirupaforum.com; however, when I first arrive at the site I do have to type in my password and username.
Why Is That? :h:
In other words, how does the computer remember that i'm logged in?

Thanks,
Matt

rysolag
November 24th, 2004, 09:10 PM
cookies. When the user logs in a cookie is set on the user's computer. Then whenever they come back to the site your script sees the cookie and thus knows to auto-login the user. There should probably be two cookies: the user id and an md5() of their password. You then validate these two values against the database.

ironikart
November 24th, 2004, 09:29 PM
Or session variables (server side cookies). These still work if the user has cookies turned off in their browser. I think there are limits to how many sessions you can run on a server at once, but I haven't found the limit yet.

goldy2006
November 24th, 2004, 09:44 PM
First of all, I'm not asking how the computer remembers your password after you close the browser (I already knew of cookies).

I was trying to find out how the browser (or server) "remembers" you're logged in while going from page to page.

So how do you create a session variable, and can a session variable remember you're logged in while you're visiting the site (and forget you're logged after you close your browser).

RvGaTe
November 26th, 2004, 11:05 AM
you start a session with:


session_start();


then, when you want something to be saved inside the session (so that you can use the variables on other pages) use:



$_SESSION['yourVarname'] = "some value";
$_SESSION['yourOtherVarname'] = "some value";
$_SESSION['yourArray'] = new Array("some value", "w00t");


you can also store arrays, or other variables inside your session...
to close a session, use:



session_destroy();

or you just close the broweser, that automaticly stops the session...