Home Backend Development PHP Tutorial CI framework source code reading---------URI.php_PHP tutorial

CI framework source code reading---------URI.php_PHP tutorial

Jul 14, 2016 am 10:09 AM
defined direct php s frame Source code read

[php]  

/** 
 * CodeIgniter 
 * 
 * An open source application development framework for PHP 5.1.6 or newer 
 * 
 * @package     CodeIgniter 
 * @author      ExpressionEngine Dev Team 
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc. 
 * @license     http://codeigniter.com/user_guide/license.html 
 * @link        http://codeigniter.com 
 * @since       Version 1.0 
 * @filesource 
 */  
  
// ------------------------------------------------------------------------  
  
/** 
 * URI Class 
 * 
 * Parses 解析 URIs and determines routing 
 * 
 * @package     CodeIgniter 
 * @subpackage  Libraries 
 * @category    URI 
 * @author      ExpressionEngine Dev Team 
 * @link        http://codeigniter.com/user_guide/libraries/uri.html 
 */  
class CI_URI {  
  
    /**
* List of cached uri segments
* Cache uri segment list
* @var array
* @access public
*/  
    var $keyval         = array();  
    /**
* Current uri string
* Current uri string
* @var string
* @access public
*/  
    var $uri_string;  
    /**
* List of uri segments
* uri segment list
* @var array
* @access public
*/  
    var $segments       = array();  
    /**
* Re-indexed list of uri segments
* Starts at 1 instead of 0
* Re-index the rui segment list starting from 1
* @var array
* @access public
*/  
    var $rsegments      = array();  
  
    /** 
     * Constructor 
     * 
     * Simply globalizes the $RTR object.  The front 
     * loads the Router class early on so it's not available 
     * normally as other classes are. 
     * 
     * @access  public 
     */  
    function __construct()  
    {  
        $this->config =& load_class('Config', 'core');  
        log_message('debug', "URI Class Initialized");  
    }  
  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Get the URI String 
     * 
     * @access  private 
     * @return  string 
     */  
    function _fetch_uri_string()  
    {  
        // 下面的uri_protocol是在config.php里面的一个配置项,  
        // 其实是问你用哪种方式去检测uri的信息的意思,  
// The default is AUTO, automatic detection, that is, detection through various methods until it is detected, or all methods are detected. .
if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
{
// Is the request coming from the command line?
// Start trying various methods, mainly: command line, REQUEST_URI, PATH_INFO, QUERY_STRING.
                                                                                                                                                                                                                                                                                                   
// After the filtration and trimming value, the value of $ this-& gt; uri_string is temporarily understood as assignment.
                                       // If the script is run in command line mode, the parameters are passed through $_SERVER['argv']. Below
// $this->_parse_cli_args(); is to get some parameters related to routing that meet our needs
// If you do not use the command line to execute the script, the IF below can not be controlled for the time being.
                         // At this time we found that the URI class uses the function php_sapi_name() to test different environments
                                                                                                                                                                                                                                                                                               
// The result of the output output in CGI mode is "CGI-FCGI"
                                                                                                                                                                 
if (php_sapi_name() == 'cli' or defined('STDIN'))
                                                                 
$this->_set_uri_string($this->_parse_cli_args());
            return;                          
      }  
                                                                                                                                                                                                                                                                                                   
// Find uri
if ($uri = $this->_detect_uri())
                                                                 
// If you find the URI setting $ this- & gt; uri_string
$this->_set_uri_string($uri);
            return;                          
      }  
// Is there a PATH_INFO variable?
// Note: some servers seem to have trouble with getenv() so we'll test it two ways
// Get Path $ _Server ['PATH_INFO'] is not every request.
$path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
              if (trim($path, '/') != '' && $path != "/".SELF)  
                                                                 
$this->_set_uri_string($path);
            return;                          
      }  
// No PATH_INFO?... What about QUERY_STRING?
             // If $_SERVER['PATH_INFO'] is not found we use QUERY_STRING
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
          if (trim($path, '/') != '')
                                                                 
$this->_set_uri_string($path);
            return;                            
      }  
// As a last ditch effort lets try using the $_GET array
                      // If neither PATH_INFO nor QUERY_STRING is found, we can only use $_GET
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
                                                                 
$this->_set_uri_string(key($_GET));
            return;                            
      }  
