简体   繁体   中英

How to get specific parameter from URL in php

I want to fetch method id from following URL,

I have used following code to get url

$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);

$url = $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];

Result:

http://www.mydomain.com/post-file.php?id=154&email=stevemartin144@ymail.com&method=2&reference=uhr748

I want to get method id from above URL ie 2

Use superglobals in this case $_GET :

$id = $_GET['id'];

All your request data will be available in either $_GET or $_POST , you should not decompose the URL yourself. If however, you want to parse a URL, that is not the current request, use parse_url()

Use $_GET like this

echo $_GET['id'];

Output of var_dump($_GET);

array (size=4)
  'id' => string '154' (length=3)
  'email' => string 'stevemartin144@ymail.com' (length=24)
  'method' => string '2' (length=1)
  'reference' => string 'uhr748' (length=6)

You can get other vars:

echo $_GET['email'];
echo $_GET['method'];
echo $_GET['reference'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM