CoderZone.org

Category: >> PHP Code >> Fix missing $_SERVER SCRIPT_URL variable Bookmark and Share

<< lastnext >>

Snippet Name: Fix missing $_SERVER SCRIPT_URL variable

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.

Also see:
» Variable variables example
» One-liner to swap variables
» Multi-line variables and strings i...
» Get all application variables
» Read GET URL Variables Into An Ass...
» Checking Server Status with PHP
» Declaring Variables
» Call subroutine and pass variable ...
» SEQUENCE: get sequence value into ...
» Anonymous blocks - Constants and V...
» GET and POST variables in hidden f...
» Variable Speed Horiz Scroll
» Get file in as variable

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

/**
 * Tries to determine Servers' SCRIPT_URL, if it doesn't exist.
 * The missing superglobal $_SERVER array element will be fixed.
 *
 * Example: client requests ... 
 * http://www.hostname.com/testpage.html?param=value&foo=bar
 *
 * ...$_SERVER['SCRIPT_URL'] is (or becomes)
 * "/testpage.html"
 */
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()
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 7