2011-06-03 42 views
0

我在第x.php頁?user = john中使用swfobject嵌入了flash播放器。玩家調用xml文件content.php來獲得結果。在content.php我有$ _GET ['user']。我試圖從網址ID獲取用戶名並根據此結果獲取結果。然而,我得到了500錯誤。我不認爲content.php能夠訪問user變量。如果我只是把用戶名「約翰」,而不是$ _GET ['用戶'],那麼它的作品。我怎樣才能得到這個與$ _GET [ '用戶']

XML文件content.php看起來像這樣

$sql = 'SELECT a.videote as videote, b.user_name as user_name'. 
' FROM '.$video.' as a,'.$users.' as b'. 
' where b.user_name=$_GET['user'] and... //if i replace $_GET['user'] with john then it works 

在x.php閃光燈內嵌這樣

<script type="text/javascript"> 
    var flashvars = {}; 
    var so = new SWFObject("play2.swf", "sotester", "1000", "400", "8", "#000000"); 
    so.addParam("allowFullScreen", "true"); 
    so.addParam("scale", "noscale"); 
    so.addParam("menu", "false"); 
    so.write("flashcontent"); 
</script> 

工作我的播放器腳本當然指向content.php這不是問題

xmlData.load("contentp.php"); 
+0

你可以發佈相關的代碼位?另外,加載x.php或content.php時是否返回服務器錯誤? – GargantuChet 2011-06-03 01:58:31

+0

@GargantuChe檢查更新的文章 – Pinkie 2011-06-03 02:09:26

+0

@GargantuChe也500錯誤是由content.php而不是x。php – Pinkie 2011-06-03 02:10:07

回答

2

Pinkie,感謝您發佈的代碼。

$sql = 'SELECT a.videote as videote, b.user_name as user_name'. 
    ' FROM '.$video.' as a,'.$users.' as b'. 
    ' where b.user_name=$_GET['user'] and... //if i replace $_GET['user'] with john then it works 

這裏有幾個問題,但我們可以通過它們。

更改字符串時會引入語法錯誤。你有正確的想法$video$users。但是,在添加$_GET['user']時,PHP認爲第一個撇號正在結束當前字符串。

考慮以下因素:

' where b.user_name=$_GET['user'] and...' 

看起來像兩個字符串,由單詞 「用戶」 分隔:

' where b.user_name=$_GET[' user '] and...' 

這不是正確的語法,所以返回500錯誤。我猜,如果你想你的錯誤會消失:

' where b.user_name=' . $_GET['user'] . ' and...' 

下一個問題是,如果用戶要發送一個精心製作的價值,爲「用戶」的參數,它們可能會導致查詢的行爲以你不打算的方式。

試試這個:創建一個名爲的login.php文件,包含以下內容:

<?php 
    // Just display the output; no HTML formatting needed 
    header("Content-Type: text/plain"); 

    // This must succeed, or mysql_real_escape_string() won't have any effect 
    mysql_connect('mysql_host', 'mysql_user', 'mysql_password') 
     OR die(mysql_error()); 

    $safeQuery = 'SELECT count(*) FROM users WHERE user=\'' . mysql_real_escape_string($_GET['username']) . '\' AND pass=\'' . mysql_real_escape_string($_GET['password']) . '\';'; 
    echo " safeQuery is: $safeQuery\n"; 

    $unsafeQuery = 'SELECT count(*) FROM users WHERE user=\'' . $_GET['username'] . '\' AND pass=\'' . $_GET['password'] . '\';'; 
    echo "unsafeQuery is: $unsafeQuery\n"; 
?> 

負載login.php?username=bob&password=sample。輸出看起來合理的:

safeQuery is: SELECT count(*) FROM users WHERE user='bob' AND pass='sample'; 
unsafeQuery is: SELECT count(*) FROM users WHERE user='bob' AND pass='sample'; 

現在嘗試加載login.php?username=bob&password=sample' OR 'hello'='hello"

safeQuery is: SELECT count(*) FROM users WHERE user='bob' AND pass='sample\' OR \'hello\'=\'hello'; 
unsafeQuery is: SELECT count(*) FROM users WHERE user='bob' AND pass='sample' OR 'hello'='hello'; 

的安全查詢將返回零,除非你有一個叫鮑勃其密碼真的是sample' OR 'hello'='hello"用戶。

但是,不安全的版本將返回數據庫中用戶的總數。 WHERE子句現在是:

WHERE user='bob' AND pass='sample' OR 'hello'='hello' 

OR 'hello'='hello'將在所有情況下,條件爲真,即使bob不存在或具有比其他sample密碼。

部分查詢甚至可能被註釋掉。嘗試login.php?username=bob' --

