user580950 user580950 - 1 year ago 85
PHP Question

Error Count - PHP

I have a form which has email as field name. What I am trying to do is if the email is it no equal to

is not equal to
, it should throw an error first time. The second time if the user enters wrong email id again it should always redirect to
even if the page refreshes.

It doesn't work the form always shows even if the email id is entered wrong twice.


if (!empty($_POST['email'])) {
header("Location: /error.htm");

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<body id="main_body" >

<div id="form_container">

<form id="form_1123848" class="appnitro" method="post" action="">
<div class="form_description">
<p> <input type="text" name="email" value="" /></p>
<ul >

<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />


Answer Source

You have two issues here:

1. You are defining $count as a string, and never incrementing it. If you look over your code, $count is getting specifically set to 1 every time there is a mismatch. How is supposed to ever get to 2?

2. Furthermore, data here is stateless. How is the script supposed to know what $count was set to on the previous call? You need to also set $count as a session variable so that the script will know what its previous value.

You should try updating your code to something similar to this:

// Check if `email` passed in POST request:
if ($_POST['email']) {
  $email = ""; //Manually define expected email address.

  // Check if provided email does *not* match the expected email:
  if ($email !== $_POST['email']) {
    // Record the mismatch attempt in session and increment:
    if (!($_SESSION['incorrectEmailCount'])) {
      // If this is the first mismatch, define the session variable, and set to 1. 
      $_SESSION['incorrectEmailCount'] = 1;
    } else {
      // Session variable already set due to previous mismatch. Increment it.
  // If user entered incorrect email more than once:
  if ($_SESSION['incorrectEmailCount'] > 1) { 
    // Redirect to error page and stop execution. 
    header("Location: /error.htm");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download