2011-12-02 85 views
-3

可能重複:
getting current URLPHP和調用url?

我wan't使用URL(與任何GET參數等)在PHP代碼。

所以當我的網站是在網址:http:/sample.com/art/id/title我想在網站上顯示此字符串。

還有一件事......我的代碼會在具有各種網址約定的各種服務器上啓動。

+0

究竟是什麼問題? – jeroen

+0

@Shakti Singh有趣的是,我在想URL重寫,但你的猜測和我的一樣好:-) – jeroen

+0

是的,我在想URL重寫了太多了......大聲笑不太清楚 – jValdron

回答

2
// Get HTTP/HTTPS (the possible values for this vary from server to server) 
$myUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']),array('off','no'))) ? 'https' : 'http'; 
// Get domain portion 
$myUrl .= '://'.$_SERVER['HTTP_HOST']; 
// Get path to script 
$myUrl .= $_SERVER['REQUEST_URI']; 
// Add path info, if any 
if (!empty($_SERVER['PATH_INFO'])) $myUrl .= $_SERVER['PATH_INFO']; 
// Add query string, if any (some servers include a ?, some don't) 
if (!empty($_SERVER['QUERY_STRING'])) $myUrl .= '?'.ltrim($_SERVER['REQUEST_URI'],'?'); 

echo $myUrl; 

應占幾乎所有的變化,你可能會遇到的(雖然mod_rewrite的可以在未糾正的方式搞砸了,我不確定)。

3

下應該給你兩個域名和網址:

$_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 
+0

不知道REQUEST_URI。 thx –

0

你必須有一個.htaccess文件,它被稱爲URL重寫,一個例子.htaccess文件:

Options +FollowSymlinks 
RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^/index.php 
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC] 
RewriteRule (.*) index.php?query=$1&%{QUERY_STRING} 

這會將所有內容像art/id/title發送到index.php頁面,並且可以使用$ _GET ['query']進行使用。所以在index.php文件,你可以使用變量,像這樣:

echo $_GET['query']; 
+0

有時你不能訪問服務器上的htaccess文件,但這會在我遇到的另一個問題中得到幫助。 –