0

Recently I've upgraded to PHP7, the previous code worked on my older version of PHP, I'm not quite sure how to fix my current problem, is there any recommended reading someone could suggest to get me up to speed with the latest versions of PHP? I have been slowly working through the manual as well although some additional resources would be appreciated, my current error is on line 32, I think it relates to Backward incompatible changes

PHP code

<?php 

require ("classes/Login.php");
require ("classes/Database.php");

    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        $login = new Login();
        $email = $password = "";
        $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);     

        $email = $post['email'];
        $password =  $post['password'];     
        $errors = array();

        $fields = array(
        'email' => array(        
            'validate' => 'validEmail',
            'message' => 'Enter a valid email address',
            'value' => $email,
        ),
        'password' => array(         
            'validate' => 'emptyPassword',
            'message' => 'Password required',
            'value' => $password,
        )
        );


        foreach($fields as $key => $value) 
        {
            $validation_result = $login->$value['validate']($value['value']);

            if(!$validation_result) 
            {
                $errors[] = ['name' => $key, 'error' => $value['message']];
            }
        }

     if(empty($errors))  
        {  
            $db = new Database;
            $query = "SELECT userId,email,username,password FROM users WHERE email = :email";   
            $stmt = $db->prepare($query);    
            $stmt->bindValue(':email', $email);  
            $stmt->execute();   
            if(!$results = $stmt->fetch())  
            {  
                //  email did not match  
                $errors[] = ["name" => "email", "error" => "Incorrect password"];  
            }  
            else   
            {  
                // verify the password  
                if(!password_verify($password, $results['password']))  
                {  
                    // the password did not verify  
                       $errors[] = ["name" => "password", "error" => "Incorrect password"];    
                }  
                else  
                {  
                    // the password did verify 
                    session_start(); 
                    // this is the success response  
                    $success = ['response' => 'true'];   
                    $_SESSION['userId'] = $results['userId'];   
                }  
            }
        }


    }

header('Content-Type: application/json');
if (empty($errors))
{
    echo json_encode($success);
}
else
{
    echo json_encode(["errors" => $errors]);
}   

Login

class Login
{   
    private 
    $email,
    $password;


    public function validEmail($email)
    {   
        return (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE);  

        return $email;
    }   

    public function emptyPassword($password)
    {
        return (empty($password) !== TRUE);

        return $email;
    }       
}

Line 32:

$validation_result = ($login->$value)['validate']($value['value']); 
4
  • 1
    ah come on. atleast point to where line-32 is. Commented Apr 2, 2018 at 15:02
  • I suppose it is $login->$value['validate']($value['value']). What are you trying to do here? Commented Apr 2, 2018 at 15:04
  • Trying to run validation, I guess I really should add that class too for clarity, although it is a little space right now Commented Apr 2, 2018 at 15:06
  • 2
    $login->${$value['validate']}($value['value']) Commented Apr 2, 2018 at 15:14

1 Answer 1

1

I have found that…

$validation_result = {$login->$value['validate']}($value['value']);

works for me. another similar question

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

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.