PHP retrieves the current URL and replaces the parameter or URL

Source: Internet
Author: User
Tags http authentication php website

First, PHP retrieves the URL of the current page: CopyCode The Code is as follows: // obtain 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 to replace the value of a variable in the query part of the PHP website. For example, we need to set key = 321 in $ URL;
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 ′;
And so on. Although there are many situations, PHP is very simple to handle, as shown below:Copy codeThe Code is as follows:/* set a parameter in the URL to a value * // [This section is quite good]
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 );
}

However, my replacement is written in this way .. Of course .. Copy code The Code is as follows: <? PHP
/**
* Routine: used for paging or page replacement.
$ 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 parameters:". url: Replace ($ URL, array ('pid '=> 10, 'page' => 5 )));
// Echo (urlreplace ($ URL, array ('pid '=> 10, 'page' => 5 )));
*/
/**
* URL replace
* @ Param string $ the URL string to be replaced, which is generally AAA. php? ABC = def, can also carry a path, like a http://xxx.com/abc/def.php? AA = bb
* @ Param mixed $ options: the variable to be replaced. It can be a string or an array. If it is a string, the format is "AA = BB, Cc = dd". There are multiple variables ", "separated
* @ Return string $ the URL after the URL is replaced
*/
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 taken into consideration, it is only a very general solution.

The following are some additional information:
For example, I need to obtain the current URL address.
$ Url_this = "http: //". $ _ server ['HTTP _ host']. $ _ server ['php _ Self '];
Echo $ url_this;

Http: // localhost/Lu. php

Server variable: $ _ Server
Note: PhP 4.1.0 and later versions are used. In earlier versions, $ http_server_vars is used.

$ _ Server is an array containing headers, paths, and script locations. The object of the array is created by the Web server. It is not guaranteed that all servers can generate all information. The server may ignore some information or generate new information not listed below. This means that a large number of these variables are described in 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 only means that it is valid in all scripts. You do not need to use global $ _ server; to access a function or method, just like using $ http_server_vars.

$ Http_server_vars contains the same information, but it is not an automatic global variable. (Note: $ http_server_vars and $ _ server are different variables. php processes them differently .)

If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ server and $ http_server_vars arrays are separated. For more information, see the security section using register globals. These independent global variables are not automatic global variables.

You may find that some $ _ server elements listed below are not available. Note: If you run php In the command line mode, the elements listed below are hardly valid (or have no practical significance ).

"Php_self"
The file name of the script being executed, which is 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 the result of/test. php/Foo. Bar.

If PHP is run as a command line, the variable is invalid.

"Argv"
Parameters passed to the script. When the script runs in the command line mode, the argv variable is passedProgramC-style command line parameters. When the get method is called, the variable contains the requested data.

"Argc"
Contains the number of command line parameters passed to the Program (if it is in command line mode ).

"Gateway_interface"
The CGI Specification Version Used by the server. For example, "cgi/1.1 ".

'Server _ name'
Name of the server host where the script is currently running. If the script runs on a VM, the name is determined by the value set by that VM.

'Server _ soft'
The string identified by the server, which is in the header of the Response Request.

"Server_protocol"
The name and version of the communication protocol on the request page. For example, "HTTP/1.0 ".

"Request_method"
The request method used to access the page. For example, "get", "head", "Post", and "put ".

"QUERY_STRING"
Query string.

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

"Http_accept"
The accept of the current request: the content in the header.

"Http_accept_charset"
Accept-charset of the current request: Content in the header. Example: "iso-8859-1, *, UTF-8 ".

"Http_accept_encoding"
Accept-encoding of the current request: Content in the header. For example, "gzip ".

"Http_accept_language"
The accept-language of the current request: the content in the header. For example, "en ".

"Http_connection"
Connection of the current request: Content in the header. For example, "keep-alive ".

"Http_host"
Host of the current request: Content in the header.

"Http_referer"
The URL of the previous page that is linked to the current page. Not all user proxies (browsers) will set this variable, and some can also manually modify http_referer. Therefore, this variable is not always true.

"Http_user_agent"
User_agent of the current request: Content in the header. This string indicates the information of 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 obtain this information.

"Remote_addr"
Browsing the IP address of the user on the current page.

'Remote _ host'
The host name of the user browsing the current page. Reverse Domain name resolution is based on the user's remote_addr.

Note: You must configure the Web server to create this variable. For example, Apache needs to include hostnamelookups on in httpd. conf. See gethostbyaddr ().

"Remote_port"
The port used by the user to connect to the server.

"Script_filename"
The absolute path name of the currently executed script.

"Server_admin"
This value specifies the server_admin parameter in the Apache server configuration file. If the script runs on a VM, this value is the value of that VM.

"Server_port"
The port used by the server. The default value is "80 ". If you use an SSL secure connection, this value is the http port you set.

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

"Path_translated"
The basic path of the file system (not the document root directory) where the current script is located. This is the result of a virtual image to a real path on 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 runs in the Apache module mode and uses the HTTP authentication function, this variable is the user name entered by the user.

"Php_auth_pw"
When PHP runs 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 runs in the Apache module mode and uses the HTTP authentication function, this variable is the authentication type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.