如何从php中获取URL中的特定参数

时间:2022-10-27 10:27:27

I want to fetch method id from following URL,

我想从以下URL获取方法ID,

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 i.e 2

我想从上面的URL获取方法ID,即2

2 个解决方案

#1


0  

Use superglobals in this case $_GET:

在这种情况下使用超全局$ _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()

您的所有请求数据都将以$ _GET或$ _POST的形式提供,您不应自行分解URL。但是,如果要解析不是当前请求的URL,请使用parse_url()

#2


0  

Use $_GET like this

像这样使用$ _GET

echo $_GET['id'];

Output of var_dump($_GET);

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'];

#1


0  

Use superglobals in this case $_GET:

在这种情况下使用超全局$ _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()

您的所有请求数据都将以$ _GET或$ _POST的形式提供,您不应自行分解URL。但是,如果要解析不是当前请求的URL,请使用parse_url()

#2


0  

Use $_GET like this

像这样使用$ _GET

echo $_GET['id'];

Output of var_dump($_GET);

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'];