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?
Get previous login username
| Code: |
| <?PHP
setcookie("TestCookie", "Username"); // sets the cookie TestCookie with the value Username echo $_COOKIE["TestCookie"]; // outputs the cookie TestCookie |
It's worth noting that cookies have to be set before any output.
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!
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!
| Ducksteina wrote: | ||
|
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?
you need to set a time in seconds
| Code: |
| setcookie("Username", $username, time()+36000); //10 hours |
Yes. That was the problem. It worked. Thanks.
