How can I find an application's base url?

后端 未结 11 1392
南方客
南方客 2020-12-16 01:35

I\'d like to find the base url of my application, so I can automatically reference other files in my application tree...

So given a file config.php in the base of my

11条回答
  •  离开以前
    2020-12-16 02:01

    url root of the application can be found by

    $protocol = (strstr('https',$_SERVER['SERVER_PROTOCOL']) === false)?'http':'https';
    $url = $protocol.'://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['REQUEST_URI']);
    

    this code will return url path of any application whether be online or on offline server.

    I've not made proper check for proper '/'. Please modify it for slashes.

    this file should be placed in root.

    example : if application url :

    http://localhost/test/test/test.php
    

    then this code will return

    http://localhost/test/test
    

    Thanks

提交回复
热议问题