0

I need to find out dynamically the base url of a script. Let's say I have installed a script in the folder script, the base url of the script would be http://www.example.com/script, let's say I place it in the root it has to be http://www.example.com

Is there a way to find this out dynamically?

Using basename(__DIR__), or basename(__FILE__), getcwd() won't bring me the desired results.

Tried stuff similar to echo $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']); but this of course always gives back the current page url.

I want to declare a constant called ROOT_URL in my index.php (everything gets loaded by this file), so I can use this constant in any wished project.

halfer
  • 19,824
  • 17
  • 99
  • 186
David Ericsson
  • 2,570
  • 2
  • 19
  • 33
  • Possible duplicate of http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php – mvp Aug 25 '13 at 07:31

3 Answers3

1

I had to cover the same issue a few weeks ago in my MVC implementation.

There you go :

public static function RealDirname($path)
{
    $path = dirname($path);

    if($path[0] == '\\') //Windows servers tend to mess up
        $path[0] = '/';

    $lastChar = strlen($path) - 1;
    if($path[$lastChar] != '/')
        $path .= '/';

    return $path;
}

public static function GetAbsolutePath()
{
            global $config;

    if(empty($config["base_path"]))
        $config["base_path"] = self::GetDomainURL();

    return $config["base_path"].(self::RealDirname($_SERVER['PHP_SELF']));
}

public static function GetDomainURL()
{
    return (isset($_SERVER['HTTPS']) ? 'https://' :'http://').$_SERVER['HTTP_HOST'];
}

Just call getAbsolutePath, and this should return you the whole path with the URL :)

Tried on a page located at :

http://localhost:8080/old/test.php

GetAbsolutePath returned :

http://localhost:8080/old/

And GetDomainURL :

http://localhost:8080
  • m8 where is enkelDirname defined :p ? – David Ericsson Aug 25 '13 at 07:37
  • Oops, it was a typo, replace it with RealDirname ;) – Anthony Teisseire Aug 25 '13 at 07:38
  • Tried it m8, when i define the constant on index.php with your method it returns this: `http://www.domain.com/index.php/user/` when im accessing `http://www.domain.nl/index.php/user/register` It should just return `http://www.domain.com` – David Ericsson Aug 25 '13 at 07:40
  • Ok, but when i install the script lets say in a folder called `mvc` , it would still return `http://www.domain.com`, instead of `http://www.domain.com/mvc/` , thing is i could just add http://www.domain.com/rootfolder or something hard into the code, but i really like this to be dynamicly – David Ericsson Aug 25 '13 at 07:45
  • What I have provided now just returns the domain URL, http://domain.com What was posted before returned dynamically the path of the script, such as: http://domain.com/mvc/ – Anthony Teisseire Aug 25 '13 at 07:50
0

Have you tried $script_filename = $_SERVER['SCRIPT_FILENAME']; You can have a look here http://php.net/manual/en/reserved.variables.server.php

Red Cricket
  • 9,762
  • 21
  • 81
  • 166
0

Got it fixed with the help of Anthony

Changed his first submitted method a bit :

  public static function RealDirname($path)
{
    $path = dirname($path);

    if($path[0] == '\\') //Windows servers tend to mess up [everything]
        $path[0] = '/';

    $lastChar = strlen($path) - 1;
    if($path[$lastChar] != '/')
        $path .= '/';

    return $path;
}

public static function getAbsolutePath()
{
        $path = (isset($_SERVER['HTTPS']) ? 'https://' :'http://').$_SERVER['HTTP_HOST'];
        $path = $path.self::RealDirname($_SERVER['PHP_SELF']);
        return substr($path, 0, strpos($path, "index.php"));

}  

Strips out everything after the index.php and gives me back the root.

Mattios550
  • 372
  • 1
  • 5
  • 18
David Ericsson
  • 2,570
  • 2
  • 19
  • 33