PHP $_GET and $_POST undefined problem

流过昼夜 提交于 2019-11-26 23:24:51

问题


I'm new to PHP so I apologize if this is a simple problem...

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).

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

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:

http://www.[site].com/test.php#login

... 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.


回答1:


The new server has error_reporting set to E_ALL. What you're seeing is a notice, not an error. Try:

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:

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=...)




回答2:


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.




回答3:


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_NOTICE flag set in error_reporting somewhere, either in php.ini or in some config file or bootstrap that is run on pageloads.

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."

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.




回答4:


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 $_GET or $_POST to 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.

Jay, I'd be careful about your usage of some of these calls. <?php is 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.




回答5:


That's because confirm query string variable does not seem to be set, you can check it like:

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

var_dump($_GET['confirm']);


来源:https://stackoverflow.com/questions/1359240/php-get-and-post-undefined-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!