Проблема с php логином - PullRequest
       20

Проблема с php логином

0 голосов
/ 09 марта 2011

Я делаю сценарий входа и получаю эту ошибку

Ошибка разбора: синтаксическая ошибка, неожиданная T_ELSE в /Users/Sites/website/login.php на линия 59

и вот мой код:

    <?php

        if(isset($_POST['submitted'])) {

            if(!empty($_POST['email'])) {
                $email = mysql_real_escape_string($_POST['email']);
            } else {
                $email = false;
                echo "<p class='error'>You forgot to enter your email address.</p>";
            }


            if(!empty($_POST['password'])) {
                $password = mysql_real_escape_string($_POST['password']);
            } else {
                $email = false;
                echo "<p class='error'>You forgot to enter your password.</p>";
            }

            if($email && $password){
                $query = "SELECT * FROM `users` WHERE email='$email' AND pass=SHA1('$password') AND active='0'";
                $result= mysql_query ($query, $link) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error($link));
                while ($row = mysql_fetch_array($result)){
                    $id = $row['id'];
                    $bizid = $row['biz_id'];
                    $firstname = $row['first_name'];
                    $lastname = $row['last_name'];
                    $email = $row['email'];
                    $pass = $row['pass'];
                    $date_joined = $row['registration_date'];
                    $address = $row['address'];
                    $phone = $row['phone'];
                    $_SESSION['auth']=1;
                    $_SESSION['id'] = $id;
                    $_SESSION['bizid'] = $bizid;
                    $_SESSION['firstname'] = $firstname;
                    $_SESSION['lastname'] = $lastname;
                    $_SESSION['email'] = $email;
                    $_SESSION['date_joined'] = $date_joined;
                    $_SESSION['address'] = $address;
                    $_SESSION['phone'] = $phone;

                    header("Location: profile.php");
                    exit();
                }   else{
                        echo"<p class='error'>Your email or password was incorrect. Please try again.</p>";
                    }

                }

            }
            mysql_close($link);

        ?>

        <form method='post' action='./login.php'>
        <label>Email Address:</label><br/><input type='text' class='normal' style='border:1px #999 solid;' name='email' />
        <br/><br/><label>Password:</label><br/><input type='password' class='normal' style='border:1px #999 solid;' name='password' />
        <input type='submit' class='normalbutton' value='Login' />
        <p><br/><a href='forgotpassword.php'>Forgot Your Password?</a></p>
        <p><br/><a href='signup.php'>Don't have an Account? Sign Up</a></p>
        <input type='hidden' name='submitted' value='TRUE' />
        </form>

И помощь будет принята с благодарностью. Спасибо!

Ответы [ 5 ]

3 голосов
/ 09 марта 2011
                header("Location: profile.php");
                exit();
            }   else{
                    echo"<p class='err

Должно быть:

                header("Location: profile.php");
                exit();
            } 
 }  else{
                    echo"<p class='err

Вам не хватает }.

2 голосов
/ 09 марта 2011

Это исправленная версия вашего кода.

if (isset($_POST['submitted']))
{
    $errors   = array();
    $required = array('email', 'password');

    foreach($required as $field)
    {
        if(empty($_POST[$field]))
        {
            $errors[$field] = 'You forgot to enter the '. $field .' field.';
        }
        else {
            $required[$field] = = mysql_real_escape_string($_POST[$field]);
        }
    }

    if(count($errors) == 0)
    {
        $query = "SELECT * FROM `users` WHERE email='$email' AND password=SHA1('$password') AND active='0' LIMIT 1";
        $result= mysql_query ($query, $link) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error($link));

        if(mysql_num_rows($result) > 0)
        {
            while ($row = mysql_fetch_array($result))
            {
                $id                         = $row['id'];
                $bizid                      = $row['biz_id'];
                $firstname                  = $row['first_name'];
                $lastname                   = $row['last_name'];
                $email                      = $row['email'];
                $pass                       = $row['pass'];
                $date_joined                = $row['registration_date'];
                $address                    = $row['address'];
                $phone                      = $row['phone'];

                $_SESSION['auth']           = 1;
                $_SESSION['id']             = $id;
                $_SESSION['bizid']          = $bizid;
                $_SESSION['firstname']      = $firstname;
                $_SESSION['lastname']       = $lastname;
                $_SESSION['email']          = $email;
                $_SESSION['date_joined']    = $date_joined;
                $_SESSION['address']        = $address;
                $_SESSION['phone']          = $phone;

                header("Location: profile.php");
                exit();
            }
        }
        else{
            echo '<p class="error">Your email and password was not entered correctly!</p>'
        }
    }
    else{
        foreach($errors as $error)
        {
            echo '<p class="error">'. $error .'</p>';
        }
    }
}

