Jump to content

Redirect help and SSL Help


phpfan28

Recommended Posts

I having trouble with my re-direct code on my php registration form. The form does submit data to the database but I'm getting the following error.

 

"Warning: Cannot modify header information - headers already sent by (output started at /homepages/25/d232402382/htdocs/testencourage/registration.php:15) in /homepages/25/d232402382/htdocs/testencourage/registration.php on line 87"

 

I would like the form goes to the thanks.php after submitting the data but its not doing its job. Here is the code.

 

<?php 

if ($_SERVER['HTTPS']) { 
header('location: https://www.fakewebsite.com'); 
} 

the regular html syntax <title>, site  navigation, etc...

?> 


<?php 

if (isset($_POST['submitted'])){ 
     
$fields = array( 
    'email', 
    'state', 
    'district', 
    'gender', 
    'age', 
    'profession', 

); 

if (safe($_POST['survey']=="Yes")){ 
    $survey = "Yes"; 
} 
else{ 
    $survey = "No"; 
} 
     

foreach($fields as $fieldName) { 
    if(isset($_POST[$fieldName]) and safe(trim(stripslashes($_POST[$fieldName]))) !==''){ 
        $$fieldName = safe(trim(stripslashes($_POST[$fieldName]))); 
    }else { 
         
                $errors[] = "Please enter your". $fieldName .""; //code to validate fields 
    } 
} 
if(!isset($errors)){ 
     
require_once('Connections/encourage.php'); 

$query = "INSERT INTO participants (email, state, district, gender, age, profession, survey, registration_date)
VALUES ('$email', '$state', '$district', '$gender', '$age', '$profession','$survey', NOW())"; //databasse connection 
    
    $result = mysql_query ($query);      



if ($result){ 
     
    $url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); 
     
    if ((substr($url, -1) == '/') || (substr($url, -1) == '\\')) { 
        $url = substr ($url, 0 -1); 
    } 
     
    $url .= '/thanks.php'; 
    header("Location: $url");// this is line 87 
    exit(); 
     
    }else{ 
     
    echo '<h1 id="mainhead">System Error</hl> 
    <p>Your registration could not be completed due to a system error We apologize for any incovience</p>';//gives system error 
    echo 'p' . mysql_error(). '<br /><br />Query: ' . $query . '</p>'; 
    exit(); 
     
    } 
    mysql_close(); 
     
    } else { 
         
        echo '<h1 id="mainhead">Error!</h1> 
        <p class="error">The following error(s) occurred:<br />'; 
        foreach($errors as $msg) { 
            echo " - $msg<br/>\n"; 
        } 
        echo '</p><p>Please try again.</p><p><br/></p>'; 
    } 

} 

function safe($string) 
    { 
        $pattern = "/\r|\n|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i"; 
        return preg_replace($pattern, '', $string); 
    } 
     
?>

Also I'm going to set up my ssl on the server so I'm planning to use that https redirect syntax but since I'm very new at this, am I missing something. Your help will be greatly appreciated, thanks!

Link to comment
Share on other sites

 

Basically you cannot echo anything before using the header function. You cannot send one header out to the client in the form of echo, then sent out a header to relocate in between. You can relocate, or, echo... that is what the error is telling you.

 

Also, after header function it is always best to exit(); your php scripts... prevents anything from running any further causing unforeseen bugs.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.