0

I am doing a log in form which has different PHP scripts that link together. I have already posted one of those scripts here and corrected some errors in it. I have tried for hours to make these work but it isn't happening. I get the error

Fatal error: Call to undefined function validate() in C:\Program Files\Abyss Web Server\htdocs\login_action.php on line 21

OK, so I looked at "Login Tools.php" and I had already made some corrections to it. The function appears to be defined correctly. I am copying this code from a PHP newbie book - In Easy Steps. I also looked at the other script Login Action.php, but can't see any errors with that either.

I will show you "Login action.php" first. I know some of you won't like the methods the book uses but I am just copying from the book so I am not choosing the way the script is typed.

<?php

if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
 require ( '../connect_db.php' ) ;

 require ( 'login_tools.php' ) ;

 list ( $check , $data ) =
   validate ( $dbc , $_POST[ 'email' ] , $_POST[ 'pass' ] ) ;

 if ( $check )
  {
   session_start() ;

   $_SESSION[ 'user_id' ] = $data[ 'user_id' ] ;
   $_SESSION[ 'first_name' ] = $data[ 'first_name' ] ;
   $_SESSION[ 'last_name' ] = $data[ 'last_name' ] ;

   load ( 'home.php' ) ;

   }
   else { $errors = $data ; }

   mysqli_close( $dbc ) ;

} 

?> 

So the above script uses the function that is defined in the script below. I have been through both of these and even corrected errors, but can't find why the function is coming up as undefined??

<?php

function load( $page = 'login.php' )

{

$url = 'http://' . $_SERVER[ 'HTTP_HOST' ] .
               dirname( $_SERVER[ 'PHP_SELF' ] ) ;

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

$url .= '/' . $page ;

header( "Location: $url" ) ;
return;


function validate( $dbc , $email = '' , $pwd = '')

{

$errors = array() ;

if ( empty( $email ) )
{ $errors[] = 'Enter your email address.' ; }

else 

{ $e = mysqli_real_escape_string( $dbc, trim( $email ) ) ; }

if ( empty( $pwd ) )
{ $errors[] = 'Enter your password.' ; }

else 

{ $p = mysqli_real_escape_string( $dbc, trim( $pwd ) ) ; }

if ( empty( $errors ))
{
 $q = "SELECT user_id, first_name, last_name, 
   FROM users
   WHERE email = '$e'
   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[] = 'Email address and password not found.' ; } 

}

return array( false , $errors ) ;  }

}

?> 

I hope I am not making some simple error here, but I only post stuff here when I have used code checkers and checked the code again and again manually from the book I am using. Anyone know what I am doing wrong?

2 Answers 2

1

You are missing a closing curly brace:

header( "Location: $url" ) ;
return;


function validate( $dbc , $email = '' , $pwd = '')

{

Should be:

header( "Location: $url" ) ;
return;

}


function validate( $dbc , $email = '' , $pwd = '')

{

Without it your validate function's scope is limited to the load function, which is obviously not what you want.

As an additional point, you might want to consider indenting your code. This way the missing curly brace would have been more obvious.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, would you be so kind as to explain how the function load() works in this post? I've come across the same script but cant seem to get my head around what it does
0

There's a missing } between the end of load and the begin of validate

// (...)
header( "Location: $url" ) ;
return;

} // <--

function validate( $dbc , $email = '' , $pwd = '')

{
// (...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.