error in login script

Discussion in 'PHP' started by nrodes, Nov 7, 2008.

  1. #1
    i have a very simple login script. There are still some errors that I cant figure out:

    this is my code

    <?php
    session_start();
    ?>
    <html>
    <head>
    <title>
    Logging In
    </title><script type="text/javascript">
    <!--
    function delayer(){
    window.location = "main.php"
    }
    //-->
    </script>
    </head>
    <body onLoad="setTimeout('delayer()', 5000)" bgcolor="blue"><center><font size="36"><b><br>
    Logging in, please wait. If your browser does not auto matically redirect you, <a href="main.php">click here</a>.
    </font></center>
    </body>
    </html>
    
    <?php
    //get input data
    $user=$_POST["username"];
    $pass=$_POST["password"];
    
    //encrypt username and pass
    $cuser=md5($user);
    $cpass=md5($pass);
    
    //make variables
    $ip = $_SERVER['REMOTE_ADDR'];
    $userplusip = $user . $ip;
    
    //include login file
    include($user . ".php");
    
    //make sure info matches and starts session
    if ($cuser==$realuser && $cpass==$realpass)
    {
    $_SESSION["auth_username"] = $user;
    $_SESSION["userplusip"] = $userplusip;
    }
    
    ?>
    PHP:
    how can i fix the errors?

    thanks in advance.
     
    nrodes, Nov 7, 2008 IP
  2. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #2
    well for starters you have it set so it looks for a to go to after logged in named after the user. Try to make a page like members.php and send it there!
     
    ryandanielt, Nov 7, 2008 IP
  3. nrodes

    nrodes Peon

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    as you can see, i have JavaScript in the beginning thats suppose to forward to main.php whether tho logins correct or not. the idea is that main.php will make sure there is a session and all that stuff.
    Im getting errors on the validate script then main.php wont recognize the session.

    Heres main.php


    <?php
    
    session_start();
    
    //gets ip address
    $ip = $_SERVER['REMOTE_ADDR'];
    
    //checks for active session
    if(isset($_SESSION["auth_username"])&&isset($_SESSION["userplusip"]))
       {
       $user = $_SESSION["auth_username"];
       $userplusip = $user . $ip;
       if ($userplusip==$_SESSION["userplusip"])
          {
          $file = "users/" . $user . ".php";
          include($file);
          }
       else
          {
          header ( 'Location: index.htm' );
          exit;
          }
       }
    else
       {
       header ( 'Location: index.htm' );
       exit;
       }
    
    ?>
    PHP:
     
    nrodes, Nov 7, 2008 IP
  4. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #4
    The way you are doing it is not simple at all, what I suggest is to not use any javascript or anything like that and just a simple meta refresh where it will redirect the member to main.php. Using javascript when it comes to logins and sessions gets really tricky.

    This way should resolve all problems you are having.
     
    ryandanielt, Nov 7, 2008 IP
  5. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    the problem is that your PHP code is always going to execute before your JavaScript onload code. So the script is trying to include a different page before your JavaScript gets to execute. The problem is this line:

    
    include($user . ".php");
    
    PHP:
    This bdougan.php page doesn't exist, and it's trying to include it in this page, which is only going to redirect to main.php after 5 seconds anyway. Why not just remove that "include" line completely, or just redirect the whole page to main.php to begin with, like this:

    
    header("location: main.php"); exit(0);
    
    PHP:
     
    dev_SeeInside, Nov 7, 2008 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Barti1987, Nov 7, 2008 IP