Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
  1. #1
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    On a website I use I require members to login. The basis of the site means they are not there for long.

    However, I would prefer to remember their details by cookie rather by session as this requires a login everytime they visit.

    Currently my login code reads..
    PHP Code:
    if ($_POST['submit'] == "Login") {
    if(
    $_POST['user_name'] || $_POST['pass_word'])
    {
       if(!
    get_magic_quotes_gpc())
       {
       
    $user_name addslashes($_POST['user_name']);
       
    $pass_word addslashes($_POST['pass_word']);
       }else{
       
    $user_name $_POST['user_name'];
       
    $pass_word $_POST['pass_word'];
       }
    include (
    "includes/conn.php"); 
    $sql="SELECT * FROM users WHERE user_name='".$user_name."' AND pass_word='".md5($pass_word)."'";
     
    $res=mysql_query($sql) or die("Error: ".mysql_error());
     if(
    mysql_num_rows($res)>0)
     {
    $_SESSION['name'] = $user_name;
     }
    }

    So, to change this over to cookies, what steps do I need to take? I presume I still need the session code for those who block cookies?

    Any help is welcome.

    Cheers

  2.  

  3. #2
    Senior Member Fallout's Avatar
    Join Date
    Aug 2003
    Location
    Richmond, Virginia
    Posts
    543
    Member #
    2748
    Just use setcookie() (http://us3.php.net/setcookie) everywhere where you would set your session code.

    Then on your other pages do a check against $_COOKIE['var name'] for the correct value.

  4. #3
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    But if cookies are not allowed..?

  5. #4
    Senior Member Fallout's Avatar
    Join Date
    Aug 2003
    Location
    Richmond, Virginia
    Posts
    543
    Member #
    2748
    Then it just falls back on your existing session code?

  6. #5
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Hm, so I keep that code in..?

  7. #6
    Senior Member Fallout's Avatar
    Join Date
    Aug 2003
    Location
    Richmond, Virginia
    Posts
    543
    Member #
    2748
    On the first page they visit, set a session variable from the cookie as if they had logged in. Then you keep the rest of your code the same.

  8. #7
    Senior Member Stylise's Avatar
    Join Date
    Jul 2005
    Location
    Mount Martha, Australia
    Posts
    229
    Member #
    10679
    You should set a cookie when a user logs in, and then unset it when they log out.

    Then you can use the cookie to log them in the next time the visit, ie.

    if cookie exists
    ... get cookie value
    ... log user in
    else
    ... log user in
    ... set cookie

  9. #8
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    I follow that but the part I am unsure of is what if they do not allow cookies? Then I still need the session code or...?

  10. #9
    Senior Member Fallout's Avatar
    Join Date
    Aug 2003
    Location
    Richmond, Virginia
    Posts
    543
    Member #
    2748
    if cookie | session exists
    ... get cookie value
    ... set session value
    ... log user in
    else
    ... log user in
    ... set cookie
    ... set session value

    This way if they dont have cookies, the cookie parts fail but a session value is still set for the rest of the current visit.

  11. #10
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Cheers.. we got there in the end! (I have not implemented it but follow your pseudo's)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:47 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com