Method for PHP to get the url of the current url and replace the parameter or url

  • 2020-03-31 20:55:02
  • OfStack

One is that PHP gets the url of the current page:
 
//Gets the current script url
function GetCurUrl() 
{ 
if(!empty($_SERVER["REQUEST_URI"])) 
{ 
$scriptName = $_SERVER["REQUEST_URI"]; 
$nowurl = $scriptName; 
} 
else 
{ 
$scriptName = $_SERVER["PHP_SELF"]; 
if(empty($_SERVER["QUERY_STRING"])) 
{ 
$nowurl = $scriptName; 
} 
else 
{ 
$nowurl = $scriptName."?".$_SERVER["QUERY_STRING"]; 
} 
} 
return $nowurl; 
} 

The other is that PHP replaces the value of some variable in the query part of the url for example, we're going to say key=321 in the $url;
There are a couple of things:
$url = "www.sina.com/a.php? Key = 330 ';
Or $url = "www.sina.com/a.php;
Or $url = "www.sina.com/a.php? The cat = 2 ');
And so on. While there are many cases, PHP is very simple to handle, as follows:
 
 //That's a good paragraph.
function url_set_value($url,$key,$value) 
{ 
$a=explode('?',$url); 
$url_f=$a[0]; 
$query=$a[1]; 
parse_str($query,$arr); 
$arr[$key]=$value; 
return $url_f.'?'.http_build_query($arr); 
} 

But my substitution is written like this. Of course it sucks.
 
<?php 
/** 
* Usage routines: can be used for pagination classes, page replacements, etc  
$url = "add_jd.php?pid=4&tb=gm_jd&page=1"; 
echo( " The original URL : " . $url ); 
echo( '<br/>' ); 
echo( " String parameter: " . url::replace( $url , "pid=10,page=2") ); 
echo( '<br/>' ); 
echo( " Array type parameters: " . url::replace( $url , array('pid'=>10,'page'=>5)) ); 
//echo( urlReplace( $url , array('pid'=>10,'page'=>5)) ); 
*/ 
/** 
* url replace 
* @param string $url  replaceable URL String, usually aaa.php?abc=def, You can also take a path, like http://xxx.com/abc/def.php?aa=bb 
* @param mixed $options  The variable that needs to be replaced can be a string or an array, and if it's a string, the format is "aa=bb,cc=dd" There are more than one "," separated  
* @return string $url  After the replacement URL 
*/ 
class url 
{ 
static function replace ( $url , $options) 
{ 
$options = self::optInit( $options ); 
$Query = parse_url( $url , PHP_URL_QUERY ); 
if($Query){ 
parse_str( $Query , $GET ); 
if ( $GET ){ 
//foreach ( $GET as $_k => $_v ){ 
// //if( array_key_exists( $_k , $options)){ 
// $GET[$_k] = $options[$_k]; 
// //} 
//} 
$GET = array_merge($GET,$options); 
} 
return str_replace( $Query , http_build_query( $GET ), $url ); 
} 
if( !$Query && $options ){ 
return $url . "?" . http_build_query($options); 
} 
return $url; 
} 
static private function optInit ( $options ) 
{ 
if( is_string( $options )){ 
$optlists = Power::Normalize( $options ); 
foreach( $optlists as $val){ 
list($tmpKey,$tmpVal) = Power::Normalize( $val , "="); 
$opts[$tmpKey] = $tmpVal; 
} 
}else{ 
$opts = $options; 
} 
//unset( $options ); 
return $opts; 
} 
} 

A few things considered, but only a very general solution

The following are some supplementary information:
Example: I need to get the current URL address
$url_this = "http://". $_SERVER [' HTTP_HOST] $_SERVER [' PHP_SELF];
Echo $url_this;

http://localhost/lu.php displays

Server variable: $_SERVER
Note: used in PHP 4.1.0 and later. Previous versions used $HTTP_SERVER_VARS.

$_SERVER is an array containing such items as headers, paths, and script locations. The entities of the array are created by the web server. There is no guarantee that all servers will produce all the information; The server may have ignored some information or generated some new information that is not listed below. This means that a large number of these variables are specified in the CGI 1.1 specification, so you should take a closer look at it.

This is a "superglobal" or can be described as an automatic global variable. This simply means that it works in all scripts. You do not need to use global $_SERVER in functions or methods; Accessing it is like using $HTTP_SERVER_VARS.

