views:

1536

answers:

6

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.%5Bsite%5D.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.

+17  A: 

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

Josh
+1. OP is experiencing sloppy coding practices, nothing more.
Lucas Oman
+3  A: 

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.

Johrn
Josh
+3  A: 

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.

tuergeist
I'd rather suggest a switch...case, but this is admittedly better than the original script.
nikc
A: 

it was HelpFul. Thanks.

Sachin
+1  A: 

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']);
Sarfraz
A: 

hey guys,

how important is it to have the isset() function there? what does it do?

Husein Poonawala

Husein Poonawala