【问题标题】:Trying to convert mysql to mysqli, not working试图将mysql转换为mysqli,不工作
【发布时间】:2013-12-20 01:35:49
【问题描述】:

我有这段代码,它最初在 mysql() 中,但由于它已被弃用和过时,我决定更改。某些东西显然不起作用,因为当我执行它时,它总是说密码/用户名不正确,尽管它是正确的。数据库工作。三重检查。请原谅我,我是 php 的菜鸟。这里:

<?php
//If the user is logged, we log him out
if(isset($_SESSION['username']))
{
    //We log him out by deleting the username and userid sessions
    unset($_SESSION['username'], $_SESSION['userid']);
?>
<div class="alert alert-info">You have been logged out securely.</div>
<?php
}
else
{
    $ousername = '';
    //We check if the form has been sent
    if(isset($_POST['username'], $_POST['password']))
    {
        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc())
        {
$escapePass = stripslashes($_POST['username']);
$escapeUser = $_POST['username'];
            $ousername = stripslashes($_POST['username']);
            $username = mysqli_real_escape_string($link, $escapePass);
            $password = sha1(stripslashes($_POST['password']));
        }
        else
        {
            $username = mysqli_real_escape_string($link, $escapeUser);
            $password = sha1($_POST['password']);
        }
        //We get the password of the user
$query = 'SELECT password, id FROM users WHERE username="'.$username.'" ';
        $req = mysqli_query($link, $query);
        $dn = mysqli_fetch_array($req);
print $reg;
        //We compare the submited password and the real one, and we check if the user exists
        if($dn['password']==$password and mysqli_num_rows($req)>0)
        {
            //If the password is good, we dont show the form
            $form = false;
            //We save the user name in the session username and the user Id in the session userid
            $_SESSION['username'] = $_POST['username'];
            $_SESSION['userid'] = $dn['id'];
?>

@Fred -ii- 这个:

        if(get_magic_quotes_gpc())
        {
$escapePass = stripslashes($_POST['username']);
$escapeUser = $_POST['username'];
$passescape = sha1($_POST['password']);
$passescape2 = sha1(stripslashes($_POST['password']));
            $ousername = stripslashes($_POST['username']);
            $username = mysqli_real_escape_string($link, $escapePass);
            $password = mysqli_real_escape_string($link, $passescape2);
        }
        else
        {
            $username = mysqli_real_escape_string($link, $escapeUser);
            $password = mysqli_real_escape_string($link, $passescape);
        }

【问题讨论】:

  • 据我所知,您将 $link 传递给您的 username,但不是您的 password
  • 您需要将您的代码缩减到您遇到问题的最低限度,并通过错误消息准确指定问题所在,并清楚地指出行错误所指的位置。
  • 你为什么不改用面向对象的 PHP,这样你就不必做所有额外的输入?
  • @Fred-ii- 所以我只需要为 $password 做一个真正的转义?
  • 我更喜欢手工工作。

标签: php mysql mysqli


【解决方案1】:

只是为了快速测试目的,试试下面这个简单的方法。

然后,您可以从中慢慢建立消毒和故障排除。

<?php
$username = $_POST['username'];
$password = sha1($_POST['password']);

$link = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');

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

$result = mysqli_query($link, $query);

if(mysqli_num_rows($result) < 1) 
{
echo 'Sorry, your username and/or password was incorrect.';
}

else
{
echo "Welcome!";
}
?>

脚注:我在您的原始代码中注意到您正在使用会话。

我没有在你的代码中看到session_start();,也没有提到它。

这需要位于您的代码顶部和您使用的所有文件中,
为了访问当前在 $_SESSION 中的任何内容,需要它

有关会议的更多信息,请访问 PHP.net 网站。

【讨论】:

  • session_start 在我的 config.php 中。
  • 好的,谢谢。你试过我上面的例子吗? @user2624486
  • 这是个好消息!您是否希望我们关闭问题并将其标记为已回答?如果是这样,您可以单击白色复选标记,直到它在我的答案旁边变为绿色,否则它将保留在未回答的类别中。 @user2624486 干杯:)
  • 我做到了,当我发布该评论时对两者都进行了测试。
  • 我让它与原始脚本一起工作。我修复了一些变量,它起作用了!不过感谢您的参考!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2018-02-25
  • 2014-07-02
  • 1970-01-01
  • 2012-05-03
  • 2016-03-14
相关资源
最近更新 更多