FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Get previous login username

 


kv
I have a login page. I want to retain the previous logged in user name on that computer so that when the user visits next time, he need not type user name again. I know this needs cookies, but can somebody tell me how to do this in php?
Ducksteina
Code:
<?PHP
setcookie("TestCookie", "Username"); // sets the cookie TestCookie with the value Username
echo $_COOKIE["TestCookie"]; // outputs the cookie TestCookie
Stubru Freak
It's worth noting that cookies have to be set before any output.
mathiaus
depending on how secure you need your pages you may require to set a cookie for the password (encrypted!) and on every page load query the database to check otherwise they can just change the username and their logged in as someone else.

Another way not as secure but it stops the user doing this is to encrypt the username somehow and set a second cookie for this. On every pageload check that the encrypted cookie name is the same as the normal name when you encrypt that as well. If not warn them! Twisted Evil
kv
Ducksteina wrote:
Code:
<?PHP
setcookie("TestCookie", "Username"); // sets the cookie TestCookie with the value Username
echo $_COOKIE["TestCookie"]; // outputs the cookie TestCookie


Somehow this didn't work for me. I put the echo $_COOKIE["TestCookie"]; in the next page accessed as said in the manual. I also checked the browser's cookies. It is not setting any cookies. Cookies are enabled in the browser. Is there any other setting (like php.ini) which needs to be set?
mathiaus
you need to set a time in seconds
Code:
setcookie("Username", $username, time()+36000); //10 hours
kv
Yes. That was the problem. It worked. Thanks.
This topic is locked: you cannot edit posts or make replies.    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.