1

I'm coding a basic PhP login script right now to develop later on, I've got as far as the Login_tools.php file but when I login in with the wrong details (to check login_tools is working) however I get this error:

"PHP Syntax Check: Parse error: syntax error, unexpected '$un' (T_VARIABLE) in your code on line 27 $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) ; }

    ?>

Any help as to why I am getting this error would be much appreciated...

Shedlor
  • 85
  • 7
  • It works with the correct details? It looks like you're defining things in blocks and trying to use them outside those blocks... – Wain Feb 13 '15 at 21:44

2 Answers2

1

The error is in this line:

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

backslash is an escape char.

Change it to

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

and the error goes away.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Hiya! Thanks for that, I added this to my code. However, when putting in the incorrect details, the errors appear correctly but I get this error : Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\Abyss Web Server\htdocs\login_tools.php on line 31 – Shedlor Feb 13 '15 at 21:54
  • @Shedlor see [here](http://stackoverflow.com/questions/2546314/php-mysql-mysqli-num-rows-expects-parameter-1-to-be-mysqli-result-boolean) – Jens Feb 13 '15 at 22:00
1

Try this one:

<?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) ; }

I hope this helps.

Lost Koder
  • 864
  • 13
  • 32
  • Totally it's good idea to use an IDE, it warns you about syntax erros. – Lost Koder Feb 13 '15 at 21:40
  • Hiya! Thanks for that, I added this to my code. However, when putting in the incorrect details, the errors appear correctly but I get this error : Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\Abyss Web Server\htdocs\login_tools.php on line 31 – Shedlor Feb 13 '15 at 21:48
  • what's an IDE (sorry, really noob question) – Shedlor Feb 13 '15 at 21:49
  • Read this tag [tag:ide] to know more about IDE. Simply, it is advanced text editor. @Shedlor – SaidbakR Feb 14 '15 at 00:30