// We've exhausted all our options...
// After the above efforts, we have not found URI, so we really can't find
$this->uri_string = '';
return;
} }
// I have rewritten it here to get uri_protocol. In fact, I think it is completely possible to get it only once. . .
$uri = strtoupper($this->config->item('uri_protocol'));
// The following is to choose different methods to obtain uri according to different methods
if ($uri == 'REQUEST_URI')
{
$this->_set_uri_string($this->_detect_uri());
return;
} }
elseif ($uri == 'CLI')
{
$this->_set_uri_string($this->_parse_cli_args());
return;
} }
// If the uri_protocol you define is outside the three methods of AUTO REQUEST_URI CLI, execute the following paragraph.
$path = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
$this->_set_uri_string($path);
}
//------------------------------------------------ -----------------------
/** 
     * Set the URI String 
     * 
     * @access  public 
     * @param   string 
     * @return  string 
     */
function _set_uri_string($str)
{
// Filter out control characters
// Filter string remove_invisible_characters function is in common.php
$str = remove_invisible_characters($str, FALSE);
// If the URI contains only a slash we'll kill it
// If the string contains only one /, clear it
$this->uri_string = ($str == '/') ? '' : $str;
}
//------------------------------------------------ -----------------------
/**
* Detects the URI
* Search uri
* This function will detect the URI automatically and fix the query string
* if necessary. Necessities
* This function will automatically find the uri and fix the query string if necessary.
*
* @access private
* @return string
*/
private function _detect_uri()
{
// If one of the two values ​​is missing, return (the two variables come from the web server. When encountering some special server programs, this may be empty.)
if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
{
            return '';                                      
} }
// Get uri
$uri = $_SERVER['REQUEST_URI'];
// If the first occurrence of SCRIPT_NAME in $uri is 0
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
{
                                                                                                                                                                                                                                                                                                   
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
} }
// The effect here is the same as above, except that $_SERVER['SCRIPT_NAME'] is replaced by
// dirname($_SERVER['SCRIPT_NAME'])
elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
{
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
} }
// This section ensures that even on servers that require the URI
// to be in the query string (Nginx) a correct correct
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
// This part ensures that the uri can be found correctly even on the Nginx server, and also fixes the QUERY_STRING server and $_GET array.
// Determine whether the first two characters of $uri are the same?/
if (strncmp($uri, '?/', 2) === 0)
{
                                      // Remove the first two characters
$uri = substr($uri, 2);
} }
// Split the string using the original book
$parts = preg_split('#?#i', $uri, 2);
$uri = $parts[0];
// If two paragraphs can be divided through the above regular regularity, is it via Query_String? Route access in the form of
if (isset($parts[1]))
{
$_SERVER['QUERY_STRING'] = $parts[1];
                           // The function parses the query string into the $_GET variable.
parse_str($_SERVER['QUERY_STRING'], $_GET);
} }
else
{
$_SERVER['QUERY_STRING'] = '';
$_GET = array();
} }
// If it is /, or empty, there are two situations, or it is through query_string? Route access in the form of
// So at this time $parts[0] is equal to the following two possibilities, and at the same time we
// If you have obtained the information you want through $parts[1], you can return.
// Or it is in the form of a segment, but the segment information is empty, that is, accessing the entry file directly without
// Any routing information passed can also be returned directly.
if ($uri == '/' || emptyempty($uri))
{
            return '/';                                            
} }
//Return the path part of this url.
$uri = parse_url($uri, PHP_URL_PATH);
// Do some final cleaning of the URI and return it
// Replace // ../ in uri with / Return
return str_replace(array('//', '../'), '/', trim($uri, '/'));
}
//------------------------------------------------ -----------------------
/**
* Parse cli arguments
* Parse cli parameters
* Take each command line argument and assume it is a URI segment.
* If you do this in the command line
* php d:/wamp/www/CodeIgniter/index.php welcome index
* _parse_cli_args() returns a string of /welcome/index
*
* @access private
* @return string
*/
private function _parse_cli_args()
{
// Returns the parameters passed when running in command line mode.
// Because the first parameter is the current file name, we want to get it starting from the second one.
$args = array_slice($_SERVER['argv'], 1);
//Return a string concatenated by '/' strings, because $this->uri_string is a string.
return $args ? '/' . implode('/', $args) : '';
}
//------------------------------------------------ -----------------------
/**
* Filter segments for malicious characters
* Filter illegal characters
* @access private
* @param string
* @return string
*/
function _filter_uri($str)
{
if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE )
{
                  // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards backwards
// compatibility compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
// It probably means because of PHP5.3.0 characters -increased as a need for righteousness. So when using str_replace() here, you need to add preg_quote() to escape -
                                                                                                                                                                         '-'))."]+$|i", $str))
                                                                 
