PhP脚本login_tools出错

时间:2015-02-13 21:34:56

标签: php mysql

我正在编写一个基本的PhP登录脚本,以便稍后开发,我已经到了Login_tools.php文件,但当我登录时出现错误的详细信息(检查login_tools是否有效)但是我得到了这个错误:

“PHP语法检查:解析错误:语法错误,第27行代码中的意外'$ un'(T_VARIABLE) $ q =“SELECT user_id,username FROM users WHERE username ='$ un'AND pass = SHA1('$ p')”;“

    <?php

function load($page = 'login.php')
{
$url = 'http://'.$_SERVER['HTTP_HOST'].dirname( $_SERVER['PHP_SELF']);
$url = rtrim( $url, '\');
$url .= '/'.$page ;

header("Location:$url") ;
exit() ;    
}
function validate( $dbc, $username =", $pwd=")
{
    $errors = array(); 
    if( empty($username))
    { $errors[] = "Enter your username."; }
    else
    {$un = mysqli_real_escape_string( $dbc, trim( $username));}

    if( empty( $pwd))
    { $errors[]= "Enter your password.";}
    else
    {$p = mysqli_real_escape_string($dbc, trim($pwd)); }

    if(empty( $errors))
    {
     $q = "SELECT user_id, username FROM users WHERE username = '$un' AND pass = SHA1('$p')" ;

     $r = mysqli_query ( $dbc, $q) ;

     if(mysqli_num_rows( $r ) == 1)
     {
      $row = mysqli_fetch_array ($r, MYSQLI_ASSOC) ;
      return array (true, $row) ;
     }
     else 
     { $errors[] = 'Username and/or Passsword not found.';}
    }

    return array ( false, $errors) ; }

    ?>

对于我收到此错误的原因有任何帮助,我们将不胜感激......

3 个答案:

答案 0 :(得分:1)

错误在这一行:

url = rtrim( $url, '\');

反斜杠是一个转义字符。

将其更改为

url = rtrim( $url, '\\');

并且错误消失了。

答案 1 :(得分:1)

试试这个:

<?php

function load($page = 'login.php')
{
    $url = 'http://'.$_SERVER['HTTP_HOST'].dirname( $_SERVER['PHP_SELF']);
    $url = rtrim( $url, '\\');
$url .= '/'.$page ;

header("Location:$url") ;
exit() ;    
}
function validate( $dbc, $username ="", $pwd="")
{
    $errors = array(); 
    if( empty($username))
    { $errors[] = "Enter your username."; }
    else
    {$un = mysqli_real_escape_string( $dbc, trim( $username));}

    if( empty( $pwd))
    { $errors[]= "Enter your password.";}
    else
    {$p = mysqli_real_escape_string($dbc, trim($pwd)); }

    if(empty( $errors))
    {
     $q = "SELECT user_id, username FROM users WHERE username = '$un' AND pass = SHA1('$p')" ;

     $r = mysqli_query ( $dbc, $q) ;

     if(mysqli_num_rows( $r ) == 1)
     {
      $row = mysqli_fetch_array ($r, MYSQLI_ASSOC) ;
      return array (true, $row) ;
     }
     else 
     { $errors[] = 'Username and/or Passsword not found.';}
    }

    return array ( false, $errors) ; }

我希望这会有所帮助。

答案 2 :(得分:0)

$url = rtrim( $url, '\');

这应该是:

$url = rtrim( $url, '\\');