Solved

How to remove this error: "PHP Notice:  Undefined index: ..."

Posted on 2008-10-26
4
2,741 Views
Last Modified: 2012-05-05
- This line in PHP:
if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) )  // <----- line 403
{
...
}

->Cause error in log file of apache:
[Sun Oct 26 13:49:48 2008] [error] [client 119.17.244.141] PHP Notice:  Undefined index:  security_code in /var/www/html/test.html on line 403

And many this errors, and LOG of apache is increase quickly

How can I resolve this problem?

Thanks in advance!
0
Comment
Question by:star6868
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
fcardinaux earned 250 total points
Comment Utility
Try this:
<?php

if(!empty($_SESSION['security_code']) && !empty($_POST['security_code']) && ($_SESSION['security_code'] == $_POST['security_code']))  

{

// ...

}

?>

Open in new window

0
 
LVL 8

Expert Comment

by:fcardinaux
Comment Utility
Here is the explanation: the empty() function does not generate any warning when the variable is not set (source: http://www.php.net/manual/en/function.empty.php). Furthermore, the condition inside the if construct is evaluated from left to right (http://www.php.net/manual/en/language.operators.php), and evaluation stops as soon as the result is known.

Thus, if $_SESSION['security_code'] or $_POST['security_code'] is not set, the third part of the condition is never evaluated ($_SESSION['security_code'] == $_POST['security_code']).
0
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
Comment Utility
Hi Star,

Alternatively, you can get rid of these messages by turning off your error reporting.
use
error_reporting(0);
to do so.


For further reference visit
http://www.php.net/error_reporting
0
 
LVL 8

Expert Comment

by:fcardinaux
Comment Utility
Shahzadfatehali,

If there is smoke in your house, do you look for fire or do you just open the windows?

Do not turn off error reporting. On the contrary, use error logs to make a more robust code.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now