show_error('The URI you submitted has disallowed characters.', 400);
      }  
} }
// Convert programatic characters to entities
// Convert character entity
        $bad    = array('$',        '(',        ')',        '%28',      '%29');  
        $good   = array('$',    '(',    ')',    '(',    ')');  
  
        return str_replace($bad, $good, $str);  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Remove the suffix from the URL if needed
* // Remove our custom suffix from the url.
* @access private
* @return void
*/  
    function _remove_url_suffix()  
    {  
        if  ($this->config->item('url_suffix') != "")  
        {  
            $this->uri_string = preg_replace("|".preg_quote($this->config->item('url_suffix'))."$|", "", $this->uri_string);  
        }  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Explode the URI Segments. The individual segments will
* be stored in the $this->segments array.
* Split the uri into positive segments and filter each segment, and store them in $this->segments[]
* @access private
* @return void
*/  
    function _explode_segments()  
    {  
        foreach (explode("/", preg_replace("|/*(.+?)/*$|", "\1", $this->uri_string)) as $val)  
        {  
            // Filter segments for security  
            $val = trim($this->_filter_uri($val));  
  
            if ($val != '')  
            {  
                $this->segments[] = $val;  
            }  
        }  
    }  
  
    // --------------------------------------------------------------------  
    /**
* Re-index Segments Re-index segment
* Makes the segments saved starting with subscript 1. This makes it easier to use because there is a 1:1 relationship between the segment array and the real segment
* This function re-indexes the $this->segment array so that it
* starts at 1 rather than 0. Doing so makes it simpler to
* use functions like $this->uri->segment(n) since there is
* a 1:1 relationship between the segment array and the actual segments.
*
* @access private
* @return void
*/  
    function _reindex_segments()  
    {  
        array_unshift($this->segments, NULL);  
        array_unshift($this->rsegments, NULL);  
        unset($this->segments[0]);  
        unset($this->rsegments[0]);  
    }  
  
    // --------------------------------------------------------------------  
  
    /** 
* Fetch a URI Segment
* Get a uri segment
* This function returns the URI segment based on the number provided. Provided
* This function returns a uri segment based on the provided number
* @access public
* @param integer
* @param bool
* @return string
*/
function segment($n, $no_result = FALSE)
{
return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
}
//------------------------------------------------ -----------------------
/**
* Fetch a URI "routed" Segment
* Return a certain section after the routing is determined
* This function returns the re-routed URI segment (assuming routing rules are used)
* based on the number provided. If there is no routing this function returns the
* same result as $this->segment()
* This function returns a routed uri segment based on the provided number (assuming routing rules have been used)
* If not routed yet, this function will be the same as $this->segment()
*
* @access public
* @param integer
* @param bool
* @return string
*/
function rsegment($n, $no_result = FALSE)
{
return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
}
//------------------------------------------------ -----------------------
/**
* Generate generates a key value pair from the URI string
* Generate an array of key-value pairs based on the uri string
*
* This function generates and associative array of URI data starting
* at the supplied by. . . Provide segment. For example, if this is your URI:
*
* example.com/user/search/name/joe/location/UK/gender/male
*
* You can use this function to generate an array with this prototype:
*
* array (
* name => joe
* location => UK
*   gender => male
* ) )
* This function generates an associative array from the uri segment
* Example: If your uri is like this
* example.com/user/search/name/joe/location/UK/gender/male
* Then a prototype like this will be generated
* array (
* name => joe
* location => UK
*   gender => male
* ) )
* @access public
* @param integer the starting segment number
* @param array an array of default values ​​
* @return array
*/
function uri_to_assoc($n = 3, $default = array())
{
return $this->_uri_to_assoc($n, $default, 'segment');
}
/**
* Identical exactly the same thing to above only it uses the re-routed segment array
* It is exactly the same as the previous function except that it flushes the routed segment array (note the third parameter)
* @access public
* @param integer the starting segment number
* @param array an array of default values ​​
* @return array
*
*/
function ruri_to_assoc($n = 3, $default = array())
{
return $this->_uri_to_assoc($n, $default, 'rsegment');
}
//------------------------------------------------ -----------------------
/**
* Generate a key value pair from the URI string or Re-routed URI string
* Generate an array of key-value pairs based on the uri string or the rerouted uri string
* @access private
* @param integer the starting segment number starting segment number
* @param array an array of default values ​​
* @param string which array we should use
* @return array
*/
function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
{
// Determine whether the segmented array is rerouted.
if ($which == 'segment')
{
$total_segments = 'total_segments';
$segment_array = 'segment_array';
} }
else
{
$total_segments = 'total_rsegments';
$segment_array = 'rsegment_array';
} }
// Is $n a number?
if ( ! is_numeric($n))
{
return $default;
} }
// There is enough key $n in the cache uri segment list
if (isset($this->keyval[$n]))
{
return $this->keyval[$n];
} }
// The total number of segments is less than $n
if ($this->$total_segments() < $n)
{
if (count($default) == 0)
                                                                 
return array();
      }  
