当用户名和密码正确时不能登录

时间:2023-01-11 23:53:53

Hello so I am doing this tutorial from php academy for a php and mysql login and registration form. It was going okay.. he was showing how to idk what the correct way to say it but to create functions to echo errors.. yeah that sounds right. So the first couple errors echoed correctly but the one to actually validate the user_id and whatknot, isn't working. Its showing the error I created when the username and password combination is incorrect even when i submit the correct information. I've created a few dummy users and none of them can get through.

你好,我正在做php学院的本教程,学习php和mysql登录和注册表单。这是好的. .他展示了如何输入正确的表达方式,但是创建函数来回显错误。是的,听起来不错。因此,前两个错误得到了正确的响应,但是验证user_id和whatknot的错误无效。它显示了我在用户名和密码组合不正确时创建的错误,即使我提交了正确的信息。我已经创建了一些虚拟用户,但没有一个可以通过。

this is my code..

这是我的代码. .

include 'core/init.php';
include 'includes/overall/header.php';

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

if (empty($username) === true || empty($password) === true) {
$errors[] = 'Uh oh! You forgot to enter your username and password';

} else if (user_exists($username) === false) {
$errors[] = 'Who is that? Have you registered?';

} else if (user_active($username) === false) {
$errors[] = 'Account is not activated.';

} else {

$login = login($username, $password);
if ($login === false) {
$errors[] = 'That username and password combination is incorrect';

} else {
    $_SESSION['user_id'] = $login;
    header('Location:index.php');
    exit();
    }
}
print_r($errors);
}

include 'includes/overall/footer.php';

and

function logged_in() {
return (isset($_SESSION['user_id'])) ? true : false;
}

function user_exists($username) {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
return (mysql_result($query, 0) == 1) ? true : false;
}


function user_active($username) {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `active` = 1");
return  (mysql_result($query, 0) == 1) ? true : false;
}

function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}

function login($username, $password) {
$user_id = user_id_from_username($username);

$username = sanitize($username);
$password = md5($password);

$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'");
return (mysql_result($query, 0) == 1) ? $user_id : false;
}

does anyone know what I am doing wrong? I've tried a few things and nothing works. For example in the video he does it a little different and puts his queries in line but that was giving me errors so i did it the way he originally had it and made the queries variables (i only have somewhat of an idea what I'm actually saying haha).. but that fixed the errors. I tried doing that to the other functions (not shown) but that caused a whole lot of errors :(

有人知道我做错了什么吗?我试过一些东西,但都没用。例如,在视频中,他做了一些不同的事情,将他的查询放在一条直线上,但这给了我错误,所以我按照他最初的方式做了,并做了查询变量(我只知道我实际上在说什么,哈哈)。但这修正了错误。我试着对其他函数(未显示)这样做,但这导致了很多错误:(

is it something really dumb? I had a similar problem before that I figured out was due to a missing semi colon but I've stared at this stupid code for so long and haven't found anything.. I re-watched the videos in the tutorial series that explain all this like 10 times each.. my eyes feel like they are going to bleed or explode. Some of the comments show that others are having similar issues.. help?

这真的很愚蠢吗?之前我也遇到过类似的问题,我发现是由于缺少了一个分号,但是我已经盯着这个愚蠢的代码很久了,什么也没发现。我重新观看了教程系列中的视频,每个视频解释了10次。我的眼睛会流血或爆炸。有些评论表明其他人也有类似的问题。帮助吗?

I'm new to all this php mysql stuff so.. I wont be offended if u speak to me like a child.. in fact its appreciated.

我对php mysql很陌生。如果你像个孩子一样对我说话,我不会生气。事实上它的感激。

thanks.

谢谢。

2 个解决方案

#1


0  

Do I understand correctly that you are unable to login now?

我是否正确理解您现在无法登录?

I had similar problems in the past. I do not see your mistake but I will share a method that allowed me to find mistakes.

我过去也遇到过类似的问题。我看不出你的错误,但我将分享一个让我发现错误的方法。

  1. Store the MySQL query in a variable as a string and data inputted from a form
  2. 在变量中将MySQL查询存储为字符串和从表单输入的数据。
  3. echo that variable
  4. 呼应这个变量
  5. Test the outputted string in phpMyAdmin - if the query is wrong it will give you a hint what is wrong with it.
  6. 在phpMyAdmin中测试输出的字符串——如果查询错误,它将提示您它有什么问题。

Also it might be worth testing the queries with "LIKE" instead of "="

同样,用“LIKE”而不是“=”来测试查询也是值得的

eg.

如。

.....FROM `users` WHERE `username` LIKE '$username'.....

#2


0  

This is my code:

这是我的代码:

function user_exists($username) {
  $username = sanitize($username); 
  return (mysql_result(mysql_query("SELECT * FROM `users` WHERE `username` = '$username' "),0) ==1) ? true : false; 
}

#1


0  

Do I understand correctly that you are unable to login now?

我是否正确理解您现在无法登录?

I had similar problems in the past. I do not see your mistake but I will share a method that allowed me to find mistakes.

我过去也遇到过类似的问题。我看不出你的错误,但我将分享一个让我发现错误的方法。

  1. Store the MySQL query in a variable as a string and data inputted from a form
  2. 在变量中将MySQL查询存储为字符串和从表单输入的数据。
  3. echo that variable
  4. 呼应这个变量
  5. Test the outputted string in phpMyAdmin - if the query is wrong it will give you a hint what is wrong with it.
  6. 在phpMyAdmin中测试输出的字符串——如果查询错误,它将提示您它有什么问题。

Also it might be worth testing the queries with "LIKE" instead of "="

同样,用“LIKE”而不是“=”来测试查询也是值得的

eg.

如。

.....FROM `users` WHERE `username` LIKE '$username'.....

#2


0  

This is my code:

这是我的代码:

function user_exists($username) {
  $username = sanitize($username); 
  return (mysql_result(mysql_query("SELECT * FROM `users` WHERE `username` = '$username' "),0) ==1) ? true : false; 
}