2017-05-25 27 views
0

我想通過HTTP_REFERER獲取前一頁的鏈接來創建後退按鈕,但是此前一頁有過濾器,它使用錨點更新網址。 目前我只是得到了簡單的網址。從鏈接中獲取與HTTP Referer的鏈接

謝謝

if($_SERVER['HTTP_REFERER'] == "http://website.com/cars/#blue-car/"):?> 
    <a href="/cars/#blue-car/"> 
      <input type="button" class="back-button"></input> 
      </a> 
+0

你有沒有嘗試過的,而不是''$ _ SERVER [ 'HTTP_REFERER']''encodeURIComponent方法(document.referrer)? – doutriforce

+0

謝謝你的回答。不知道這件事,我會調查。 – tomdes

回答

0

$_SERVER['HTTP_REFERER']很容易被欺騙,我會使用onclick="window.history.back();",即:

<input value="Go Back" type="button" onclick="window.history.back()" class="back-button"> 

go(-1)

<input value="Go Back" type="button" onclick="window.history.go(-1)" class="back-button"> 

Window.history

0

謝謝你們兩位,幫我找到合適的解決方案。 我覺得這是在其他崗位

的js

home_url = ['http://website.com/']; 

pathArray = document.referrer.split('/'); 
protocol = pathArray[0]; 
host = pathArray[2]; 

url_before = protocol + '//' + host; 

url_now = window.location.protocol + "//" + window.location.host; 


function goBackOrGoHome(){ 
if (url_before == url_now) { 
window.history.back();  
}else{ 
window.location = home_url; 
}; 
} 

的Html

<input value="Go Back" type="button" class="back-button" onclick="goBackOrGoHome()"> 
+0

該帖子丟失了我的答案的一部分。實際上,它會比較以前的網址和實際的網址,如果上一個網址來自其他網域,則會重定向到首頁。 – tomdes