Results 1 to 8 of 8

Thread: login page help

  1. #1
    Join Date
    Aug 2008
    Location
    Smiths, AL
    Posts
    164
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Default login page help

    okay I have this code for a login script that I am copying out of my book (Practical PHP and MySQL Building Eight Dynamic Web Applications)
    (if you have the book it's page 144)

    I typed the code, retyped the code, then downloaded a digital version of the book and copied the code that way. Why am I still getting this error:
    Parse error: parse error, unexpected '}' in login.php on line 68

    PHP Code:
    <?php
    session_start
    ();
        require(
    "config.php");
        require(
    "functions.php");
        include (
    ".dbconnect.php");

        if(
    $_POST['submit']) {    
        
    $sql "SELECT * FROM users WHERE username = '"
        
    $_POST['username'] . "' AND password = '"
        
    $_POST['password'] . "';";
        
        
    $result mysql_query($sql);
        
    $numrows mysql_num_rows($result);
        
    $result mysql_query($sql);
        
    $numrows mysql_num_rows($result);
        
        if(
    $numrows == 1) {
           
    $row mysql_fetch_assoc($result);        
           if(
    $row['active'] == 1) {
              
    session_register("USERNAME");
              
    session_register("USERID");
              
    $_SESSION['USERNAME'] = $row['username'];
              
    $_SESSION['USERID'] = $row['id'];
            
              switch(
    $_GET['ref']) {
                 case 
    "newpost":
                    if(isset(
    $_GET['id']) == FALSE) {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php");
                    }
                    else {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php?id=" $_GET['id']);
                    }
                 break;

                 case 
    "reply":
                    if(isset(
    $_GET['id']) == FALSE) {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php");
                    }
                    else {
                        
    header("Location: " $config_basedir .
     
    "/newtopic.php?id=" $_GET['id']);
                    }
                 break;

                 default:
                    
    header("Location: " $config_basedir);
                 break;
             }
        }
        else {
           require(
    "header.php");                            
           echo 
    "This account is not verified yet. You were emailed a link
     to verify the account. Please click on the link in the email to
     continue."
    ;
           }
              echo 
    "This account is not verified yet. You were emailed a link
     to verify the account. Please click on the link in the email to 
     continue."
    ;
           }
        }
        else {
           
    header("Location: " $config_basedir "/login.php?error=1");
        }
     }
     else {
     
        require(
    "header.php");
            
        if(
    $_GET['error']) {
           echo 
    "Incorrect login, please try again!";
        }
    ?>

    <form action="<?php echo pf_script_with_get($SCRIPT_NAME); ?>"
    method="post">
    <table>
    <tr>
    <td>Username</td>
    <td><input type="text" name="username"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="submit" value="Login!"></td>
    </tr>
    </table>
    </form>
    Don't have an account? Go and <a href="register.php">Register</a>!

    <?php
    }
    require(
    "footer.php");
    ?>
    Last edited by Dirt_Diver; 02-03-2009 at 01:59 AM.
    ___________________________________

    Still working on it!

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    You have a } right before:

    PHP Code:
    else {
     
        require(
    "header.php");
            
        if(
    $_GET['error']) {
           echo 
    "Incorrect login, please try again!";
        } 
    that doesn't have an opening bracket.

    Are you using a text editor that highlights the opening and closing brackets when you put the cursor next to either one? This is a very very helpful feature.

    On windows I used Notepad++ and on Linux I use Geany.

    Good luck,

    J

  3. The Following User Says Thank You to JasonDFR For This Useful Post:

    Dirt_Diver (02-03-2009)

  4. #3
    Join Date
    Aug 2008
    Location
    Smiths, AL
    Posts
    164
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Default

    I am using DW but I do have notebook++.
    I saw that I didn't have an opening tag but even when I remove it I get this error:

    Parse error: parse error, unexpected T_ELSE in login.php on line 67

    PHP Code:
       require("header.php");    //line 54                        
           
    echo "This account is not verified yet. You were emailed a link
     to verify the account. Please click on the link in the email to
     continue."
    ;
           }
              echo 
    "This account is not verified yet. You were emailed a link
     to verify the account. Please click on the link in the email to 
     continue."
    ;
           }
        }
        else {
           
    header("Location: " $config_basedir "/login.php?error=1");
        }
    else { 
    //line 67
        
    require("header.php");
        
        if(
    $_GET['error']) {
           echo 
    "Incorrect login, please try again!";
        }
    ?> 
    ___________________________________

    Still working on it!

  5. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I've never used dreamweaver, so I'm not sure it does what I tried to described or not.

    Line 67 is right around the same place you had the problem with the bracket.

    You might have two else statements in a row, but that could be another problem. Are you sure you have an "if () {" that corresponds with the else on line 67?

    Just judging by your indents:

    PHP Code:
        else { 
           require(
    "header.php");                             
           echo 
    "This account is not verified yet. You were emailed a link 
     to verify the account. Please click on the link in the email to 
     continue."

           } 
              echo 
    "This account is not verified yet. You were emailed a link 
     to verify the account. Please click on the link in the email to  
     continue."

           } 
        } 
        else { 
           
    header("Location: " $config_basedir "/login.php?error=1"); 
        } 
    The above looks like a problem, but I didn't go through line by line.

    You can't do:

    PHP Code:
    if () {


    } else { 
    // needs to be elseif() { here


    } else {



    Last edited by JasonDFR; 02-02-2009 at 09:24 PM.

  6. The Following User Says Thank You to JasonDFR For This Useful Post:

    Dirt_Diver (02-03-2009)

  7. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    What JasonDFR said should work, just to sum it up - here is the altered code.

    I've tested it and I know it works, the only errors I get is that I don't have the files that are called with the require() function, so that's not a mistake on the part of the coding. Anyway, here it is:

    PHP Code:
    <?php
    session_start
    ();
        require(
    "config.php");
        require(
    "functions.php");
        include (
    ".dbconnect.php");

        if(
    $_POST['submit']) {    
        
    $sql "SELECT * FROM users WHERE username = '"
        
    $_POST['username'] . "' AND password = '"
        
    $_POST['password'] . "';";
        
        
    $result mysql_query($sql);
        
    $numrows mysql_num_rows($result);
        
    $result mysql_query($sql);
        
    $numrows mysql_num_rows($result);
        
        if(
    $numrows == 1) {
           
    $row mysql_fetch_assoc($result);        
           if(
    $row['active'] == 1) {
              
    session_register("USERNAME");
              
    session_register("USERID");
              
    $_SESSION['USERNAME'] = $row['username'];
              
    $_SESSION['USERID'] = $row['id'];
            
              switch(
    $_GET['ref']) {
                 case 
    "newpost":
                    if(isset(
    $_GET['id']) == FALSE) {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php");
                    }
                    else {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php?id=" $_GET['id']);
                    }
                 break;

                 case 
    "reply":
                    if(isset(
    $_GET['id']) == FALSE) {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php");
                    }
                    else {
                        
    header("Location: " $config_basedir .
     
    "/newtopic.php?id=" $_GET['id']);
                    }
                 break;

                 default:
                    
    header("Location: " $config_basedir);
                 break;
             }
        }
        else {
           require(
    "header.php");                            
           echo 
    "This account is not verified yet. You were emailed a link
     to verify the account. Please click on the link in the email to
     continue."
    ;
           }
              echo 
    "This account is not verified yet. You were emailed a link
     to verify the account. Please click on the link in the email to 
     continue."
    ;
           
        }
        else {
           
    header("Location: " $config_basedir "/login.php?error=1");
        }
     }
     else {
     
        require(
    "header.php");
            
        if(
    $_GET['error']) {
           echo 
    "Incorrect login, please try again!";
        }
    ?>

    <form action="<?php echo pf_script_with_get($SCRIPT_NAME); ?>"
    method="post">
    <table>
    <tr>
    <td>Username</td>
    <td><input type="text" name="username"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="submit" value="Login!"></td>
    </tr>
    </table>
    </form>
    Don't have an account? Go and <a href="register.php">Register</a>!

    <?php
    }
    require(
    "footer.php");
    ?>

  8. The Following User Says Thank You to Schmoopy For This Useful Post:

    Dirt_Diver (02-03-2009)

  9. #6
    Join Date
    Aug 2008
    Location
    Smiths, AL
    Posts
    164
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Default

    Here are the pages from the book. I think that is might be a book typo because it has the db connection twice along with the echo statement.


    http://treasureshackonline.com/misc/login_page.pdf
    ___________________________________

    Still working on it!

  10. #7
    Join Date
    Aug 2008
    Location
    Smiths, AL
    Posts
    164
    Thanks
    30
    Thanked 5 Times in 5 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    What JasonDFR said should work, just to sum it up - here is the altered code.

    I've tested it and I know it works, the only errors I get is that I don't have the files that are called with the require() function, so that's not a mistake on the part of the coding. Anyway, here it is:

    PHP Code:
    <?php
    session_start
    ();
        require(
    "config.php");
        require(
    "functions.php");
        include (
    ".dbconnect.php");

        if(
    $_POST['submit']) {    
        
    $sql "SELECT * FROM users WHERE username = '"
        
    $_POST['username'] . "' AND password = '"
        
    $_POST['password'] . "';";
        
        
    $result mysql_query($sql);
        
    $numrows mysql_num_rows($result);
        
    $result mysql_query($sql);
        
    $numrows mysql_num_rows($result);
        
        if(
    $numrows == 1) {
           
    $row mysql_fetch_assoc($result);        
           if(
    $row['active'] == 1) {
              
    session_register("USERNAME");
              
    session_register("USERID");
              
    $_SESSION['USERNAME'] = $row['username'];
              
    $_SESSION['USERID'] = $row['id'];
            
              switch(
    $_GET['ref']) {
                 case 
    "newpost":
                    if(isset(
    $_GET['id']) == FALSE) {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php");
                    }
                    else {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php?id=" $_GET['id']);
                    }
                 break;

                 case 
    "reply":
                    if(isset(
    $_GET['id']) == FALSE) {
                       
    header("Location: " $config_basedir .
     
    "/newtopic.php");
                    }
                    else {
                        
    header("Location: " $config_basedir .
     
    "/newtopic.php?id=" $_GET['id']);
                    }
                 break;

                 default:
                    
    header("Location: " $config_basedir);
                 break;
             }
        }
        else {
           require(
    "header.php");                            
           echo 
    "This account is not verified yet. You were emailed a link
     to verify the account. Please click on the link in the email to
     continue."
    ;
           }
              echo 
    "This account is not verified yet. You were emailed a link
     to verify the account. Please click on the link in the email to 
     continue."
    ;
           
        }
        else {
           
    header("Location: " $config_basedir "/login.php?error=1");
        }
     }
     else {
     
        require(
    "header.php");
            
        if(
    $_GET['error']) {
           echo 
    "Incorrect login, please try again!";
        }
    ?>

    <form action="<?php echo pf_script_with_get($SCRIPT_NAME); ?>"
    method="post">
    <table>
    <tr>
    <td>Username</td>
    <td><input type="text" name="username"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="submit" value="Login!"></td>
    </tr>
    </table>
    </form>
    Don't have an account? Go and <a href="register.php">Register</a>!

    <?php
    }
    require(
    "footer.php");
    ?>
    I don't know why the code above is different from mine when we did the same changes but the code above works and mine did not. Guess I'll need to go in letter by letter.
    thanks everyone
    ___________________________________

    Still working on it!

  11. #8
    Join Date
    Dec 2008
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    make sure to keep it secure!

    mysql_real_escape_string(); around your $_POST[];


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
  •