Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Login with Remember Me feature not working

    Can somebody help me? I want this login script to work like this:

    1. Checks to see if user and pass inputs are not empty
    2. Checks to see if $_POST['remember'] is checked, if so, set 2 cookies: $_COOKIE['cookname'] and $_COOKIE['cookpass'].
    3. Checks to see if $_COOKIE's are set, if so, $username1 and $password1 == those cookies, else, they equal the user's posted username and password.
    4. Check to see if there's a row in the db where $username and $password are equal.
    5. If not, redirect to login page.

    Here's my code:

    Code:
    <?php
    define("lockout", true);
    
    error_reporting(E_ALL ^ E_NOTICE);
    
    $ip=$_SERVER["REMOTE_ADDR"];
    $ipcheck1="SELECT * FROM `banned` WHERE ip='$ip' AND type='website'"; 
    
    if(mysql_num_rows(mysql_query($ipcheck1)) > 0) { 
    die("You have been banned from the website."); }
    
    if(!isset($_SESSION['db_logged']) || $_SESSION['db_logged'] !== true) {
    
    session_start(); 
    session_set_cookie_params($loginlength);
    
    if (isset($_POST['username']) && isset($_POST['password'])) {
    
       if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])) {
    
       $username = $_COOKIE['cookname'];
       $password = $_COOKIE['cookpass'];
    
       }
    
       else {
    
       $username = catch(username);
       $password = catch(password);
    
       }
    
    
       $sql = "SELECT userid, username, password, position, dispname 
               FROM tbl_auth_user
               WHERE username = '$username' 
                     AND password = PASSWORD('$password')";       
    
       $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
    
       while($r = mysql_fetch_array($result)) {
       $userid1 = $r['userid'];
       $username1 = $r['username'];
       $position1 = $r['position'];
       $dispname1 = $r['dispname'];
       $password1 = $r['password'];
       }
    
    
      if (mysql_num_rows($result) == 1 && $position1 == 1) {
    
     if(isset($_POST['remember'])){
          setcookie("cookname", $_POST['username'], time()+60*60*24*100, "/");
          setcookie("cookpass", $_POST['password'], time()+60*60*24*100, "/");
          $_SESSION['db_logged'] = true;
       }
    
          $_SESSION['db_logged'] = true;
          $_SESSION['userid'] = $userid1;
          $_SESSION['position'] = $position1;
          $_SESSION['username1'] = (isset($_COOKIE['cookname']) ? $_COOKIE['cookname'] : $username1);
          $_SESSION['password'] = (isset($_COOKIE['cookpass']) ? $_COOKIE['cookpass'] : $password1);
          $_SESSION['dispname'] = $dispname1;
    
    
          header("Location: $_SERVER[HTTP_REFERER]");
          
          $error = '';
          exit;
       } 
    
    
    elseif(mysql_num_rows($result) == 1 && $position1 == 2) {
    
     if(isset($_POST['remember'])){
          setcookie("cookname", $_POST['username'], time()+60*60*24*100, "/");
          setcookie("cookpass", $_POST['password'], time()+60*60*24*100, "/");
          $_SESSION['db_logged'] = true;
       }
    
          $_SESSION['db_logged'] = true;
          $_SESSION['userid'] = $userid1;
          $_SESSION['position'] = $position1;
          $_SESSION['username1'] = (isset($_COOKIE['cookname']) ? $_COOKIE['cookname'] : $username1);
          $_SESSION['password'] = (isset($_COOKIE['cookpass']) ? $_COOKIE['cookpass'] : $password1);
          $_SESSION['dispname'] = $dispname1;
    
    
    
          header("Location: $_SERVER[HTTP_REFERER]");
          
          $error = '';
          exit;
       } 
    
    
    else
    
    {
    $error = '<font color="#CC0000">Wrong Username Or Password</font>';
       }
    mysql_close();
    } elseif ((isset($_SESSION['db_logged']) || $_SESSION['db_logged'] == true) && $position1 == 1) { header("Location: index.php?page=admin&userid=$userid1"); } 
    elseif ((isset($_SESSION['db_logged']) || $_SESSION['db_logged'] == true) && $position1 == 2) { header("Location: index.php?page=admin&userid=$userid1"); } 
    
    } else {
    
    header("Location: $_SERVER[HTTP_REFERER]");
    
    }
    
    ?>
    Any help would be greatly appreciated. Login is based on member position (or ranking) too. 1 = admin, 2 = member. Thanks.

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    try making the cookie one cookie for one by compiling the data..

    PHP Code:

    $cookie_data 
    $name "|"$pass;
    $cookie_expiration_time time()+60*60*24*100;
    setcookie("userinfo"$cookie_data$cookie_time); 

    PHP Code:

    if(isset($_COOKIE['userinfo'])) {
      
    $cookie_data explode("|"$_COOKIE['userinfo']);
      
    $username =  $cookie_data[0];
      
    $password =  $cookie_data[1];


  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Forget my script. It's not working at all. Can anyone point me in the direction of a good php login tutorial/script with databases and remember me features?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •