php 通过 URL 传递多个变量并在下一页读取所有变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9832508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:43:53  来源:igfitidea点击:

Pass multiple variables via URL and reading all of them on next page

phpurlparametersget

提问by Danny

I have a link that points to a webpage e.g. "land.php". The link looks like this:

我有一个指向网页的链接,例如“land.php”。该链接如下所示:

<a href="land.php?id=1&cd=a">link</a>

this takes me to the page land.php where I can read the first parametere with $id(and it is equal to 1, correctly), but I cannot read the second one. I either tried with $cdor $_GET['cd']. None of them works.

这将我带到land.php 页面,我可以在其中读取第一个参数$id(并且它正确地等于1),但我无法读取第二个参数。我要么尝试使用$cd要么$_GET['cd']。它们都不起作用。

if I tried isset($cd)it says false. Same thing for isset($_GET['cd']).

如果我试过isset($cd)它会说false。同样的事情isset($_GET['cd'])

How can I pass the second parameter too (and read it!)?

我怎样才能传递第二个参数(并阅读它!)?

EDIT:

编辑:

some code (so people are happy. I think it's pointless in this case..).

一些代码(所以人们很高兴。我认为在这种情况下它毫无意义......)。

land.php

土地.php

<?php
if($_GET['cd']==a)
    echo "<h2>HI</h2>";
else
    echo "<h2>BY</h2>";
?>

if I use $cdinstead of $_GET['cd']it doesn't work anyway..

如果我使用$cd而不是$_GET['cd']它无论如何都不起作用..

EDIT2 I don't get any syntax error, it just doesn't behave how expected.

EDIT2 我没有收到任何语法错误,它只是不符合预期。

回答by Ragnar123

The value is stored in $_GET['cd'].

该值存储在$_GET['cd'].

Try printing out the $_GETarray, with print_r($_GET);

尝试打印出$_GET数组,使用print_r($_GET);

print_r($_GET)should output

print_r($_GET)应该输出

Array
(
    [id] => 1
    [cd] => a
)

This should ofcourse be in the land.phppage, as the get variables are only available in the requested page.

这当然应该在land.php页面中,因为获取变量仅在请求的页面中可用。

回答by Andreas Hagen

Your server might be set up to accept semicolon instead of ampersands. Try replacing & with ;

您的服务器可能设置为接受分号而不是&符号。尝试用 ; 替换 &

回答by Phil

$_GET['cd']is the correct syntax. Are you actually on the land.phppage, ie does your browser's address bar read something like

$_GET['cd']是正确的语法。您是否真的在land.php页面上,即您浏览器的地址栏是否显示类似

example.com/land.php?id=1&cd=a

Also, it looks like you have register_globalsenabled if you can read $id. This is a very bad idea.

此外,register_globals如果您可以阅读$id. 这是一个非常糟糕的主意。



Update

更新

Your code snippet contains syntax errors. I recommend the following, including enabling decent error reporting for development

您的代码段包含语法错误。我推荐以下内容,包括为开发启用适当的错误报告

ini_set('display_errors', 'On');
error_reporting(E_ALL);

if(isset($_GET['cd']) && $_GET['cd'] == 'a') {
    echo "<h2>HI</h2>";
} else {
    echo "<h2>BY</h2>";
}