mysql_close($link);

?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
    <p>
        <label>Email Address:</label>
        <input type='text' class='normal' style='border:1px #999 solid;' name='email' />
    </p>
    <p>
        <label>Password:</label>
        <input type='password' class='normal' style='border:1px #999 solid;' name='password' />
    </p>
    <p>
        <input type='submit' name="submitted" class='normalbutton' value='Login' />
        <a href='signup.php'>Don't have an Account? Sign Up</a>
    </p>
    <p>
        <a href='forgotpassword.php'>Forgot Your Password?</a>
    </p>
</form>
1 голос
/ 09 марта 2011

    if(isset($_POST['submitted'])) {

        if(!empty($_POST['email'])) {
            $email = mysql_real_escape_string($_POST['email']);
        } else {
            $email = false;
            echo "<p class='error'>You forgot to enter your email address.</p>";
        }


        if(!empty($_POST['password'])) {
            $password = mysql_real_escape_string($_POST['password']);
        } else {
            $email = false;
            echo "<p class='error'>You forgot to enter your password.</p>";
        }

        if($email && $password){
            $query = "SELECT * FROM `users` WHERE email='$email' AND pass=SHA1('$password') AND active='0'";
            $result= mysql_query ($query, $link) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error($link));
            while ($row = mysql_fetch_array($result)){
                $id = $row['id'];
                $bizid = $row['biz_id'];
                $firstname = $row['first_name'];
                $lastname = $row['last_name'];
                $email = $row['email'];
                $pass = $row['pass'];
                $date_joined = $row['registration_date'];
                $address = $row['address'];
                $phone = $row['phone'];
                $_SESSION['auth']=1;
                $_SESSION['id'] = $id;
                $_SESSION['bizid'] = $bizid;
                $_SESSION['firstname'] = $firstname;
                $_SESSION['lastname'] = $lastname;
                $_SESSION['email'] = $email;
                $_SESSION['date_joined'] = $date_joined;
                $_SESSION['address'] = $address;
                $_SESSION['phone'] = $phone;

                header("Location: profile.php");
                exit();
            }  } else {
                    echo"<p class='error'>Your email or password was incorrect. Please try again.</p>";
                }



        }
        mysql_close($link);

    ?>

    <form method='post' action='./login.php'>
    <label>Email Address:</label><br/><input type='text' class='normal' style='border:1px #999 solid;' name='email' />
    <br/><br/><label>Password:</label><br/><input type='password' class='normal' style='border:1px #999 solid;' name='password' />
    <input type='submit' class='normalbutton' value='Login' />
    <p><br/><a href='forgotpassword.php'>Forgot Your Password?</a></p>
    <p><br/><a href='signup.php'>Don't have an Account? Sign Up</a></p>
    <input type='hidden' name='submitted' value='TRUE' />
    </form>

Поставьте этот код и запустите ваш скрипт

0 голосов
/ 09 марта 2011

вам нужен еще один}, чтобы закрыть if раньше, чем здесь:

  else {
     echo"<p class='error'>Your email or password was incorrect. Please try again.</p>";
  }
0 голосов
/ 09 марта 2011

Ваш цикл while не замкнут.

...