PHP $_GET 和 $_POST 未定义问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1359240/
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
PHP $_GET and $_POST undefined problem
提问by tuergeist
I'm new to PHP so I apologize if this is a simple problem...
我是 PHP 新手,所以如果这是一个简单的问题,我深表歉意......
I am moving a PHP site from one server to another. The new server is IIS 7.0, PHP 5.2.1, with short open tag turned "On", and I don't know how the original server was set-up (I was just given the code).
我正在将一个 PHP 站点从一台服务器移动到另一台服务器。新服务器是IIS 7.0,PHP 5.2.1,短的打开标签打开了,我不知道原来的服务器是如何设置的(我只是给了代码)。
The following is the very first section of code on one of the pages:
以下是其中一个页面上的第一段代码:
<?
ob_start();
session_start();
if($_GET['confirm'] == 13 || $_GET['confirm'] == 14 || $_GET['confirm'] == 15 || $_GET['confirm'] == 16)
{
include("test/query/test_query.php");
}
?>
When this page executes, the following error is always shown:
执行此页面时,始终显示以下错误:
PHP Notice: Undefined index: confirm in [file location].php on line 6
PHP 注意:未定义索引:在第 6 行的 [文件位置].php 中确认
Also, users access this page by being redirected from the home page (which is a standard HTML page). The full URL when properly navigated to is the following:
此外,用户通过从主页(这是一个标准的 HTML 页面)重定向来访问此页面。正确导航到的完整 URL 如下:
... I understand why the error is thrown. What I don't understand is how this code could ever work as it does on the original server. Could I be missing a configuration setting?
...我明白为什么会抛出错误。我不明白的是这段代码如何像在原始服务器上那样工作。我可能缺少配置设置吗?
*This same issue happens in dozens of locations all over the site. This is just one specific occurrence of the issue.
*同样的问题发生在整个站点的数十个位置。这只是该问题的一个特定事件。
回答by Josh
The new server has error_reportingset to E_ALL. What you're seeing is a notice, not an error. Try:
新服务器已error_reporting设置为 E_ALL。您看到的是通知,而不是错误。尝试:
error_reporting(E_ALL ^ E_NOTICE)
With error reporting set to E_ALL, accessing a member of an array which is not set generates an error. If you don't wish to lower your error reporting level, before checking $_GET['var'], change your code to:
将错误报告设置为 E_ALL 时,访问未设置的数组成员会产生错误。如果您不想降低错误报告级别,请在检查 $_GET['var'] 之前,将代码更改为:
if(isset($_GET['confirm']) && ($_GET['confirm'] == 13 || $_GET['confirm'] == 14 || $_GET['confirm'] == 15 || $_GET['confirm'] == 16)) {
by adding the call to isset() before you actually access $_GET['confirm'], you will verify that you're not accessing an array member which is not set. ($_GET['confirm']will only be set if the URL ends in ?confirm=...or ?something...&confirm=...)
通过在实际访问之前添加对 isset() 的调用$_GET['confirm'],您将验证您没有访问未设置的数组成员。($_GET['confirm']仅当 URL 以?confirm=...或结尾时才会设置?something...&confirm=...)
回答by tuergeist
I suggest to optimize the code for reading:
我建议优化阅读代码:
if (isset($_GET['confirm']) && ($_GET['confirm'] >= 13 && $_GET['confirm'] <= 16))
And I totally agree with Josh's proposal.
我完全同意乔希的提议。
回答by Cyathus
isset()is a useful function. It returns "true" if the variable exists and "false" if not. Usually, people use it in conjunction with a superglobal like $_GETor $_POSTto determine whether you're being sent from another page on the site - this allows you to create different actions based on where your user is coming from and what data is tagging along. It also prevents errors in trying to use variables you haven't yet defined, like the OP is getting. So instead of needing to write two different .php files and worrying about sending your user to the wrong one, you can do it all in one page.
isset()是一个有用的功能。如果变量存在,则返回“true”,否则返回“false”。通常,人们将它与超全局变量 like $_GETor结合使用$_POST来确定您是否是从站点上的另一个页面发送的 - 这允许您根据用户来自何处以及标记哪些数据来创建不同的操作。它还可以防止在尝试使用您尚未定义的变量时出错,就像 OP 一样。因此,您无需编写两个不同的 .php 文件并担心将您的用户发送到错误的文件中,您可以在一页中完成所有操作。
Jay,
I'd be careful about your usage of some of these calls. <?phpis more likely to work than <?. I've heard session_start() should be the very first thing set to the browser or it can cause header issues. And yes, you need to have a variable declared before you use it - if you're not typing in [file].php?confirm=[some number]as your URL, your page will break unless you amend it to allow for breaks.
杰伊,我会小心你对其中一些调用的使用。<?php比 更有可能工作<?。我听说 session_start() 应该是设置到浏览器的第一件事,否则它会导致标题问题。是的,您需要在使用之前声明一个变量 - 如果您没有输入[file].php?confirm=[some number]URL,除非您修改它以允许中断,否则您的页面将会中断。
回答by Johrn
Since there is no index $_GET['confirm'], PHP throws a notice that you are looking at an undefined index. The notice is being displayed because the new server has the E_NOTICEflag set in error_reporting somewhere, either in php.ini or in some config file or bootstrap that is run on pageloads.
由于没有索引 $_GET['confirm'],PHP 会抛出一个通知,提示您正在查看未定义的索引。之所以显示该通知,是因为新服务器E_NOTICE在 php.ini 或在页面加载上运行的某些配置文件或引导程序中的 error_reporting 中设置了标志。
From the php manual, E_NOTICE: "Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script."
来自 php 手册,E_NOTICE:“运行时通知。表明脚本遇到了一些可能表示错误的东西,但也可能在运行脚本的正常过程中发生。”
You can either try turning off the notices if you aren't worried about them, or use them to track down places where there may be problems.
如果您不担心它们,您可以尝试关闭通知,或者使用它们来追踪可能存在问题的地方。
For the code you posted, an easy fix would be to change the conditional to
对于您发布的代码,一个简单的解决方法是将条件更改为
if(isset($_GET['confirm']) && <list of OR conditions>)
That way PHP bails out of evaluating the conditional if there is no 'confirm' index.
这样,如果没有“确认”索引,PHP 就不会评估条件。
回答by Sarfraz
That's because confirmquery string variable does not seem to be set, you can check it like:
那是因为confirm查询字符串变量似乎没有设置,您可以像这样检查它:
ini_set('display_errors', true);
error_reporting(E_ALL);
var_dump($_GET['confirm']);