$retval = array();
foreach ($default as $val)
                                                                 
$retval[$val] = FALSE;
      }  
return $retval;
} }
$segments = array_slice($this->$segment_array(), ($n - 1));
$i = 0;
        $lastval = '';  
        $retval  = array();  
        foreach ($segments as $seg)  
        {  
            if ($i % 2)  
            {  
                $retval[$lastval] = $seg;  
            }  
            else  
            {  
                $retval[$seg] = FALSE;  
                $lastval = $seg;  
            }  
  
            $i++;  
        }  
  
        if (count($default) > 0)  
        {  
            foreach ($default as $val)  
            {  
                if ( ! array_key_exists($val, $retval))  
                {  
                    $retval[$val] = FALSE;  
                }  
            }  
        }  
  
        // Cache the array for reuse  
        // 缓存数组一遍重用  
        $this->keyval[$n] = $retval;  
        return $retval;  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Generate a URI string from an associative associative array array
* Generate a uri string based on an associative array
*
* @access public
* @param array an associative array of key/values ​​
* @return array
*/  
    function assoc_to_uri($array)  
    {  
        $temp = array();  
        foreach ((array)$array as $key => $val)  
        {  
            $temp[] = $key;  
            $temp[] = $val;  
        }  
  
        return implode('/', $temp);  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Fetch a URI Segment and add a trailing slash
* Get a uri segment and add a /
*
* @access public
* @param integer
* @param string
* @return string
*/  
    function slash_segment($n, $where = 'trailing')  
    {  
        return $this->_slash_segment($n, $where, 'segment');  
    }  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Fetch a URI Segment and add a trailing slash 
     * 获取一个已经路由的uri段并且添加/ 
     * @access  public 
     * @param   integer 
     * @param   string 
     * @return  string 
     */  
    function slash_rsegment($n, $where = 'trailing')  
    {  
        return $this->_slash_segment($n, $where, 'rsegment');  
    }  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Fetch a URI Segment and add a trailing slash - helper function 
     * 
     * @access  private 
     * @param   integer 
     * @param   string 
     * @param   string 
     * @return  string 
     */  
    function _slash_segment($n, $where = 'trailing', $which = 'segment')  
    {  
        $leading    = '/';  
        $trailing   = '/';  
  
        if ($where == 'trailing')  
        {  
            $leading    = '';  
        }  
        elseif ($where == 'leading')  
        {  
            $trailing   = '';  
        }  
  
        return $leading.$this->$which($n).$trailing;  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Segment Array
* Get segment array
* @access public
* @return array
*/  
    function segment_array()  
    {  
        return $this->segments;  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Routed Segment Array
* Get the routed segment array
* @access public
* @return array
*/  
    function rsegment_array()  
    {  
        return $this->rsegments;  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Total number of segments
* Get the total number of segments
* @access public
* @return integer
*/  
    function total_segments()  
    {  
        return count($this->segments);  
    }  
  
    // --------------------------------------------------------------------  
  
    /**
* Total number of routed segments
* Get the total number of routed segments
* @access public
* @return integer
*/  
    function total_rsegments()  
    {  
        return count($this->rsegments);  
    }  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Fetch the entire URI string 
     * 
     * @access  public 
     * @return  string 
     */  
    function uri_string()  
    {  
        return $this->uri_string;  
    }  
  
  
    // --------------------------------------------------------------------  
  
    /** 
     * Fetch the entire Re-routed URI string 
     *  www.2cto.com
     * @access  public 
     * @return  string 
     */  
    function ruri_string()  
    {  
        return '/'.implode('/', $this->rsegment_array());  
    }  
  
}  
// END URI Class  
  
/* End of file URI.php */  
/* Location: ./system/core/URI.php */  
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477684.htmlTechArticle[php] ?php if ( ! defined(BASEPATH)) exit(No direct script access allowed); /** * CodeIgniter * * An open source application development framework for PHP 5.1.6 or newer * * @packag...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles