Solved

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

Posted on 2008-10-26
4
2,752 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
fcardinaux earned 250 total points
ID: 22806434
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
ID: 22806463
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
ID: 22807753
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
ID: 22807906
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

738 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