gpt4 book ai didi

php - Jquery/PHP ajax登录系统

转载 作者:行者123 更新时间:2023-11-29 14:44:09 25 4
gpt4 key购买 nike

我正在为我的企业设置一个博客类型页面。 MySQL 和 PHP 是全新的。设置此登录系统。由于某种原因,不知道为什么登录会下降。假设检查错误,如果电子邮件和密码正确,则通过 php 返回“good”。如果 php 返回良好,那么它应该重定向到博客页面。

我已经处理这个问题几个月了,需要紧急帮助。谢谢。这是与 jquery 一起使用的 php 代码。

测试站点的链接在这里。 test.toddprod.com/login非常感谢您的帮助。谢谢

<?php
#fake mysql connection first
DEFINE ('DB_USER','usernamegoeshere');
DEFINE ('DB_PASSWORD','passwordhere');
DEFINE ('DB_HOST','hostnamehere');
DEFINE ('DB_NAME','andtheotherthinghere');

$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL');

$db = mysql_select_db(DB_NAME, $dbc) or die('Could not select database.'.mysql_error());


$e = $_POST['email'];
$pass = $_POST['pass'];
$q = 'SELECT user_id from toddprod where email="'.$e.'" and pass= SHA1("'.$pass.'")';
$r = mysql_query($db, $q);
if( mysql_num_rows($r)==1 ){
setcookie ( 'user_id', $r);
setcookie ( 'email', '$e');
setcookie ( 'logged-in', 'true');
echo 'good';
}
else if (mysql_num_rows($r)==0) {
echo 'Your '.$e.' with password '.$pass;
};
mysql_close ($db);
?>

最佳答案

嗯,我发现这里有很多错误......

首先应该清理您的查询...

$email = mysql_real_escape_string ($_POST['email']); // escape the email
$pass = SHA1(mysql_real_escape_string ($_POST['pass'])); // escape and encrypt the pass

// now you can put it into the query safely
$query = "SELECT user_id from toddprod where email = '$email' and pass = '$pass' ";

接下来,您执行的查询是错误的,mysql_query 函数采用两个参数:查询和数据库连接。您传递了错误的参数,您传递了查询和 mysql_select_db 函数的结果,该结果只是一个 bool 值。因此,您必须将 $dbc 而不是 $db 放入该查询中,即使如此,您也会以错误的顺序传递参数。首先是查询,然后是连接。所以应该是...

$result = mysql_query($query, $dbc);

接下来,您尝试将 mysql_query 函数的返回值设置为 cookie,但该值是资源,而不是您需要的用户 ID。您必须像这样实际读取资源中的值。

$row = mysql_fetch_array($result);
$userid = $row["user_id"];
setcookie('user_id', $userid);

继续...当您设置电子邮件 cookie 时,您将变量放在单引号中,因此 cookie 实际上将包含 $e 而不是实际的电子邮件,因为单引号存储字符串litterly(不解析变量)。因此,您应该使用双引号,或者根本不使用引号。因此,以下任一选项都可以...

setcookie('email', "$e");
setcookie('email', $e);

最后但并非最不重要的一点是,您的 if 语句末尾不应该有分号,并且您再次需要将连接而不是数据库选择结果传递到 mysql_close 函数中,所以应该是这样

mysql_close($dbc);

希望这能让您有所帮助,尝试这些更改,如果问题仍然存在,我很乐意为您提供进一步帮助。

以下链接可以帮助您:

http://www.php.net/manual/en/function.mysql-query.php

http://www.php.net/manual/en/function.mysql-fetch-array.php

http://www.php.net/manual/en/function.mysql-real-escape-string.php

编辑:

这里,我根据我发现的问题修复了代码。尝试一下,我无法测试它,所以它可能到处都有一些小的语法错误,但它应该给你一些可以比较的东西。另外,对于 future ,我建议您以语义/正确的方式命名变量,以便其他人更容易理解,并且也可以防止您感到困惑,就像您将 $db 而不是 $dbc 传递到您的一些函数中一样。

<?php
// keep the function names in lowercase, no reason, just looks better to me
define('DB_USER', 'usernamegoeshere');
define('DB_PASSWORD', 'passwordhere');
define('DB_HOST', 'hostnamehere');
define('DB_NAME', 'andtheotherthinghere');

// connect to the mysql server
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL');
// select the database, you don't need to store the result, it just returns true or false
mysql_select_db(DB_NAME, $conn) or die('Could not select database.' .mysql_error());

// escape the input
$email = mysql_real_escape_string($_POST['email']);
$pass = sha1(mysql_real_escape_string($_POST['pass']));

// create the query
$query = "SELECT user_id FROM toddprod WHERE email = '$email' AND pass = '$pass'";

// execute the query
$result = mysql_query($query, $conn);
$usercount = mysql_num_rows($result);

if($usercount == 1){
// read the results and get the user_id
$row = mysql_fetch_array($result);
$userid = $row['user_id'];

// set the cookies
setcookie('user_id', $userid);
setcookie('email', $email);
setcookie('logged-in', 'true');

// echo success message
echo 'good';
}elseif($usercount == 0) {
echo "You're $email with password $pass";
}
mysql_close($conn);
?>

关于php - Jquery/PHP ajax登录系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7341760/

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