gpt4 book ai didi

php - 用户身份验证 PHP mySQL 不工作

转载 作者:行者123 更新时间:2023-11-29 21:31:19 25 4
gpt4 key购买 nike

所以我目前正在Xampp中使用php和mysql构建一个用户身份验证系统。

我已经设法让它通过电子邮件地址识别用户是否存在,但其他功能似乎不起作用。例如,检查用户是否已激活其帐户,因为即使我在数据库中将其事件状态更改为 1,他们也没有回来。或者使用登录功能,即使电子邮件和密码都正确,也会说它们不正确。

这是我的login.php脚本

    <?php
include 'init.php';


function sanitize($data){
return mysql_real_escape_string($data);
}

//check if user exists
function user_exists($email){
$email = sanitize($email);
//$query = mysql_query("SELECT COUNT('ID') FROM 'register' WHERE 'email' = '$email'");
return (mysql_result(mysql_query("SELECT COUNT(ID) FROM register WHERE email = '$email'"),0) == 1)? true : false;
}

//check if user has activated account
function user_activate($email){
$email = sanitize($email);
//$query = mysql_query("SELECT COUNT('ID') FROM 'register' WHERE 'email' = '$email'");
return (mysql_result(mysql_query("SELECT COUNT(ID) FROM register WHERE email = '$email' AND 'active' =1"),0) == 1)? true : false;
}
function user_id_from_email($email){
$email = sanitize($email);
return (mysql_result(mysql_query("SELECT id FROM register WHERE email = '$email'"),0,'id'));
}
function login($email,$password){
$user_id = user_id_from_email($email);
$email = sanitize($email);
$password = md5($password);

return (mysql_result(mysql_query("SELECT COUNT(id) FROM register WHERE email = '$email' AND 'password' ='$password'"),0) == 1)? $user_id : false;
}


if(empty($_POST)=== false){
$email = $_POST['email'];
$password = $_POST['password'];
}

if(empty($email)|| empty($password) === true){
$errors[] = "You must enter a username and a password";
}
else if(user_exists($email) === false){
$errors[] = "Email address is not registered";
}
else if(user_activate($email) === false){
$errors[] = "You haven't activated your account yet";
}
else{
$login = login($email, $password);
if($login === false){
$errors[] = "email/password are incorrect";
} else {
echo "ok";
}
}

print_r($errors);


/*$email = $_POST['email'];
$password = $_POST['password'];


if($email&&$password){
$connect = mysql_connect("localhost","root","") or die ("Couldn't Connect");
mysql_select_db("users") or die("Couldn't find Database");
}
else
die("Please enter a username and a password");

$query = mysql_query("SELECT * FROM register WHERE email = '$email'");
$numrows = mysql_num_rows($query);

echo $numrows;*/


?>

我的数据库名为'users',目前只有 1 个名为'register' 的表。包含行:id、名字、姓氏、电子邮件、密码和事件

最佳答案

在您的函数登录中,尝试删除字段名称密码周围的引号 '。或者更喜欢使用这个 ` .

请注意,您正在使用函数 mysql_result 和 mysql_query,PHP 7.0 不再支持这两个函数

正如你在这里看到的: http://php.net/manual/en/function.mysql-query.php http://php.net/manual/en/function.mysql-result.php

关于php - 用户身份验证 PHP mySQL 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35231715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com