safeQuery is: SELECT count(*) FROM users WHERE user='bob\' --' AND pass=''; 
unsafeQuery is: SELECT count(*) FROM users WHERE user='bob' --' AND pass=''; 

password參數現在被忽略,因爲它嵌入在SQL註釋中。

因此,即使您只是執行SELECT語句,如果其輸入未被轉義,結果也可以由聰明的用戶操縱。

您可以使用mysql_real_escape_string來防止這些不良值。此功能將在必要時添加反斜槓,以防止輸入數據作爲SQL執行。

$sql = 'SELECT a.videote as videote, b.user_name as user_name'. 
    ' FROM '.$video.' as a,'.$users.' as b'. 
    ' where b.user_name=\'' . mysql_real_escape_string($_GET['user']) . '\' and...'; 

實施例1從php.net page for mysql_real_escape_string具有使用sprintf的一個很好的例子:

$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'", 
    mysql_real_escape_string($user), 
    mysql_real_escape_string($password)); 

每個%s被替換爲參數(在它們的順序依次指定)。這樣可以更輕鬆地保持查詢的可讀性,同時防止錯誤的輸入數據。

+0

太好了。你說對了。連鎖是絕對的問題。隨着我做的其他事情,它現在起作用。 – Pinkie 2011-06-03 18:52:51

+0

很高興幫助!感謝您的反饋。 – GargantuChet 2011-06-03 18:59:11

+0

我只是想知道爲什麼我需要使用mysql_real_string_escape知道我只選擇和不插入。你能澄清一下嗎? – Pinkie 2011-06-04 01:06:59

1

所以,如果我正在讀這個相關當用戶訪問x.php?user = john頁面時,服務器正在返回包含Flash對象的頁面。該flash對象反過來嘗試加載content.php中的xml頁面。你是正確的,content.php不能訪問x.php中的任何$ _GET變量,因爲它們是兩個完全不同的請求。

有兩件事情你可以做:

  1. 放$ _GET [「用戶」]內容到頁面作爲<PARAM>,然後讓Flash對象添加到其請求。
  2. 讓Flash使用技術from this other article從查詢字符串中獲取信息。

雖然我覺得這個值得警告,但請記住在使用前仔細檢查查詢字符串的內容,以免最終導致CSS或SQL注入漏洞。

+0

@JoshuaRogers你能澄清你的答案嗎?我如何將我的$ _GET作爲以及我如何將flash對象添加到請求中。我不會問是否知道答案。 – Pinkie 2011-06-03 02:18:09

+0

在x.php中,添加so.addParam(「user」,「<?php print urlencode($ _ GET ['user'])?>」);在你的.as文件中,你可以改變代碼來加載,如下所示:var user:String = swfobject.getQueryParamValue(「user」); xmlData.load(「content.php?user =」+ user);但是,您應該知道,上面的代碼易受SQL注入的影響。用戶是否應該添加?user =「Jim」;從用戶刪除,你可能會發現用戶表擦乾淨。 [http://php.net/manual/en/function.addslashes.php/]我建議PHP的addslashes。 – JoshuaRogers 2011-06-03 02:34:52

+0

此外,看起來content.php失敗了,因爲應該在SQL中的$ _GET ['user']周圍引用引號。 – JoshuaRogers 2011-06-03 02:37:08

1

所以約書亞絕對有正確的想法,但讓我看看我是否可以澄清和擴大一點。

本質上你應該通過GET變量閃光使用閃光變量參數(你可以閱讀它的一個很好的教程here)。一旦閃存有變量,它應該追加content.php的查詢與?yourNewGetVariable=ValuePassedOnToFlash,約書亞就在你傳遞該值之前,你應該肯定sanitize它和encode它的URL。然後你的content.php文件應該沒有問題訪問yourNewGetVariable

+0

您引用我的鏈接不使用swfobject。根據@JoshuaRogers的幫助,我增加了so.addParam(「user」,「<?php打印urlencode($ _ GET ['user'])?>」);'作爲一個swfobject參數,並添加到我的.as文件中'var user:String = swfobject.getQueryParamValue(「user」);'並修改'xmlData.load(「content.php?user =」+ user);'在我的sql查詢中我有'b.user_name = \'$ _GET [「user」] \''現在當我運行播放器查看請求日誌時,我看到'http://domain.com/contentp.php?user = undefined'。沒有得到結果。任何想法,爲什麼我得到undefined。 – Pinkie 2011-06-03 03:40:50

+0

@Pinkie,對不起,不好的鏈接,只要你的新問題,我相信(但我不是專家),變量應該通過使用flashvars通過書面[這裏](http://stackoverflow.com/問題/ 2899765/how-to-pass-php-variable-as-flashvars-via-swfobject)和[here](http://www.flashmagazine.com/tutorials/detail/passing_parameters_to_flash_using_javascript/) – Tomas 2011-06-03 11:08:22