Get URL query string parameters

前端 未结 11 1433
长情又很酷
长情又很酷 2020-11-22 17:41

What is the \"less code needed\" way to get parameters from a URL query string which is formatted like the following?

www.mysite.com/category/subcateg

11条回答
  •  耶瑟儿~
    2020-11-22 17:44

    The function parse_str() automatically reads all query parameters into an array.

    For example, if the URL is http://www.example.com/page.php?x=100&y=200, the code

    $queries = array();
    parse_str($_SERVER['QUERY_STRING'], $queries);
    

    will store parameters into the $queries array ($queries['x']=100, $queries['y']=200).

    Look at documentation of parse_str


    EDIT

    According to the PHP documentation, parse_str() should only be used with a second parameter. Using parse_str($_SERVER['QUERY_STRING']) on this URL will create variables $x and $y, which makes the code vulnerable to attacks such as http://www.example.com/page.php?authenticated=1.

提交回复
热议问题