2016-04-13 22 views
-1

我想綁定PHP變量中的完整URL。如何將url綁定到php中的變量

我的URL如下所示:http://develop.example.com/spf#users/admin

要獲取URL我使用下列內容:

http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI] 

和哈希值是使用此JS:

document.write(window.location.hash); 

所以我的PHP變量看起來象下面這樣:

$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"."<script>document.write(window.location.hash);</script>"; 

當我回應$current_url我得到這個輸出:http://develop.example.com/spf#users/admin

現在我想對當前的URL進行檢查:

if ($current_url != "http://develop.example.com/spf#users/admin") { 
    echo "you are NOT the admin"; 
} 
else { 
    echo "you are the admin"; 
} 

不幸的是,即使網址是完全一樣的,他一直掛在:「你是不是管理員」。

這裏怎麼回事?

+0

但是你寫的'$ current_url'包含'http://develop.example.com/spf '顯然與'http:// develop.example.com/spf#users/admin'不一樣。或者我不明白你想要做什麼。 –

+0

是的,這可能是問題:我想要將哈希(#users/admin)綁定到php變量。我怎樣才能做到這一點?該JavaScript應該被執行並且該值應該在php變量 –

+1

什麼?怎麼樣?在PHP執行完成並且Web服務器進程結束之後很長時間,瀏覽器就會執行JavaScript。您希望如何使用PHP執行該JavaScript?它不知道任何關於這一點。您應該瞭解客戶端 - 服務器如何工作,這對於開發Web應用程序至關重要。 –

回答

0

你可以使用下面的腳本,你的代碼是正確的,但問題是「document.write(window.location.hash);」這個代碼刪除,然後運行

<?php 
    $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 


    $match="http://localhost:900/at/"; 
    // echo base64_encode($current_url); 

    // echo "<br>"; 

    // echo base64_encode($match) ; 


    if ($current_url==$match) { 

     echo "you are the admin"; 

    } 
    else { 

    echo "you are NOT the admin"; 
    } 


    ?> 
相關問題