2015-03-19 48 views
0
<?php 
require_once 'login.php'; 
require_once 'welcome.php'; 
$db_server = mysql_connect($db_hostname,$db_username,$db_password); 
if(!$db_server) die("Unable to connect with MySql : " . mysql_error()); 

mysql_select_db($db_database) or die("Unable to connect with db"); 


echo <<<_END 
<form action = 'ps.php' method = 'post'><pre> 
Enter your Username <input type = 'text' name = 'username'> 
Enter your Password <input type = 'text' name = 'password'> 
<input type = 'submit' value = 'Cl1ck M3'> 
</pre></form> 
_END; 

if (isset($_POST['username']) && isset($_POST['password'])) 
{ 
    //echo "Fine till here1"; 
    echo $_POST['username']." Without htmlentities <br>"; 
    $usernameP = mysql_entities_fix_string($_POST['username']); 
    if (!$usernameP) die ("No value fetched in the variable usernameP"); 

    $passwordP = mysql_entities_fix_string($_POST['password']); 
    if (!$passwordP) die ("No value fetched in the variable passwordP"); 

    $query = "SELECT * FROM hacker WHERE username = '$usernameP' AND password = '$passwordP'"; 
    $result = mysql_query($query,$db_server); 
    if(!$result) die ("Unable to execute query : " . mysql_error()); 

    $rows = mysql_num_rows($result); 

     $row = mysql_fetch_row($result); 
     echo $row[0]; 
     if ($row[0] == '$username' && $row[1] == '$passwordP') 
     { 
      echo "Credentials Authorized"; 
     } 

    function mysql_entities_fix_string($string) 
     { 
       return htmlentities(mysql_fix_string($string)); 
     } 

    function mysql_fix_string($string) 
     { 
       if (get_magic_quotes_gpc()) $string = stripslashes($string); 
       return mysql_real_escape_string($string); 
     } 
    } 
mysql_close($db_server); 

?> 

我試圖測試一個簡單的PHP頁面。我使用函數「mysql_entities_fix_string」過濾掉惡意輸入,但程序無法調用它。因此沒有值在$ usernameP或$ passwordP中獲取。 任何人都可以提出一些建議嗎?在PHP中定義的函數「mysql_entities_fix_string」沒有被調用

+2

除了條件定義問題之外,這是一種誤導性的輸入處理方法。 HTML轉義是一個模板輸出問題,當你將內容回顯爲HTML標記時,你應該使用'htmlspecialchars'來實現。 SQL轉義是一種查詢準備問題,在向SQL字符串文本中插入內容時應該這樣做。 (或者,更好的是,避免完全通過在'mysqli'或PDO中使用參數化查詢來實現SQL轉義,而不是使用已棄用的'mysql'函數。)你不能在同一個地方執行它們,內容不一致。 – bobince 2015-03-19 11:33:50

回答

1

您正在有條件地定義該功能。如果功能被定義爲例如在if聲明中,只有您的代碼在其上執行後纔可用。相反,主範圍中定義的函數(「在括號外」)是在文件的其餘部分運行之前定義的。

+0

我很朦朧,如果有人會照顧回覆。反正我是新來的PHP所以學習的東西。明白了你的觀點。重擊@Piskvor – harveyD 2015-03-19 10:14:24

-1

我嘗試了更多的東西,並意識到我已經在'if語句'中定義了函數。得到了這個功能,問題就被解決了。順便說一句,任何人都可以解釋爲什麼函數不能在這裏的'if語句'中調用?

相關問題