PHP method to obtain the complete url of the current page

Source: Internet
Author: User
Php provides three methods to obtain the complete and real url address of the current page, including parameters. you can select the method as needed. the specific function code is as follows: the first method is functionGetCurUrl () {if (isset ($ _ SERVER [REQUEST_URI]) {$ url $ _ SERVER [REQUEST_URI];} else {if (isset ($ _ SERVER [argv]) {$ url $ _ S php obtains the complete and real url address of the current page, including parameters. three methods are summarized. you can select the method as needed. the specific function code is as follows:

Method 1:

Function GetCurUrl (){

If (isset ($ _ SERVER ['request _ URI ']) {
$ Url = $ _ SERVER ['request _ URI '];
} Else {
If (isset ($ _ SERVER ['argv']) {
$ Url = $ _ SERVER ['php _ SELF '].'? '. $ _ SERVER ['argv'] [0];
} Else {
$ Url = $ _ SERVER ['php _ SELF '].'? '. $ _ SERVER ['query _ string'];
}
}
Return urlencode ($ url); // pay attention to the encoding

}

The second method (it is said that the machine dream uses this method ):

// 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;

}

Method 3 (recommended ):

// Php retrieves the complete url of the current access

Function GetCurUrl (){
$ Url = 'http ://';
If (isset ($ _ SERVER ['https']) & $ _ SERVER ['https'] = 'on '){
$ Url = 'https ://';
}
If ($ _ SERVER ['server _ port']! = '80 '){
$ Url. = $ _ SERVER ['server _ name']. ':'. $ _ SERVER ['server _ port']. $ _ SERVER ['request _ URI '];
} Else {
$ Url. = $ _ SERVER ['server _ name']. $ _ SERVER ['request _ URI '];
}
Return $ url;
}

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.