2012-10-30 26 views
27

如何使用smarty获取网址(类似于JavaScript中的window.location)?如何使用smarty获取当前网址

我这样做

{$smarty.server.PHP_SELF} 

,但它不工作。给我一个解决方案。

+0

你得到了什么输出? – senthilbp

+0

控制器,我得到了 – Athi

回答

98

你可以用这个。

{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI} 
+2

这应该是最好的答案! – shawndreck

+0

我同意。它快速而坚实。 – cptnk

+1

如果用户篡改头字段,这可能会导致一些安全问题:http://stackoverflow.com/questions/6768793/get-the-full-url-in-php –

4

这是控制器代码:

<?php 
require_once('libs/Smarty.class.php'); 
$smarty = new Smarty(); 
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") { 
    $pro = 'https'; 
} else { 
    $pro = 'http'; 
} 
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
$current_url = $pro."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; 

$smarty->assign("current_url",$current_url); 
$smarty->display('index.tpl'); 

?> 

您可以在index.tpl模板文件显示变量:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
     <title>Title</title> 
    </head> 
    <body> 

     {$current_url} 

    </body> 
</html> 
+0

我不能使用这里的PHP – Athi

+0

没有使用控制器代码,我们不能做? – Athi

+0

这就是简单的PHP文件,不用担心,你可以直接使用这个代码 – senthilbp