2012-05-26 92 views
0

你好我試圖植入PDO到我的登錄腳本,使其從SQL注入安全。但即時得到一個白色的頁面,我認爲它是因爲我嘗試計算行數,看看用戶是否真正.....Php簡單登錄腳本白頁

// 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); 

} 
+1

停止使用'global'關鍵字。你用'md5()'做了什麼?你確定你已經啓用了錯誤報告。 – PeeHaa

+0

是錯誤報告已打開。並返回$ db;顯示沒有數據庫選擇(當int他功能文件有)而我使用MD5,因爲用戶密碼是MD5 ... – user1405062

+1

你仍然在使用'mysql_query'。你想達到什麼目的?我看到你正在使用PDO連接到數據庫,但是然後你使用'mysql_query'之外的 –

回答

1

有沒有在你的代碼夫婦的事情,在眼球枝。

如果你在這裏粘貼了整個腳本,那麼你錯過了session_start()。我不知道你的home.php裏有什麼,但是如果它的內容生成依賴於$ _SESSION ['username']中的一個值,它永遠不會發生,因爲在頭重定向之後它將是空的。

查看關於session_start()的手冊。

此外,作爲中指出:

對於大多數數據庫,PDOStatement對象:: rowCount時()不返回受SELECT語句的行數。

以防萬一想到這一點。過去我花了很多時間在自己想着這件事上。

您可能想看一下關於rowCount的手冊上的示例#2。

當然,正如@Paul已經指出的那樣,如果遷移到PDO,則不應再使用mysql_query()

+0

服務器有session_start所有準備好我把它放到頁面上,然後得到100的錯誤的說法標題都準備好發送... – user1405062

+0

如果你對已經發送的標題感到嘮叨,它發生在這一行: 'header(「Location:home.php」);'你必須在該行之前的某處輸出到瀏覽器。 – budwiser