Reputation: 10961
How do I get the URL of the currently executing PHP script?
For example, 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?
Upvotes: 0
Views: 2927
Reputation: 111
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.
Upvotes: 0
Reputation: 39704
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;
?>
Upvotes: 2
Reputation: 1608
A simple Google search would have answered your question. Here's the result.
http://php.about.com/od/learnphp/qt/_SERVER_PHP.htm
Upvotes: 2