$_SERVER[“SCRIPT_URI”] not working? alternative?

前端 未结 11 2507
北恋
北恋 2020-12-17 17:17

This is odd, but for some reason the $_SERVER[\"SCRIPT_URI\"] will not return the domain name when I am in child/sub-pages but will only work on the main page.

相关标签:
11条回答
  • 2020-12-17 17:28

    This might be due to URL rewriting, you can try $_SERVER['REQUEST_URI'] instead if you want the path that was called in the url.

    0 讨论(0)
  • 2020-12-17 17:29

    Also, SCRIPT_URI does not exist, the right syntax is REQUEST_URI:

    echo $_SERVER["REQUEST_URI"]; It returns the whole path without the host and domain.

    0 讨论(0)
  • 2020-12-17 17:31

    When in doubt

    var_dump($_SERVER);
    
    0 讨论(0)
  • 2020-12-17 17:35

    I was using $_SERVER[SCRIPT_URI] on my website http://www.a2zidx.com and a number of subdirectories like http://howto.a2zidx.com

    I have had no problem with this for years and today got an error on a number of sites where $_SERVER[SCRIPT_URI] was not assigned. After contacting my isp they claim they have not made any changes but $_SERVER[SCRIPT_URI] no longer works. getenv('SCRIPT_URI') does not fail but returns a null string. Why this should happen suddenly after so many years I do not know. I ended up calling a function to go through various options to extract the filename which is what I wanted. Hope this covers everything. I had trouble including the function but checked "SCRIPT_NAME" "PHP_SELF" "SCRIPT_FILENAME "SCRIPT_URI"

    Hope his helps.

    If anyone knows why SCRIPT_URI would suddenly stop working I would love to know. The server is currently running Apache 2.4.

    0 讨论(0)
  • 2020-12-17 17:37

    SCRIPT_URI is not fond because it has no any value when your site run with root domain. it only come when you working with sub-directory.

    HTTP_HOST is not good idea when we working with sub-directory use following code to get SCRIPT_URI :

    $SCRIPT_URI = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($_SERVER['SCRIPT_URL']),'/');
    
    0 讨论(0)
  • 2020-12-17 17:38

    Depending on what you want, I'd use one of the following:

    1. $_SERVER['PHP_SELF'] for the script file location
    2. $_SERVER['SERVER_NAME'] for the host name

    From the php docs

    EDIT: Maybe PHP_SELF isn't the best. See comments.

    0 讨论(0)
提交回复
热议问题