2012-06-09 60 views
0

我只想在參數中獲取URL字符串。 所以我下面的編碼:如何在PHP中獲取整個URL字符串(包括&,?)?

//In my HTML file. 
<script> 
document.write('<iframe id="ifr1" 
src="http://{domain}/prj.php?url=https://www.google.co.kr/search?sugexp=chrome,mod=3&sourceid=chrome&ie=UTF-8&q=url+utf8+encoding#sclient=psy-ab&hl=ko&newwindow=1&prmdo=1&tbm=klg&q=google&oq=google&aq=f&aqi=g10&aql=&gs_l=serp.3..0l10.2207859.2208404.3.2208459.6.4.0.1.1.3.496.1353.2-2j1j1.4.0...0.0.O1lbexylHQM&pbx=1&prmdo=1&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=b8fee9fdb4e7f90&biw=1440&bih=828&a=' +'asdf'+" width="100%" height="100%" frameborder="0"> </iframe>'); 
</script> 

//In my php file 
$message = $_GET['url']; 
echo("$message"); 

但我只得到 'https://www.google.co.kr/search?sugexp=chrome,mod=3'。

怎麼了?

我想獲取整個URL字符串。包括&,?或其他一切。

回答

0

原因是您使用的是另一種編碼的編碼。

第一個是:

prj.php?url=[somedata] 

其中[somedata]是與其它鍵/值集合的URL:

https://url/etc/search?key1=value1&key2=value2 

如果URL參數不url編碼有沒有辦法區分/prj.php的參數與/search的參數,假定全部用定義的參數10語法全部屬於第一頁。

訣竅是url-encode url參數,但是你怎麼做取決於上下文。

如果網址是通過PHP以動態方式生成的,請使用urlencode()函數。

如果您的網址是硬編碼(通過PHP不生成),你可以自己寫一點PHP工具來完成這項工作,或使用在線編碼器一樣http://www.opinionatedgeek.com/dotnet/tools/urlencode/Encode.aspx

1

您需要使用encodeURIComponent來編碼url參數。

+0

Downvoted,因爲我覺得這是不正確的,這取決於上下文。如果有動力,我願意討論這個問題並取消downvote。 – Cranio