Four examples of how PHP URL parameters are obtained

Source: Internet
Author: User

In the case of known URL parameters, we can use $_get to obtain the corresponding parameter information ($_get[' name ') according to our own situation; how do I get the parameter information on the URL in an unknown situation?

The first, using $_server built-in array variables

Relatively primitive $_server[' query_string ' to get, URL parameters, usually returned using this variable will be similar to the data: name=tank&sex=1
If you need to include a file name, you can use $_server["Request_uri"] (return similar to:/index.php?name=tank&sex=1)

The second, using pathinfo built-in functions

The code is as follows:
<?php
$test = PathInfo ("http://localhost/index.php");
Print_r ($test);
/*
The results are as follows
Array
(
[DirName] = http://localhost//url path
[basename] = index.php//full file name
[Extension] = PHP//filename suffix
[FileName] = = index//file name
)
*/
?>

Third, the use of parse_url built-in functions

The code is as follows:
<?php
$test = Parse_url ("Http://localhost/index.php?name=tank&sex=1#top");
Print_r ($test);
/*
The results are as follows
Array
(
[Scheme] = HTTP/What protocol to use
[Host] = localhost/host name
[path] =/index.php//Path
[query] = name=tank&sex=1//Parameters passed
[Fragment] = Top//anchor point of the back root
)
*/
?>

Fourth, the use of basename built-in functions

The code is as follows:
<?php
$test = basename ("Http://localhost/index.php?name=tank&sex=1#top");
echo $test;
/*
The results are as follows
Index.php?name=tank&sex=1#top
*/
?>

In addition, there is a regular matching process to get the desired value. This method is more accurate, the efficiency is not considered ...
Following the development of the practice of regular processing methods:

The code is as follows:
<?php
Preg_match_all ("/(\w+=\w+) (#\w+)?/I", "Http://localhost/index.php?name=tank&sex=1#top", $match);
Print_r ($match);
/*
The results are as follows
Array
(
[0] = = Array
        (
[0] = Name=tank
[1] = Sex=1#top
        )
[1] = = Array
         (
[0] = Name=tank
[1] = Sex=1
         )
[2] = = Array
        (
[0] = =
[1] = = #top
        )
)
*/
?>

Four examples of how PHP URL parameters are obtained

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.