• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2782
  • Last Modified:

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

- 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] 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!
  • 3
1 Solution
Try this:
if(!empty($_SESSION['security_code']) && !empty($_POST['security_code']) && ($_SESSION['security_code'] == $_POST['security_code']))  
// ...

Open in new window

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']).
Shahzad Fateh AliWeb Solutions Architect & Technical Project Manager- VentureDive (Pvt) LtdCommented:
Hi Star,

Alternatively, you can get rid of these messages by turning off your error reporting.
to do so.

For further reference visit

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now