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

前端 未结 11 2508
北恋
北恋 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:41

    If your browser formats JSON documents nicely and no output has taken place, inserting the following will result in something more readable than var_dump:

    header('Content-Type: application/json');die(json_encode($_SERVER));
    

    phpinfo() will also provide a list of all $_SERVER values and so much more!

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

    Late late reply. I use this piece of script to store information in my db. Hope this might help someone. How to get your full url and the ip address of the client access from.

    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    //Is it a proxy address
    }elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    
    $url = $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
    
    echo $ip;
    echo "<br>";
    echo $url;
    
    0 讨论(0)
  • 2020-12-17 17:51

    If you need domain name, use:

    $_SERVER['HTTP_HOST']
    
    0 讨论(0)
  • 2020-12-17 17:52

    var_dump($_SERVER) and see what suites your needs.

    P.S. Making use of unsanitized $_SERVER["PHP_SELF"] could be a security risk.

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

    On Apache, SCRIPT_URI only exists when mod_rewrite is enabled.

    Apache requires:

    rewrite_module (note: use platform specific load syntax)

    and

    RewriteEngine On

    statements in the Apache configuration file at the appropriate locations.

    Then restart Apache, and it should work fine.

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