Dear Forum,
I am having a little trouble writing a login script for a website I am making with the help of a PHP book. If I enter an incorrect username/password/blank_field the errors work perfectly, but I get a 500 error whenever I put in correct details. I have no idea how to fix it or what I am doing wrong, so would really appreciate some help!
I have a few files, but hopefully the two will hold the problem. Please let me know if you need more from me. Thanks so much!!
login_tools.php:
[php]<?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 , $user = ‘’ , $pwd = ‘’ )
{
$errors = array();
if ( empty( $user ) )
{ $errors[] = ‘Please enter your username.’;}
else
{ $e = mysqli_real_escape_string( $dbc , trim( $user ) ) ; }
if ( empty( $pwd ) )
{ $errors[] = ‘Please enter your password.’ ; }
else
{ $p = mysqli_real_escape_string( $dbc , trim( $pwd ) ) ; }
if ( empty( $errors ) )
{
$q = “SELECT gLogon, gFirstName, gLastName
FROM dGuests
WHERE gLogon = ‘$e’
AND gPassword = ‘$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 password have not been found. Please contact the webmaster.' ; }
}
return array( false , $errors ) ;
} [/php]
login_action.php:
[php]<?php
if ( $_SERVER[ ‘REQUEST_METHOD’ ] == ‘POST’ )
{
require ( ‘…/connect_db.php’);
require ( ‘login_tools.php’ );
list ( $check , $data ) =
validate ( $dbc , $_POST[ ‘gLogon’ ] , $_POST[ ‘gPassword’ ] );
if ( $check )
{
session_start();
$_SESSION[ 'Logon' ] = $data[ 'gLogon' ];
$_SESSION[ 'FirstName' ] = $data[ 'gFirstName' ];
$_SESSION[ 'LastName' ] = $data[ 'gLastName' ];
load ('ccenter.php');
}
else { $errors = $data ; }
mysqli_close( $dbc);
}
include ( ‘index.php’ ) ; [/php]