469,290 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

When I click on ‘remember me’ and after sign out, when I try again to login cookies a

I have a login form in which i have a username, password and a remember me radio button option. Now I want it so that when I've signed out from home page and I try again to login then without asking for a username and password I get logged in because I am going to store the username and password values into cookie.

I wrote code on password_match.php page(action page of login page)
Expand|Select|Wrap|Line Numbers
  1. $con=mysql_connect("localhost","root","");
  2. if(!$con)
  3. {
  4. die('Could Not Connect:'.mysql_error());
  5.  
  6. mysql_select_db("tcs",$con);
  7.  
  8. if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass']))
  9. {
  10. $usr = $_COOKIE['cookname'];
  11. $pwd = $_COOKIE['cookpass'];
  12. $pwd=hash('sha1',$_POST['password']);
  13. }
  14.  
  15. else
  16. {
  17. $usr=$_POST["username"];                 
  18. $pwd=hash('sha1',$_POST['password']);           
  19. }
  20. </code>
  21. Through this code I am checking is there any cookie with cookname and cookpassword which contain username and password. If no cookie is found with this name then retrieve username and password from textboxes directly, if the user enters them.
  22.  
  23. Now the code which I am going to write will set a cookie if username and password exists:
  24. <code>
  25. if ($result) { $row=mysql_fetch_array($result);
  26.  
  27. if (($row["Username"]==$usr) && ($row["Password"]==$pwd))
  28. {
  29.     if(isset($_POST['remember']))
  30.     {
  31.     setcookie("cookname", $row['Username'], time()+60*60*24*100, "/");
  32.     setcookie("cookpass", $row['Password'], time()+60*60*24*100, "/");
  33.     }
  34.  
  35.  
  36. $_SESSION['employee']['id']=$row['User Id'];
  37.     $_SESSION['employee']['username']=$row['Username'];
  38.  
  39.     header ("Location: home_page.php");
  40.  
  41. }
  42.  
Now when starting, when I enter username and password and if these are right show a cookie must have been set and if now I am signed out and try the same sign in form, as according to me it should retrive cookie but nothing happining here. I am not able to implement it. Where am I going wrong?
Feb 1 '10 #1
4 1852
'Remember me' and 'Stay logged in' are two different things. when you log off the session is killed. Now, if you want to get automatically logged in you may have to store the session_id as well in the cookie and process it later in the program for this purpose.
Feb 2 '10 #2
kovik
1,044 Expert 1GB
@shabinesh: No and no. When you manually log off, the session is no longer authenticated as the user, but it is by no means destroyed. Even guest users have sessions. Also, you wouldn't just save the session ID in a cookie and reuse it. Firstly, sessions expire when they are inactive for a certain period of time. Secondly, this allows a user that finds this cookie to save that value in their own cookie and always access this account.

Anyway, refer to my response in another thread for how to accomplish this:
http://bytes.com/topic/php/answers/8...on#post3541792
Feb 2 '10 #3
But in this thread solution is not given in this thread it is told that u should add an extra column in ur database to store login status.I dont know what should be stored there and how i can retrive this state at login page..Can i use all password matching php coading on login page also...plz explain little bit more for those guies who need some more explaiantion in form of code to grab concept...
Feb 2 '10 #4
kovik
1,044 Expert 1GB
The answer's given. I won't go more in-depth than that. I have given you the theory. The implementation is up to you.
Feb 2 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by Joshua Beall | last post: by
2 posts views Thread by Shakun | last post: by
1 post views Thread by Dabbler | last post: by
7 posts views Thread by Kesavan | last post: by
4 posts views Thread by Jon Slaughter | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.