开发者

How do I get the current URL of my script using PHP? [duplicate]

This question already has answers here: 开发者_JAVA技巧 getting current URL (2 answers) Closed 7 years ago.

How do I get the current URL of my script using PHP?


Please see PHP Get Current URL:

Sometimes it is not as straightforward as one may think to get the current url to use it inside your application. Here is a snippet that I use to fetch the current URL and use it in a script. The current url (whether http or https) is now a local variable that you can do with as you please.

$url = "http".(!empty($_SERVER['HTTPS'])?"s":"").
"://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

Note: You should groom this value before using in anything sensitive, like a sql query.


For www.example.com/files/script.php

$_SERVER['SCRIPT_NAME'] = /files/script.php

and if you want the current directory url:

dirname($_SERVER['SCRIPT_NAME']) = /files


If you want to know the script name on your server, then..

$file = $_SERVER["SCRIPT_NAME"];
echo $file;

$_SERVER["SCRIPT_NAME"] will give you the current executed script path, which related on your server not on the browser's url.


Whe you need info like this its often convenient to look at the output of phpinfo() and then read the docs on those items that seems good.

Keep in mind that some of them might be correct on your server, but slightly different on others.. Depending on os, http server etc.


suppose you were accessing

How do I get the current URL of my script using PHP?

then echo $_SERVER['SERVER_NAME']; // This will return u the hostname that is:-

stackoverflow.com/

and echo $_SERVER['REQUEST_URI']; // It will return only the file name from current url will return below

"/questions/1871770/how-do-i-get-the-current-url-of-my-script-using-php"

and not the hostname.


Hi this is the solution of your problem

//fetch page url by this

$url=$_SERVER['REQUEST_URI'];
echo "$url<br />";

//it will print

//fetch host by this

$host=$_SERVER['HTTP_HOST'];
echo "$host<br />";

//you can fetch full url by this

$fullurl="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $fullurl;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