gpt4 book ai didi

php - 一些 php mysql 错误

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

下面的代码有一个问题,我似乎无法弄清楚。也许还有更多我还没有看到。出于某种原因,当我使用有效凭据点击提交时,验证无法正常进行。我已经在 mysql 中直接尝试了生成的 sql 代码并且它可以工作,但是 $result 总是返回 false。此外,记住我复选框始终在提交之间进行检查。非常感谢人们提出的任何建议。

谢谢,杰森

<?php
$username = (isset($_POST["username"]) ? $_POST["username"] : (isset($_COOKIE["username"]) ? $_COOKIE["username"] : ""));
$password = (isset($_POST["password"]) ? md5($_POST["password"]) : (isset($_COOKIE["password"]) ? $_COOKIE["password"] : ""));
$pwlength = (isset($_POST["pwlength"]) ? strlen($_POST["pwlength"]) : (isset($_COOKIE["password"]) ? $_COOKIE["password"] : 0));
$remember = (isset($_POST["remember"]) ? 1 : (isset($_COOKIE["remember"]) ? $_COOKIE["remember"] : 0));

if (isset($username) && isset($password)) {
//$result = mysql_query("select companyID from Users where username='$username' && password='$password'");
$result = 0;
if (!$result) {
$message = "Attempt to retrieve user credentials from database failed. Please contact the administrator.";
}
else {
$field = mysql_fetch_assoc($result);
if (!isset($field['companyID'])) {
$message = "Please enter a valid username and password combination.";
}
else {
$_SESSION['companyID'] = $field['companyID'];
if ($remember == '1') {
setcookie("username", $username);
setcookie("password", $password, time()+(7*24*60*60));
setcookie("pwlength", $pwlength, time()+(7*24*60*60));
setcookie("remember", $remember, time()+(7*24*60*60));
}
else {
setcookie("username",'', time()-(60*60));
setcookie("password",'', time()-(60*60));
setcookie("pwlength",'', time()-(60*60));
setcookie("remember",'', time()-(60*60));
}
}
}
}
?>
<html><head><title>Login to CAPA</title>
<?php
//include 'includes/global.php';

session_unset();

//mysql_login();

if (isset($_SESSION['companyID'])) { die("<meta http-equiv='refresh' content='0; url=company.php' /></head></html>"); }
?>
</head>
<body onLoad="document.forms.form.username.focus()">
<?php echo "select companyID from Users where username='$username' && password='$password'"; ?>
<center>
<h1>Login to CAPA</h1>
<?php if (isset($message)) { echo "<p>$message</p>"; } ?>
<form name='form' method="post" action="login.php">
<table border=0>
<tr><td colspan=2>Username:</td><td><input type=text name='username' value='<?php echo $username; ?>'/></td></tr>
<tr><td colspan=2>Password:</td><td><input type=password name='password'<?php echo " value='".str_repeat(" ", $pwlength)."'"; ?> /></td></tr>
<tr><td><input type=checkbox name=remember value=1<?php if (isset($remember)) { echo " checked"; } ?> /> Remember me</td><td colspan=2 align=right><input type=submit value="Submit" /></td></tr>
<tr></tr>
</table>
</form>
</body>
</html>

最佳答案

PHP 读取 0 == false

所以你的代码:

$result = 0;
if (!$result) {
$message = "Attempt to retrieve user credentials from database failed. Please contact the administrator.";
}

永远是真的。这就像在说:

if(!0){
echo 'true';
}

关于php - 一些 php mysql 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6766005/

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