【问题标题】:PHP - Error - MySQLPHP - 错误 - MySQL
【发布时间】:2013-08-27 21:37:51
【问题描述】:

有谁知道可能出了什么问题?我已经尝试了一切,搜索了一切,但一无所获。可能只是有一个我看不到的错字。

函数类

function validate_credentials($user_name, $user_password) {
    $user_name      = mysql_real_escape_string($user_name);
    $user_password  = sha1($user_password);

    $result = mysql_query("SELECT `user_id` FROM `users` WHERE `user_name` = '{$user_name}' AND `user_password` = '{$user_password}'");

    if (mysql_num_rows($result) !== 1) {
        return false;
    }

    return mysql_result($result, 0);
}

include('./core/user.inc.php');

if (isset($_POST['user_name'], $_POST['user_password'])) {
    if (($user_id = validate_credentials($_POST['user_name'], $_POST['user_password'])) != false) {
        $_SESSION['user_id'] = $user_id;

        header('Location: ./messages/');

        die();
    }
}

这是我的 HTML:

<form method="post" action="./sign-in.php">
    <input type="text" name="user_name" id="user_name" placeholder="Username" />
    <input type="password" name="user_password" id="user_password" placeholder="Password" />
    <input type="submit" placeholder="Confirm Password" value="Sign in" />
</form>

【问题讨论】:

  • 运行时会发生什么?有任何错误信息吗?
  • 我使用的是谷歌浏览器,它只是说“服务器错误”。
  • 我发现 'if (($user_id = validate_credentials($_POST['user_name'], $_POST['user_password'])) != false) {' 导致错误
  • 你的脚本会重定向到 ./messages 吗?
  • 您很可能需要打开错误报告或检查日志以查看那里是否报告了任何内容。在页面中的 PHP 标记之后调用 error_reporting(E_ALL); 可能就足够了。另外,你有没有给session_start 打过电话?

标签: php mysql login system


【解决方案1】:
function validate_credentials($user_name, $user_password) {
   $user_name = trim(mysql_real_escape_string($user_name));
   $user_password = trim(mysql_real_escape_string(sha1($user_password)));

   $result = @mysql_query("SELECT user_id FROM users WHERE user_name = '".$user_name."' AND user_password = '".$user_password."'");

   if (mysql_num_rows($result) != 1) {
      return false;
   }
   return mysql_result($result, 0);

}

include('./core/user.inc.php');

if (isset($_POST['user_name']) && isset($_POST['user_password'])) {
   $user_id = validate_credentials($_POST['user_name'], $_POST['user_password']);
   if ($user_id) {
      $_SESSION['user_id'] = $user_id;
      header('Location: ./messages/');
      die();
   }
}

【讨论】:

  • 我明白了,脚本不喜欢从另一个类访问函数。
  • 您检查了 $user_id 变量是否为空?
【解决方案2】:

你可以试试

$user_id = validate_credentials($_POST['user_name'], $_POST['user_password'])

if($user_id){
  $_SESSION['user_id'] = $user_id;  blahblah}

【讨论】:

  • 您确定密码已插入并使用 sha1 加密吗?也请使用其他浏览器。
  • 哦,但是函数不应该包含与mysql的连接,否则select将不起作用。
  • 我也用过 Safari,这个密码是用 SHA1 加密的。
  • 使函数包含 MySQL 连接没有做任何事情。
  • 顺便说一句,我在 Mac 上使用 MAMP 和 PHP 5.4.4 并使用 XCache,这可能会有所帮助。
猜你喜欢
  • 2016-09-02
  • 2012-07-24
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 2014-07-07
  • 2013-03-01
  • 2013-03-13
相关资源
最近更新 更多