Fix missing $_SERVER[SCRIPT_URL]


/ Published in: PHP
Save to your folder(s)

Many scripts rely on `$_SERVER[SCRIPT_URL]` which is sometimes missing. This function detects it from other server variables and fixes the missing field.


Copy this code and paste it in your HTML
  1. /**
  2.  * Tries to determine Servers' SCRIPT_URL, if it doesn't exist.
  3.  * The missing superglobal $_SERVER array element will be fixed.
  4.  *
  5.  * Example: client requests ...
  6.  * <samp>http://www.hostname.com/testpage.html?param=value&foo=bar</samp>
  7.  *
  8.  * ...$_SERVER['SCRIPT_URL'] is (or becomes)
  9.  * <samp>"/testpage.html"</samp>
  10.  *
  11.  * @return string
  12.  * @author Carsten Witt <[email protected]>
  13.  * @version 20100206
  14.  */
  15. function get_script_url()
  16. {
  17. $script_url = null;
  18.  
  19. if (!empty($_SERVER['SCRIPT_URL']))
  20. $script_url = $_SERVER['SCRIPT_URL'];
  21.  
  22. elseif (!empty($_SERVER['REDIRECT_URL']))
  23. $script_url = $_SERVER['REDIRECT_URL'];
  24.  
  25. elseif (!empty($_SERVER['REQUEST_URI'])) {
  26. $p = parse_url($_SERVER['REQUEST_URI']);
  27. $script_url = $p['path'];
  28. }
  29.  
  30. else die (__FILE__." / ".__FUNCTION__.':<br />Couldn\'t determine $_SERVER["SCRIPT_URL"].');
  31.  
  32. $_SERVER['SCRIPT_URL'] = $script_url;
  33.  
  34. return $script_url;
  35.  
  36. } // get_script_url()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.