Reputation: 43
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']);
Upvotes: 0
Views: 3636
Reputation: 603
I have found that…
$validation_result = {$login->$value['validate']}($value['value']);
works for me. another similar question
Upvotes: 1