How to get the URL of the currently executing PHP script?
How do I get the URL of the currently executing PHP script?
For exam开发者_运维知识库ple, the URL http://www.domain.com/folder/script.php
sends a request to /var/www/domain/folder/script.php
.
In script.php
, I want to echo http://www.domain.com/folder/
(not /var/www/domain/folder/
, not http://www.domain.com/folder/script.php
). What is the simplest way to do this?
A simple Google search would have answered your question. Here's the result.
http://php.about.com/od/learnphp/qt/_SERVER_PHP.htm
It is not that sophisticated, but here it goes:
<?php
$scriptFolder = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) ? 'https://' : 'http://';
$scriptFolder .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']);
echo $scriptFolder;
?>
I was looking for an answer for this question, it seems there isn't an easy one.
Then it hit me: it is because it is not always possible to get the current script url.
PHP can include a file anywhere in the file system, as long as the user running the process has access to it, it will work. But then, that means it is possible to include scripts outside the webserver's root directory - hence impossible to have a url. I believe that is the reason there is not straight forward function to do that.
精彩评论