40

How to get URL using Smarty similar to window.location in JavaScript ?

I did this

{$smarty.server.PHP_SELF} 

but it's not working.

GameO7er
  • 2,028
  • 1
  • 18
  • 33
user1187
  • 2,116
  • 8
  • 41
  • 74

3 Answers3

135

You can use this, too.

{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI}
sertaconay
  • 1,378
  • 1
  • 15
  • 14
  • 2
    This might cause some security issues if the user tampers the header fields: http://stackoverflow.com/questions/6768793/get-the-full-url-in-php – Möhre Aug 11 '14 at 10:11
  • 2
    yes yes it has security risk and not compatible with https. – sertaconay Aug 29 '14 at 21:32
  • also you can look at here: http://www.reddit.com/r/netsec/comments/1dhpiy/practical_http_host_header_attacks/ – sertaconay Nov 04 '14 at 15:20
5

See what is available with this:

{$smarty.server|@var_dump}

A couple examples for url https://some.example.com/some/path?param=1:

{$smarty.server.SCRIPT_URI}

https://some.example.com/some/path

{$smarty.server.SERVER_NAME}

some.example.com

{$smarty.server.SCRIPT_URL}

/some/path

Andrew
  • 18,680
  • 13
  • 103
  • 118
3

This is controller code :

<?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');

?>

You can display variable in index.tpl template file :

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

        {$current_url}

    </body>
</html>
hdvianna
  • 443
  • 5
  • 13
senthilbp
  • 807
  • 1
  • 9
  • 16