php 登录脚本如果返回 else 即使登录信息在 db 中是正确的

标签 php mysql

此代码块用于以我正在构建的页面后端的管理员身份登录。它解析为与我的“不正确信息”声明相呼应的“其他”声明。基本上说它没有找到我在服务器上创建的凭据,我已经检查并仔细检查过。还确认我的连接脚本正在运行。我很难过。任何帮助将不胜感激。

<?php
session_start();
if(isset($_SESSION["manager"])) {
  header("location: index.php");
  exit();
}
?>
<?php 
// Parse the log in form if the user has filled it out and pressed "Log In"
if (isset($_POST["username"]) && isset($_POST["password"])) {

    $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["username"]); // filter everything but numbers and letters 
      $password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]); // filter everything but numbers and letters 
      // Connect to the MySQL database
      include "../php/connect_to_mysql.php";
      $sql = mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"); // Query the person
//------MAKE SURE PERSON EXISTS-----
$existCount = mysql_num_rows($sql); // Count the row nums
if ($existCount == 1) {
    while($row = mysql_fetch_array($sql)){
      $id = $row["id"];
    }
    $_SESSION["id"] = $id;
    $_SESSION["manager"] = $manager;
    $_SESSION["password"] = $password;
    header("location: index.php");
    exit();
  } else {
    echo 'That information is incorrect, <a href="index.php">try again</a>.';
    exit();
  }
}
?>

这是表单代码

<form name="adminform" id="adminform" method="post" action="admin_login.php">
              <div class="row collapse">
                <div class="large-2 columns">
                  <label class="inline">Username</label>
                </div>
                <div class="large-10 columns">
                  <input type="text" id="username" placeholder="JohnDoe" name="username">
                </div>
              </div>
              <div class="row collapse">
                <div class="large-2 columns">
                  <label class="inline">Password</label>
                </div>
                <div class="large-10 columns">
                  <input type="password" id="password" placeholder="Shazam" name="password">
                </div>
              </div>
              <button type="submit" name="button" id="button" class="radius button">Log In</button>
        </form>

最佳答案

问题可能在于,即使您正在检查 $_POST var,当您执行过滤器时,您也会使用 $_SESSION 值。尝试使用 $_POST。

// Parse the log in form if the user has filled it out and pressed "Log In"
if (isset($_POST["username"]) && isset($_POST["password"])) {
    $manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]);
    $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]);

    // ...

关于php 登录脚本如果返回 else 即使登录信息在 db 中是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18154981/

相关文章:

php 数组数据在 JQuery 中不起作用

php - 使用 php 循环遍历多个连接表

python mysql 连接器查询字符串语法

javascript - 当查询有变量时,PHP/Javascript + MySQL 数据不会被检索

python - 有没有办法在 Python 2.7.5 的 MySQLdb.connect 中将 secure_auth 设置为 false?

php - 带有动态表删除记录按钮的 jQuery 对话框不起作用

php - 使用PHP显示的长日志数据易于阅读

php - 在 php 中使用 session.use_cookies

php - 如何在更新 MYSQL 数据库时向列表发送电子邮件

mysql - 如何从mysql状态列中显示通过和失败的总和?