我正在为登录页面创建一个有效的凭证功能。
如果我使用以下代码在函数外部使用该函数,则该函数有效:
if(isset($_POST['email'])){
$email = $_POST['email'];
$password = $_POST['password'];
$q = "SELECT * FROM user WHERE email = :email";
$query = $db->prepare($q);
$query->execute(array(":email" => $email));
$results = $query->fetchAll();
if($results !=FALSE && $query->rowCount() > 0) {
if($results[0]['password'] == $password){
$_SESSION['email'] = $email;
}
但是,如果我从外部函数调用该函数,则不会发生任何事情,就好像登录凭据有问题一样。
功能:
function valid_credientials($email,$password){
global $db;
$email = $_POST['email'];
$password = $_POST['password'];
$q = "SELECT * FROM user WHERE email = :email";
$query = $db->prepare($q);
$query->execute(array(":email" => $email));
$results = $query->fetchAll();
if($results !=FALSE && $query->rowCount() > 0) {
if($results[0]['password'] == $password){
$_SESSION['email'] = $email;
}}
}
和isset
帖子:
if (isset($_POST['email'], $_POST['password'])){
if (valid_credientials($_POST['email'], $_POST['password']) == false ){
$errors[] = 'No matching email found.';
}
if (empty($errors)){
$_SESSION['email'] = htmlentities($_POST['email']);
header("Location: profile.php");
die();
}
}
我正在考虑连接问题,但如果我使用此功能,它会返回电子邮件。
function valid_credientials($email,$password){
$test = $db->query("SELECT `email` FROM `user`");
while($row = $test->fetch(PDO::FETCH_ASSOC)){
echo $row['email'], '<br>';
}
}