gpt4 book ai didi

php - PHP登录脚本不起作用-引发错误

转载 作者:行者123 更新时间:2023-11-29 04:02:00 25 4
gpt4 key购买 nike

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Warning: mysql_num_rows() expects parameter 1 to be resource,
前几天我在这里发布了一个问题,因为我的登录脚本只是显示了一个空白页面,我按照建议打开了错误报告并解决了问题,但是现在这是一个不同的问题,当我现在登录时,它有时只是将我重定向回登录屏幕,如果登录失败,这是要重定向的位置(我已经检查了我使用正确的密码)。偶尔会出现以下错误。

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/PropSuite/login.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/PropSuite/login.php:24) in /Applications/MAMP/htdocs/PropSuite/login.php on line 26

这是我的代码:
<?php

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

session_start(); //must call session_start before using any $_SESSION variables
$username = $_POST['username'];
$password = $_POST['password'];
//connect to the database here

$hostname_PropSuite = "localhost";
$database_PropSuite = "propsuite";
$username_PropSuite = "root";
$password_PropSuite = "root";
$PropSuite = mysql_pconnect($hostname_PropSuite, $username_PropSuite, $password_PropSuite) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_PropSuite, $PropSuite);

$username = mysql_real_escape_string($username);
$query = "SELECT password, salt
FROM users
WHERE username = '$username';";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1) //no such user exists
{
header('Location: http://localhost/PropSuite/index.php');

die();
}
$userData = mysql_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . Hash('sha256', $password) );
if($hash != $userData['password']) //incorrect password
{
header('Location: http://localhost/PropSuite/index.php');

die();
}
else
{
validateUser(); //sets the session data for this user
}
//redirect to another page or display "login success" message
header('Location: your-desired-login-location.php');
die()


//redirect to another page or display "login success" message


?>

我对PHP还不太熟悉,所以希望您的帮助,顺便提一下,我可能会得到一些关于将数据库信息直接放在脚本中的评论,这最终将通过一个连接文件来完成。

最佳答案

第一个问题是以下查询:

$query = "SELECT password, salt
FROM users
WHERE username = '$username';";

它会在每一行的空白处(空白处)提取。这将使它成为一个无效查询,这就是此行不起作用的原因:
if(mysql_num_rows($result) < 1) //no such user exists
{
header('Location: http://localhost/PropSuite/index.php');

die();
}

尝试将查询更改为一行:
$query = "SELECT password, salt FROM users WHERE username = '$username';";

要进行调试,请尝试输入以下命令:
$result = mysql_query($query) or die(mysql_error());

关于头,一旦出现任何形式的输出,就不能立即发送头。您可以执行以下操作来绕过它:
<?php
ob_start(); // Start output buffering

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

关于php - PHP登录脚本不起作用-引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13048166/

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