Problems with PHP $ _GET and $ _POST undefined

I am 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 a short open tag converted to "On", and I don’t know how the original server was configured (I was just provided with the code).

Below 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 is executed, the following error is always displayed:

PHP note: Undefined index: confirm at [file location] .php on line 6

In addition, users access this page by redirecting from the home page (which is a standard HTML page). Full URL with proper navigation:

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

... I understand why the error occurs. I do not understand how this code could work, as on the source server. Can I skip the configuration?

* The same problem occurs in dozens of locations around the site. This is just one specific problem event.

+4
source share
5 answers

On the new server, error_reporting set to E_ALL. What you see is a notification, not an error. Try:

 error_reporting(E_ALL ^ E_NOTICE) 

With the error message set to E_ALL, accessing an array member that is not set generates an error. If you do not want to lower the level of error reporting, 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 a isset () call, before you really get access to $_GET['confirm'] , you will be sure that you are not accessing an array member that is not defined. ( $_GET['confirm'] will only be set if the URL ends with ?confirm=... or ?something...&confirm=... )

+27
source

I suggest optimizing the code for reading:

 if (isset($_GET['confirm']) && ($_GET['confirm'] >= 13 && $_GET['confirm'] <= 16)) 

And I totally agree with Josh's suggestion.

+6
source

Since there is no index $ _GET ['confirm'], PHP issues a notification that you are looking at an undefined index. A notification is displayed because the new server has the E_NOTICE flag set in the error_reporting file somewhere, either in php.ini, or in some configuration file or boot buffer that runs in pageloads.

In the php E_NOTICE : " E_NOTICE notifications. Indicate that the script has detected something that may indicate an error, but may also occur in the normal course of the script."

You can try to turn off notifications if you are not bothered by them, or use them to track places where there may be problems.

For the code you posted, a simple solution would be to change the conditional expression to

 if(isset($_GET['confirm']) && <list of OR conditions>) 

Therefore, PHP does not perform a conditional evaluation if there is no confirmation index.

+3
source

isset() is a useful function. It returns true if the variable exists and false if not. Usually, people use it in combination with a superglobal, for example $_GET or $_POST , to determine whether another page on the site is sent to you - this allows you to create various actions based on where your user is from and what data is tagged together. It also prevents errors when trying to use variables that you have not yet defined, such as OP. Therefore, instead of writing two different .php files and worrying about sending your user to the wrong one, you can do it all on one page.

Jay, I will be careful in your use of some of these calls. <?php more likely to work than <? . I heard that session_start () should be the first thing that is needed for the browser, or may cause header problems. And yes, you must have a variable declared before using it - if you do not enter [file].php?confirm=[some number] as your URL, your page will break if you do not change it to allow breaks .

+3
source

That, since the confirm query string does not seem to be set, you can check it as:

 ini_set('display_errors', true); error_reporting(E_ALL); var_dump($_GET['confirm']); 
+2
source

All Articles