Extrakun
Extrakun

Reputation: 19305

PHP: How do I get the URL a file is in?

I can't seem to find a super-global for this. Basically, if a PHP file is executed on, say, http://www.example.com/services/page.php, I would like to retrieve http://example.com/services. How do I achieve this?

Upvotes: 1

Views: 183

Answers (3)

Jeff Busby
Jeff Busby

Reputation: 2011


$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')  ? "https://" : "http://";

echo $scheme . str_replace(basename(__file__), '', $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']);


Upvotes: 0

Sinan Ünür
Sinan Ünür

Reputation: 118128

You can abuse dirname:

<p>
<?php echo $_SERVER["HTTP_HOST"] . dirname($_SERVER["REQUEST_URI"]) ?>
</p>

Upvotes: 4

Gumbo
Gumbo

Reputation: 655239

Take a look at $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI']. HTTP_HOST would contain the host name the resource was requested from an REQUEST_URI URI path and query that was requested.

Upvotes: 5

Related Questions