The first is PHP to get the current page URL:
Copy Code code as follows:
Get 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 the PHP replacement URL in the query section of a variable value for example, we want to set the $url in the key=321;
In fact, there are several situations:
$url = ' www.sina.com/a.php?key=330′;
or $url= ' www.sina.com/a.php;
or $url= ' www.sina.com/a.php?cat=2′;
Wait a minute. Although the situation is many, PHP is very simple to deal with, as follows:
Copy Code code as follows:
/* Set a parameter in the URL to a value//"This is 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 replacement is written in this way. Of course it sucks.
Copy Code code as follows:
<?php
/**
* Use routines: can be used in pagination classes or in pages of replacements, etc.
$url = "add_jd.php?pid=4&tb=gm_jd&page=1";
Echo ("Original URL:". $url);
Echo (' <br/> ');
Echo ("string parameter:". Url::replace ($url, "pid=10,page=2"));
Echo (' <br/> ');
echo ("Array parameter:". Url::replace ($url, Array (' PID ' =>10, ' page ' =>5));
Echo (Urlreplace ($url, Array (' PID ' =>10, ' page ' =>5));
*/
/**
* URL Replace
* @param string $url a URL string that needs to be replaced, typically aaa.php?abc=def, or with a path, like HTTP://XXX.COM/ABC/DEF.PHP?AA=BB
* @param mixed $options need to replace the variable, can be a string or array, if it is a string, formatted as "AA=BB,CC=DD", there are multiple, separated by ","
* @return string $url the replaced 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;
}
}
Although some things have been considered, it is only a general solution.
The following are some additional information:
Example: I need to get the current URL address
$url _this = "http://". $_server [' http_host '].$_server[' php_self '];
echo $url _this;
Then show: http://localhost/lu.php
Server variables: $_server
Note: Used in PHP 4.1.0 and later versions. Previous versions, using the $HTTP _server_vars.
$_server is an array that contains such things as the head (headers), the path (paths), and the script location (scripts locations). The entities of the array are created by the Web server. There is no guarantee that all servers will generate all the information, the server may have overlooked some information, or some new information that has not been listed below. This means that a large number of these variables are described in the CGI 1.1 specification, so you should study it carefully.
This is a "superglobal", or it 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 a function or method; Access it, just as you would with $HTTP _server_vars.
The $HTTP _server_vars contains the same information, but is not an automatic global variable. (Note: $HTTP _server_vars and $_server are different variables, PHP handles them in different ways.) )
If the register_globals directive is set, these variables are also available in all scripts, that is, the $_server and $HTTP _server_vars arrays are separated. For information, please refer to the Security section for the use of the 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 if you run PHP on a command-line basis, the elements listed below are almost not valid (or meaningless).
"Php_self"
The name of the file that is currently executing the script, related to document root. For example, using $_server[' php_self ' in a script with a URL address of Http://example.com/test.php/foo.bar will get/test.php/foo.bar this result.
If PHP runs as a command-line, the variable is not valid.
"argv"
The arguments passed to the script. When the script runs on the command-line, the ARGV variable is passed to the program C language-style command-line argument. When you call the Get method, the variable contains the requested data.
"ARGC"
Contains the number of command-line arguments that are 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 that is identified by the server, given in the header in response to the request.
"Server_protocol"
The name and version of the communication protocol when the page is requested. For example, "http/1.0".
"Request_method"
The request method when accessing the page. For example:, POST and put.
"Query_string"
A string of queries (query).
"Document_root"
The document root directory where the currently running script resides. Defined in the server configuration file.
"Http_accept"
Accept of the current request: the contents of the header.
"Http_accept_charset"
Accept-charset of the current request: the contents of the header. For example: "Iso-8859-1,*,utf-8".
"Http_accept_encoding"
Accept-encoding of the current request: the contents of the header. For example: "gzip".
"Http_accept_language"
Accept-language of the current request: the contents of the header. For example: "en".
"Http_connection"
Connection of the current request: the contents of the header. For example: "Keep-alive".
"Http_host"
Current requested Host: The contents of the header.
"Http_referer"
Link to the URL address of the previous page of the current page. Not all user agents (browsers) will set this variable, and some can also modify http_referer manually. Therefore, this variable is not always true.
"Http_user_agent"
User_agent of the current request: the contents of the header. The string indicates the information for the user agent accessing the page. A typical example is: mozilla/4.5 [en] (X11; U Linux 2.2.9 i586). You can also use Get_browser () to get this information.
"REMOTE_ADDR"
Browsing the IP address of the user of the current page.
' Remote_host '
The host name of the user who is browsing the current page. The reverse domain name resolution is based on the user's remote_addr.
Note: The WEB server must be configured to establish this variable. For example, Apache needs to have hostnamelookups on in httpd.conf. See Gethostbyaddr ().
"Remote_port"
The port to use when the user connects to the server.
"Script_filename"
The absolute path name of the current execution 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, the value is the value of that virtual host.
"Server_port"
The port used by the server. The default is "80". If you are using SSL for secure connection, this value is the HTTP port you set up.
"Server_signature"
A string containing the server version and the virtual host name.
"Path_translated"
The basic path of the file system (not the document root) that contains the current script. This is the result of a virtual to real-path image of the server.
"Script_name"
The path that contains 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 the Apache module mode and is using the HTTP authentication function, this variable is the username entered by the user.
"PHP_AUTH_PW"
When PHP is running in the Apache module mode and is using the HTTP authentication function, this variable is the password entered by the user.
"Auth_type"
When PHP is running in the Apache module mode and is using HTTP authentication, this variable is the type of authentication.