unanswerable
unanswerable

Reputation: 23

Redirecting Request Never Completes - Anthing wrong with PHP script?

To be frank, I am not a coding freak. Basically, This php script validates Name & Email field(s) for a 'Newsletter Subscription' form. Well, everything seemed to work fine on all browsers. After 2 days, this script stopped working on IE9 & Firefox 14-16. It works fine on Chrome. Could you guys please let me know, what is causing this error?

php script is named signup.php

The 'echo script alerts' in the code doesn't seem to work on FF & IE9. It works absolutely fine on Chrome. This is exactly my problem.

Error on Firefox - Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies.

Action Point on Firefox - All necessary action was taken to counter this error. Nothing worked out.

Error on IE9 - It simply doesn't respond and stays on the same page.

<?php

ob_start();
session_start();

/*Name Validation.*/
function checkName($name) 
{
$nAccept = array("&", "’", " ",  "-");

if (!ctype_alpha(str_replace ($nAccept, "", $name)))
   {
        return TRUE;
    }
}

/*Email Validation.*/
function checkEmail($email) 
{
   if(mb_eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
   {
      return FALSE;
   }
    if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/i", $email))
    {

        return false;
    }

   list($Username, $Domain) = explode("@",$email);

   if(getmxrr($Domain, $MXHost)) 
   {
      return TRUE;
   }
   else 
   {
      if(fsockopen($Domain, 25, $errno, $errstr, 30)) 
      {
         return TRUE; 
      }
      else 
      {
         return FALSE; 
      }
   }
}


function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    }
    else {
        return false;
    }
}

if(checkName($_REQUEST['name']) == TRUE) {
echo "<script>alert('Please enter a Valid Name.');history.back();</script>";
}

elseif (!isset($_POST['email'])) {
echo "<script>alert('Please ensure you have completed all fields before submitting the form. No fields to be left blank.');history.back();</script>";
}

elseif (empty($_POST['name']) || empty($_POST['email'])) {
echo "<script>alert('Please ensure you have completed all fields before submitting the form. No fields to be left blank.Type in only one valid email address.');history.back();</script>";
}

elseif ( isInjected($_POST['email']) ) {
echo "<script>alert('Please ensure you have completed all fields before submitting the form. No fields to be left blank.Type in only one valid email address.');history.back();</script>";
}

elseif(checkEmail($_REQUEST['email']) == FALSE) {
echo "<script>alert('Entered E-Mail is Invalid or The E-Mail does not belong to a valid domain.');history.back();</script>";
}

else{

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="connect_members_temp"; //table name

mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); 
mysql_select_db("$db_name")or die("cannot select DB");

$confirm_code=md5(uniqid(rand()));

$name=$_POST['name'];
$email=$_POST['email'];

$sql="INSERT INTO $tbl_name(confirm_code, name, email)VALUES('$confirm_code', '$_POST[name]', '$_POST[email]')";
$result=mysql_query($sql);

}
mysql_close();

if($result)
{

// send e-mail to
$to= 'me@localhost';
$thankyou_page = "before_subscription.html";
$email_page = "email_connectivity.html";
$data_page = "email_not_found.html";

$subject="Confirm Your Subscription with XXXXX";


$header="from: XXXXX <[email protected]>";

$message="Your Confirmation link \r\n";
$message.="Click on this link to confirm your subscription \r\n";
$message.="http://localhost/XXXXX/confirmation.php?passkey=$confirm_code";

// send email
$sentmail = mail($to,$subject,$message,$header);

}

else
{
header( "Location: $data_page" );
}

if($sentmail)
{
header( "Location: $thankyou_page" );
}
else{
header( "Location: $email_page" );
}

?>

Here is the HTML Form

<form action="signup.php" method="post">
        <fieldset>
          <legend>Digital Newsletter</legend>
          <div class="fl_left">
            <input type="text" name="name" value="Enter name here&hellip;" onfocus="this.value=(this.value=='Enter name here&hellip;')? '' : this.value ;" />
            <input type="text" name="email" value="Enter email address&hellip;" onfocus="this.value=(this.value=='Enter email address&hellip;')? '' : this.value ;"/>
          </div>
          <div class="fl_right">
            <input type="submit" name="newsletter_go" id="newsletter_go" value="&raquo;"/>
          </div>
        </fieldset>
      </form>

Upvotes: 1

Views: 226

Answers (1)

Barmar
Barmar

Reputation: 781210

You can't do a redirect and also echo messages on the page. When validation fails, you should echo the message and then exit.

The reason you're getting the redirection error is because of the problem GBD mentioned. When validation fails, you never go into the else clause that sets $data_page, but then you do header("Location: $data_page"); anyway.

Also, after you do that, you then go into the code that sends either $thankyou_page or $email_page. You can only have one Location: header. You need to exit after sending the redirection that you want.

Upvotes: 2

Related Questions