2015-10-18 109 views
-2

我想使用PHP加載外部網頁,並在顯示它之前注入一些JavaScript。使用PHP注入JavaScript

說實話,我不知道該怎麼做(如果可能的話)。有人?

例子:

$html = file_get_contents($url); 
// inject javascript here 
echo $html; 
+0

我想顯示它之前注入一些JavaScript?就像XSS攻擊一樣? –

+0

你嘗試了什麼? –

回答

0

簡單地echo它

$html = file_get_contents($url); 
echo "your javascript here"; 
echo $html; 
1

你需要簡單地構建你的js代碼的字符串,如果它僅用於該頁面並添加腳本標籤和echo內整個字符串。像這樣:

echo "<script>alert('hi');</script>"; //as page script example 

或者,如果它是一個文件包括然後用script標籤和echo它包括得當,這將是提供您的網頁上。像這樣:

echo "<script src='path to file'></script>"; 

在這種情況下,你的代碼結構會變成這個樣子

$html = file_get_contents($url); 
    echo "<script src='path to file'></script>"; 
    // echo "<script>alert('hi');</script>"; //as page script example 
    echo $html; 
+0

請解釋您的答案,因爲它被標記爲低質量。 –

+0

@RohitGupta我們到底在哪裏可以看到這些標誌。 –

+0

我不知道如何再次找到它。但是當我遇到它時,我正在審查低質量隊列。而且我正在給你一個提醒,而不是建議刪除。 –