0

こんにちは、私はPDOをログインスクリプトに埋め込んで、SQLインジェクションから保護しようとしています。しかし、白いページを取得しているのは、ユーザーが本物かどうかを確認するために行を数えようとしているからだと思います。

// Here we inculde the function page
include 'functions/functions.php';
// Here we connect to the db
$db = mysqlconnect();

$password = md5($_POST['mypassword']);
$statement = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$statement->execute(array($_POST['myusername'], $password));


// Replace counting function based on database you are using.
 $count = $statement->rowCount();
// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1){
  // Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['username'] = $myusername ;


//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
  $ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
  $ip=$_SERVER['REMOTE_ADDR'];
}


$updateinfo=mysql_query("UPDATE `users` SET lastip ='$ip' WHERE `username` = '".$_SESSION['username']."'");
mysql_query("INSERT INTO user_log 
(username, ip) VALUES('".$_SESSION['username']."', '$ip' ) ") 
or die(mysql_error());  





header("Location: home.php");
} else {
  echo "Wrong Username or Password";
}


echo"<p> </p>";

エラーが発生しないのは、単なる白いページです。

また、ここに私が含む私の関数ページがあります

    function mysqlconnect(){
     global $db;
    $host = 'localhost';
    $port = 3306; // This is the default port for MySQL
    $database = '';
    $username = '';
    $password = '';

    // Construct the DSN, or "Data Source Name".  Really, it's just a fancy name
    // for a string that says what type of server we're connecting to, and how
    // to connect to it.  As long as the above is filled out, this line is all
    // you need :)
    $dsn = "mysql:host=$host;port=$port;dbname=$database";

    // Connect!
    $db = new PDO($dsn, $username, $password);

}
4

1 に答える 1

1

コードには、目玉に固執するものがいくつかあります。

ここにスクリプト全体を貼り付けた場合は、が欠落していますsession_start()。何が含まれているのかわかりませんがhome.php、コンテンツの生成が$ _SESSION ['username']の値に依存している場合、ヘッダーのリダイレクト後に空になるため、発生することはありません。

session_start()に関するマニュアルをご覧ください。

また、述べたように:

ほとんどのデータベースでは、PDOStatement :: rowCount()はSELECTステートメントの影響を受ける行数を返しません。

念のため、これについて言及しようと考えました。私は過去にかなりの時間を費やして、このことを自分で考えてきました。

rowCountに関するマニュアルの例2を一瞥することをお勧めします。

そしてもちろん、@ Paulがすでに指摘してmysql_query()いるように、PDOに移行する場合は、もう使用しないでください。

于 2012-05-26T02:51:19.137 に答える