Php: how to obtain the current url and replace the parameters or url _ PHP Tutorial

Source: Internet
Author: User
Tags http authentication
Php retrieves the current url and replaces the parameter or url. First, PHP retrieves the URL of the current page: Copy the code as follows: get the current script URL functionGetCurUrl () {if (! Empty ($ _ SERVER [REQUEST_URI]) {$ scriptName $ _ SERVER [1. PHP obtains the URL of the current page:

The code is as follows:

// Obtain the current script URL
Function GetCurUrl ()
If (! Empty ($ _ SERVER ["REQUEST_URI"])
$ ScriptName = $ _ SERVER ["REQUEST_URI"];
$ Nowurl = $ scriptName;
$ ScriptName = $ _ SERVER ["PHP_SELF"];
If (empty ($ _ SERVER ["QUERY_STRING"])
$ Nowurl = $ scriptName;
$ 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 Key = 330 ′;
Or $ url = 'www;
Or $ url = 'www Cat = 2 ′;
And so on. Although there are many situations, PHP is very simple to handle, as shown below:

The code is as follows:

/* Set a parameter in the URL to a value * // [this paragraph 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 ..

The code is as follows:

* Routine: used for paging or page replacement.
$ Url = "add_jd.php? Pid = 4 & tb = gm_jd & page = 1 ";
Echo ("Original URL:". $ url );
Echo ('
Echo ("string parameter:". url: replace ($ url, "pid = 10, page = 2 "));
Echo ('
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 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 ).

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 will get the result of/test. PHP/foo. bar.

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

Parameters passed to the script. When the script runs in the command line mode, the argv variable is passed to the command line parameters in the C language style of the program. When the GET method is called, the variable contains the requested data.

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

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.

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

The request method used to access the page. For example, "GET", "HEAD", "POST", and "PUT ".

Query string.

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

The Accept of the current request: the content in the header.

Accept-Charset of the current request: content in the header. Example: "ISO-8859-1, *, UTF-8 ".

Accept-Encoding of the current request: content in the header. For example, "gzip ".

The Accept-Language of the current request: the content in the header. For example, "en ".

Connection of the current request: content in the header. For example, "Keep-Alive ".

Host of the current request: content in the header.

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.

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.

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 ().

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

The absolute path name of the currently executed script.

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.

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.

A string containing the server version and virtual host name.

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.

The path that contains the current script. This is useful when the page needs to point to itself.

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

When PHP runs in the Apache module mode and uses the HTTP authentication function, this variable is the user name entered by the user.

When PHP runs in the Apache module mode and is using the HTTP authentication function, this variable is the password entered by the user.

When PHP runs in the Apache module mode and uses the HTTP authentication function, this variable is the authentication type.

The response code is as follows: // Obtain the current script URL function GetCurUrl () {if (! Empty ($ _ SERVER ["REQUEST_URI"]) {$ scriptName = $ _ SERVER [...

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: 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.