// If neither PATH_INFO nor QUERY_STRING is found, we can only use $_GET
// We've exhausted all our options...
// I have rewritten it here to get uri_protocol. In fact, I think it is completely possible to get it only once. . .
// The following is to choose different methods to obtain uri according to different methods
// If the uri_protocol you define is outside the three methods of AUTO REQUEST_URI CLI, execute the following paragraph.
* if necessary. Necessities
* This function will automatically find the uri and fix the query string if necessary.
// 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.)
// The effect here is the same as above, except that $_SERVER['SCRIPT_NAME'] is replaced by
// 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.
// If two paragraphs can be divided through the above regular regularity, is it via Query_String? Route access in the form of
// The function parses the query string into the $_GET variable.
// 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 */
http://www.bkjia.com/PHPjc/477684.htmlwww.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...