Get current URL path in PHP

2019-01-07 19:07发布

I need to get the path from the URL of the current request. For example, if the current URL is:

"http://www.example.com/example/test/hi.php?randomvariable=1"

I would want this:

"/example/test/hi.php?randomvariable=1"

标签: php url
4条回答
淡お忘
2楼-- · 2019-01-07 19:34
                <?php
                function current_url()
                {
                    $url      = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                    $validURL = str_replace("&", "&amp", $url);
                    return $validURL;
                }
                //echo "page URL is : ".current_url();

                $offer_url = current_url();

                ?>



                <?php

                if ($offer_url == "checking url name") {
                ?> < p > hi this is manip5595 < /p>

                <?php
                }
                ?>
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-07 19:38

You want $_SERVER['REQUEST_URI']. From the docs:

'REQUEST_URI'

The URI which was given in order to access this page; for instance, '/index.html'.

查看更多
等我变得足够好
4楼-- · 2019-01-07 19:38

$_SERVER['REQUEST_URI'] will give you the path after the domain name. In your example "/example/test/hi.php?randomvariable=1"

查看更多
趁早两清
5楼-- · 2019-01-07 19:47

it should be :

$_SERVER['REQUEST_URI'];

Take a look at : Get the full URL in PHP

查看更多
登录 后发表回答