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 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    login code not working properly

    I had my login working fine until I tried to add a remember me...now the page is loading my incorrect login page even when i enter a correct user/login...here is my code:

    Login.php:
    Code:
    <?php
    
    include 'functions.php';
    
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    $rememberme = $_POST['rememberme'];
    
    if (loggedin())
    {
       header("Location: members.php");
       exit();
    
    }
    
    
    if ($username&&$password)
    {
    
     $login = mysql_query("SELECT * FROM users WHERE username='$username'");
     $numrows = mysql_num_rows($login);
     
    if ($numrows!=0)
    {
    
     while ($row = mysql_fetch_assoc($login))
     {
           $dbpassword = $row['password'];
           $dbusername = $row['username'];
           if ($username==$dbusername&&$password==$dbpassword)
           {
              $loginok = TRUE;
              header("Location: members.php");
              exit();
    
      if ($loginok==TRUE)
      {
         if ($rememberme=="on")
            setcookie("username", $username, time()+7200);
         else if ($rememberme=="")
              $_SESSION['username']=$username;
              
              header("Location: members.php");
              exit();
      }
           }
         else
         $loginok = FALSE;
         header("Location: incorrect.html");
    
     }
     }
         else
        header("Location: incorrect.html");
    }
         else
        header("Location: incorrect.html");
    
    
    ?>

    functions.php:
    Code:
    <?php
    
    session_start();
    
    mysql_connect("localhost","root","*****") or die ("Couldn't connect!");
    mysql_select_db("phplogin") or die ("Couldn't find db");
    
    function loggedin()
    {
    if (isset($_SESSION['username'])||isset($_COOKIE['username']))
    
    {
       $loggedin = TRUE;
       return $loggedin;
    
    }
    
    }
    ?>

    members.php:
    Code:
    	<?php
    	session_start();
            include 'functions.php';
    
            if (!loggedin())
            {
             header("Location: login.php");
             exit();
    
            if ($_SESSION['username'])
            {
            echo "HELLO, ".$_SESSION['username']."!";
            }
            else header ("Location: index.html");
            }
            ?>

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    At a glance looks like the first

    PHP Code:
    header("Location: members.php");
     exit(); 
    happens before the login session has been set. Try removing this and only have it inside the if ($loginok==TRUE)
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

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