如何使用PHP获取脚本的当前URL? [重复]

时间:2022-10-06 12:14:08

This question already has an answer here:

这个问题在这里已有答案:

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

如何使用PHP获取脚本的当前URL?

6 个解决方案

#1


25  

Please see PHP Get Current URL:

请参阅PHP获取当前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在你的应用程序中使用它。这是我用来获取当前URL并在脚本中使用它的片段。当前的URL(无论是http还是https)现在都是一个本地变量,您可以随意使用它。

$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.

注意:您应该在使用任何敏感内容之前修饰此值,例如sql查询。

#2


3  

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.

$ _SERVER [“SCRIPT_NAME”]将为您提供当前执行的脚本路径,该路径与您的服务器相关,而不是浏览器的URL。

#3


3  

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

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

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

and if you want the current directory url:

如果你想要当前目录的网址:

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

dirname($ _ SERVER ['SCRIPT_NAME'])= / files

#4


2  

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.

如果您需要这样的信息,通常可以方便地查看phpinfo()的输出,然后阅读那些看起来不错的项目的文档。

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

请记住,其中一些可能在您的服务器上是正确的,但在其他服务器上略有不同。取决于操作系统,http服务器等。

#5


0  

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

*.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.

假设您正在访问如何使用PHP获取我的脚本的当前URL?然后回显$ _SERVER ['SERVER_NAME']; //这将返回以下主机名: - *.com/和echo $ _SERVER ['REQUEST_URI']; //它只返回当前url中的文件名,将返回“/ questions / 1871770 / how-do-i-get-the-current-url-of-my-script-using-php”而不是主机名。

#6


0  

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

//你可以通过这个来获取完整的URL

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

#1


25  

Please see PHP Get Current URL:

请参阅PHP获取当前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在你的应用程序中使用它。这是我用来获取当前URL并在脚本中使用它的片段。当前的URL(无论是http还是https)现在都是一个本地变量,您可以随意使用它。

$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.

注意:您应该在使用任何敏感内容之前修饰此值,例如sql查询。

#2


3  

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.

$ _SERVER [“SCRIPT_NAME”]将为您提供当前执行的脚本路径,该路径与您的服务器相关,而不是浏览器的URL。

#3


3  

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

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

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

and if you want the current directory url:

如果你想要当前目录的网址:

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

dirname($ _ SERVER ['SCRIPT_NAME'])= / files

#4


2  

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.

如果您需要这样的信息,通常可以方便地查看phpinfo()的输出,然后阅读那些看起来不错的项目的文档。

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

请记住,其中一些可能在您的服务器上是正确的,但在其他服务器上略有不同。取决于操作系统,http服务器等。

#5


0  

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

*.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.

假设您正在访问如何使用PHP获取我的脚本的当前URL?然后回显$ _SERVER ['SERVER_NAME']; //这将返回以下主机名: - *.com/和echo $ _SERVER ['REQUEST_URI']; //它只返回当前url中的文件名,将返回“/ questions / 1871770 / how-do-i-get-the-current-url-of-my-script-using-php”而不是主机名。

#6


0  

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

//你可以通过这个来获取完整的URL

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