Results 1 to 4 of 4

Thread: Login Script Issues

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Login Script Issues

    I created a login script that works fine, but for some reason the first time I try to login it simply shows the login page again. Then when I get to the control panel and click on a link it once again returns me to the login page. On the 3rd login it works fine. Here is the login page script:
    PHP Code:
    <?php

    $ip 
    $_SERVER['REMOTE_ADDR'];

    if (
    $_POST['login']) {
        
    $user $_POST['user'];
        
    $pass md5($_POST['password']);
        
            
    $chk_user mysql_query("SELECT * FROM `clients` WHERE username = '$user'") or die ('Error Getting User Data! <br />' .mysql_error());
            
    $u mysql_fetch_array($chk_user);
            
    $chk mysql_num_rows($chk_user);
            
                if (
    $chk 1) {
                    echo 
    '<meta http-equiv="refresh" content="2;URL=../login.php" />
                        The user <b>'
    .$user.'</b> does not exist!';
                } 
                elseif (
    $pass !== $u['password']) {
                    echo 
    '<meta http-equiv="refresh" content="2;URL=../login.php" />
                        Incorrect password'
    ;
                }
                elseif(
    $pass == $u['password']) {        
                
                    
    $user $_POST['user'];
                    
    $pass md5($_POST['password']);
                        @
    session_start();
                            
    $_SESSION['user'] = $user;
                            
    $_SESSION['pass'] = $pass;
                            
    $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
                        
                        echo 
    '<meta http-equiv="refresh" content="2;URL=/beta/client_cp" />
                            '
    .$user.', you are now logged in, redirecting you to the main page...';
                }
    }

    elseif (!
    $_POST['login']) {
        
    ?>
            <div id="login"><b>Please Log In</b>
    <!-- Login Form -->
            <form name="login" method="post" action="">
                <table width="230" align="center">
                    <tr>
                        <td><b>Username:</b></td>
                        <td><input type="text" name="user" class="input" /></td>
                    </tr>
                    <tr>
                        <td><b>Password:</b></td>
                        <td><input type="password" name="password" class="input" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right"><input type="submit" name="login" value="Login" /></td>
                    </tr>
                </table>
            </form>
    <!-- /Login Form -->
            </div>
        <?
    }

    ?>
    This is how I check if logged in:
    PHP Code:
    @session_start();
        if(empty(
    $_SESSION['user']) || empty($_SESSION['pass']) || $_SESSION['ip'] != $_SERVER['REMOTE_ADDR']) {
        
    header('Location: ../login.php');

    Is there any obvious reason for this issue? Thanks for the help
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try placing session_start() at the very beginning of the code (directly underneath the opening php tag). Also, when you do this, don't forget to take out the line @session_start().

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    Try placing session_start() at the very beginning of the code (directly underneath the opening php tag). Also, when you do this, don't forget to take out the line @session_start().

    Hope this helps.
    EDIT
    still having issues, see below.
    Last edited by Titan85; 04-01-2007 at 10:10 PM.
    Thanks DD, you saved me countless times

  4. #4
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Actually, now it has the same problem again. I moved the session_start() and it just makes it loop. It seems that it never sets the session variables. Any ideas? Thanks
    Thanks DD, you saved me countless times

Bookmarks

Posting Permissions

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