duanchuanqu593743 2012-10-27 15:49
浏览 64
已采纳

php和mysql登录脚本错误

I've made a php login script and it just not works.

my code :

<?php

function clean($str) { /* sanatize strings for databases & security */
  $str = trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

if (!empty($_POST['login_admin'])) {

    $username = clean($_POST['name']);
    $password = clean($_POST['password']);

      try {
        $dbh = new PDO('mysql:host=localhost;dbname=imedia', "imedia", "imedia");

        $statement = $dbh->prepare("SELECT * FROM administratori WHERE username =:username AND parola =:password");
        $statement->execute(array(":username" => $username, ":password" => md5($password)));
        $row = $statement->fetch();
        if ($row) {
            session_start("imedia_admin");
            $_SESSION['imedia_admin']['logname'] = $row['username'];
            $_SESSION['imedia_admin']['password'] = $row['password'];
            echo json_encode("success");
        } else {
            echo json_encode("error");
        }

        $dbh = null;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}
?>

problem is in clean function, if a do not use it, my code works, can anybody explain me what i am doing wrong please ?

  • 写回答

2条回答 默认 最新

  • dtu15253 2012-10-27 15:54
    关注

    PDO will take care of it (it automatically escapes single quotes or sanitizes it for you). don't pass the variable with your own clean function.

    Here's what's going on.

    • let's say you have this string hello world's day
    • passing to you function clean makes it hello world''s day
    • then on PDO it escapes it again making it hello world''''s day
    • causing (mismatched) hello world's day not equal to hello world''s day
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