如何使用Smarty获取当前URL

40

如何使用 Smarty 获取类似于 JavaScript 中的 window.locationURL

我已经这样做了

{$smarty.server.PHP_SELF} 

但它没有起作用。

3个回答

135

你也可以使用这个。

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

2
这应该是最好的答案! - shawndreck
我同意。它快速而且稳定。 - cptnk
2
如果用户篡改标题字段,这可能会导致一些安全问题:https://dev59.com/imw15IYBdhLWcg3wMY6L - Möhre
2
是的,它存在安全风险,并且不兼容https。 - sertaconay
你可以在这里查看:http://www.reddit.com/r/netsec/comments/1dhpiy/practical_http_host_header_attacks/ - sertaconay
太棒了,正是我想要的。谢谢。 - Jodyshop

5

3

这是控制器的代码:

<?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>

这是一个简单的PHP文件,不需要担心,您可以直接使用这段代码。 - senthilbp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接