2

Basically, I want my script to output its absolute URL, but I don't want to statically program it into the script. For example, if my current URL is http://example.com/script.php I want to be able to store it as a variable, or echo it. i.e. $url = http://example.com/script.php;

But if I move the script to a different server/domain, I want it to automatically adjust to that, i.e. $url = http://example2.com/newscript.php;

But I have no idea how to go about doing this. Any ideas?

5 Answers 5

6
$url = "http://" . $_SERVER['HTTP_HOST'] . '/script.php';

If there's a possibility the protocol will change as well (i.e. https instead of http), use this:

$url = ($_SERVER['HTTPS'] ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . '/script.php';
Sign up to request clarification or add additional context in comments.

Comments

3

$_SERVER['HTTP_HOST'] and $_SERVER['SCRIPT_NAME'] contain this information.

UPDATE: As @Col. Shrapnel points out, SCRIPT_NAME returns the actual path of the script relative to the host, not the requested URL, which may be different if using URL rewrite. Also, unlike REQUEST_URI, it doesn't include the possibly appended variables.

Note that SCRIPT_NAME is equivalent in content to PHP_SELF, the difference is that:

SCRIPT_NAME is defined in the CGI 1.1 specification, and is thus a standard. However, not all web servers actually implement it, and thus it isn't necessarily portable. PHP_SELF, on the other hand, is implemented directly by PHP, and as long as you're programming in PHP, will always be present.

3 Comments

This solved it best, mostly because of $_SERVER['SCRIPT_NAME']; Thanks
@Rob say bye-bye to the mod_rewrite and query string
Indeed, if you want to keep it portable REQUEST_URI more of a friend.
1

by bet (:

$_SERVER['HTTP_HOST'] and $_SERVER["REQUEST_URI"];

however, $_SERVER['HTTP_PORT'] and $_SERVER['HTTPS'] could be used in the critical case

however, most of time you do not need all of these, save for $_SERVER["REQUEST_URI"]
because browser knows the rest already: port, host and everything.

Comments

0

Try using

$url = "http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";

Comments

0

I have a library that helps me do this across webservers and is also agnostic to mod_rewrite.

The library is called Bombay: http://github.com/sandeepshetty/bombay

To use it you need to do this:

<?php

    require '/path/to/bombay.php';
    requires ('uri');

    echo absolute_uri('script.php');

    //prints http://example.com/script.php if hosted on example.com and accessed over http
    //prints https://example2.com/script.php if hosted on example2.com and accessed over https

?>

You could also study the code, and take what you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.