Return to Snippet

Revision: 23411
at February 6, 2010 16:24 by drwitt


Updated Code
/**
 * Tries to determine Servers' SCRIPT_URL, if it doesn't exist.
 * The missing superglobal $_SERVER array element will be fixed.
 *
 * Example: client requests ... 
 * <samp>http://www.hostname.com/testpage.html?param=value&foo=bar</samp>
 *
 * ...$_SERVER['SCRIPT_URL'] is (or becomes)
 * <samp>"/testpage.html"</samp>
 *
 * @return string 
 * @author Carsten Witt <[email protected]>
 * @version 20100206
 */
function get_script_url() 
{
    $script_url = null;

    if (!empty($_SERVER['SCRIPT_URL']))   
        $script_url = $_SERVER['SCRIPT_URL'];

    elseif (!empty($_SERVER['REDIRECT_URL'])) 
        $script_url = $_SERVER['REDIRECT_URL'];

    elseif (!empty($_SERVER['REQUEST_URI'])) {
        $p = parse_url($_SERVER['REQUEST_URI']);
        $script_url = $p['path'];
    }
    
    else die (__FILE__." / ".__FUNCTION__.':<br />Couldn\'t determine $_SERVER["SCRIPT_URL"].');

    $_SERVER['SCRIPT_URL'] = $script_url;
    
    return $script_url;
    
} // get_script_url()

Revision: 23410
at February 6, 2010 16:05 by drwitt


Initial Code
/**
 * Tries to determine Servers' SCRIPT_URL, if it doesn't exist.
 * The missing superglobal $_SERVER array element will be fixed.
 *
 * Example: client requests ... 
 * <samp>http://www.hostname.com/testpage.html?param=value&foo=bar</samp>
 *
 * ...$_SERVER['SCRIPT_URL'] is (or becomes)
 * <samp>"/testpage.html"</samp>
 *
 * @return string 
 * @author Carsten Witt <[email protected]>
 * @version 20100206
 */
function get_script_url() 
{
    $script_url = null;

    if (!empty($_SERVER['SCRIPT_URL']))   
        $script_url = $_SERVER['SCRIPT_URL'];

    elseif (!empty($_SERVER['REDIRECT_URL'])) 
        $script_url = $_SERVER['REDIRECT_URL'];

    elseif (!empty($_SERVER['REQUEST_URI'])) {
        $p = parse_url($_SERVER['REQUEST_URI']);
        $script_url = $p['path'];
    }
    
    else die (__FILE__." / ".__FUNCTION__.':<br />Couldn't determine $_SERVER["SCRIPT_URL"].');

    $_SERVER['SCRIPT_URL'] = $script_url;
    
    return $script_url;
    
} // get_script_url()

Initial URL

                                

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

Initial Title
Fix missing $_SERVER[SCRIPT_URL]

Initial Tags
url, php, server, script, function

Initial Language
PHP