$HTTP_SERVER_VARS contains the same information, but is not an automatic global variable. (note: $HTTP_SERVER_VARS and $_SERVER are different variables, and PHP handles them differently.)

These variables are also available in all scripts if the register_globals directive is set; That is, the $_SERVER and $HTTP_SERVER_VARS arrays are separated. For information, see the section on security using Register Globals. These individual global variables are not automatic global variables.

You may find that some of the $_SERVER elements listed below are not available. Note that few of the elements listed below are valid (or meaningful) if you run PHP from the command line.


"PHP_SELF"
The file name of the script currently executing, related to the document root. , for example, in the script URL address for http://example.com/test.php/foo.bar use $_SERVER [' PHP_SELF] will get/test. The PHP/foo bar the result.

This variable is not valid if PHP is running on the command line.

"Argv"
Parameters passed to the script. When the script runs in command line mode, the argv variable is passed to the program's c-language-style command line argument. When the GET method is called, this variable contains the requested data.

"Arg c."
Contains the number of command line arguments passed to the program (if running in command line mode).

'GATEWAY_INTERFACE'
The version of the CGI specification used by the server. For example, "CGI/1.1".

'SERVER_NAME'
The name of the server host on which the script is currently running. If the script is running on a virtual host, the name is determined by the value set by that virtual host.

'SERVER_SOFTWARE'
A string for the server identity, given in the header when responding to a request.

SERVER_PROTOCOL '
The name and version of the communication protocol when a page is requested. For example, "HTTP/1.0".

"REQUEST_METHOD"
The request method when accessing the page. For example: "GET", "HEAD", "POST", "PUT".

"QUERY_STRING"
The string of a query.

"DOCUMENT_ROOT"
The document root directory where the script is currently running. Defined in the server configuration file.

'HTTP_ACCEPT'
The contents of the Accept: header for the current request.

'HTTP_ACCEPT_CHARSET'
Accept-charset: contents of the header of the current request. For example: "iso-8859-1,*,utf-8".

"HTTP_ACCEPT_ENCODING"
Contents of the current request accept-encoding: header. For example: "gzip".

"HTTP_ACCEPT_LANGUAGE"
The content of the current request's accept-language: header. For example: "en".

"HTTP_CONNECTION"
Connection of the current request: the contents of the header. For example: "keep-alive".

"HTTP_HOST"
The contents of the current request's Host: header.

"HTTP_REFERER"
The URL of the previous page that links to the current page. Not all user agents (browsers) set this variable, and some can modify HTTP_REFERER manually. Therefore, this variable is not always true.

"HTTP_USER_AGENT"
Current requested User_Agent: contents of the header. The string indicates information about the user agent accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586). You can also get this information using get_browser().

"REMOTE_ADDR"
Browsing the current page user's IP address.

'REMOTE_HOST'
Browsing the host name of the current page user. Reverse DNS is based on REMOTE_ADDR for that user.

Note: the Web server must be configured to create this variable. For example, Apache needs to have HostnameLookups On in httpd.conf. See gethostbyaddr ().

"REMOTE_PORT"
The port used by the user when connecting to the server.

"SCRIPT_FILENAME"
The absolute path name of the currently executing script.

"SERVER_ADMIN"
This value indicates the SERVER_ADMIN parameter in the Apache server configuration file. If the script is running on a virtual host, this value is the value of that virtual host.

"SERVER_PORT"
The port used by the server. The default is "80". If you use SSL for secure connections, this value is the HTTP port you set up.

"SERVER_SIGNATURE"
A string containing the server version and virtual host name.

"Just"
The basic path to the file system where the current script resides (not the document root directory). This is the result after the server virtualizes the image to the real path.

"SCRIPT_NAME"
Contains the path to the current script. This is useful when the page needs to point to itself.

"REQUEST_URI"
The URI required to access this page. For example, "/index.html".

"PHP_AUTH_USER"
When PHP is running in Apache module mode and using HTTP authentication, this variable is the username entered by the user.

"PHP_AUTH_PW"
When PHP is running in Apache module mode and using HTTP authentication, this variable is the password the user enters.

"The AUTH_TYPE"
When PHP is running in Apache module mode and using HTTP authentication, this variable is the type of authentication.

Related articles: