I am trying to place a header when the user gets a particular response from the API. It works most of the time, but sometimes the header doesn't work and I get the following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/ubuntu/workspace/Payment.php:90) in /home/ubuntu/workspace/Payment.php on line 126 Call Stack: 0.0001 238200 1. {main}() /home/ubuntu/workspace/Payment.php:0 2.6037 271848 2. header() /home/ubuntu/workspace/Payment.php:126
Line 126 in this case, is the only header I have on the page,
header("Location: $ConfirmationPage", false);
I am unsure why this is happening (especially since it's not always happening). Do I have a bug somewhere in my code?
<!DOCTYPE html>
<!--
// Cardnumber = "4005550000000001";
// Expiry = "01/19";
// CVV = "124";
-->
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<meta name="format-detection" content="telephone=no" />
<title>Payment</title>
<link href="../static/css/bootstrap.css" rel="stylesheet" />
<link href="../static/css/style.css" rel="stylesheet" />
<script src="../static/js/jquery.js"></script>
<script src="../static/js/bootstrap.js"></script>
</head>
<body>
<title>Payment</title>
<div>
<?php
include("nav.php");
require_once ("./CheckoutFunctions.php");
require_once('./api/securepay.php');
$ConfirmationPage = "/Confirmation.php";
?>
</div>
<div class="col-md-9 col-xs-8 col-lg-8 col-lg-offset-1">
<form action="" method="post" class="form-inline" id="form-signin">
<div class="col-xs-12"><h2>Processing Payment</h2></div>
<div class="form-group col-xs-12">
<div class="col-xs-3 col-md-2 form-label"><label>Name on card:</label></div>
<div class="col-xs-9 col-md-6"><input type="text" name="cardname" class="form-control" placeholder="Name on Card"/></div>
</div>
<div class="form-group col-xs-12">
<div class="col-xs-3 col-md-2 form-label"><label>Card Number:</label></div>
<div class="col-xs-9 col-md-6"><input type="text" name="creditcard" class="form-control" placeholder="Credit Card"/></div>
</div>
<div class="form-group col-xs-12">
<div class="col-xs-3 col-md-2 form-label"><label>Expiry Date:</label></div>
<div class="col-xs-4 col-md-3">
<select class="form-control" name="ExpMon" title="select a month">
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">Aug</option>
<option value="09">Sept</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
</div>
<div class="col-xs-4 col-md-3">
<select name="ExpYear" class="form-control" title="select a year">
<option value="15">2015</option>
<option value="16">2016</option>
<option value="17">2017</option>
<option value="18">2018</option>
<option value="19">2019</option>
<option value="20">2020</option>
</select>
</div>
</div>
<div class="form-group col-xs-12">
<div class="col-xs-3 col-md-2 form-label"><label>CVV: </label></div>
<div class="col-xs-4 col-md-3"><input type="text" name="CVV" class="input-block-level form-control" placeholder="CVV" /></div>
</div>
<div class="col-xs-12">
<div class="col-xs-offset-2">
<button class="btn btn-large btn-primary" id="process-btn" type="submit" name="submit">Submit Payment</button>
</div>
</div>
<!--Take the form values on submit and run them through the securepay API. Print the response on the page.-->
<?php
if(isset($_POST['submit']))
{
$trans = new SecurePay();
//PLEASE SANITIZE $_POST data. We will be heavily marked down otherwise.
//THIS will need to put put into a form and posted to the confirmation page.
//get values from form and set them to variables
$creditcard = filter_var($_POST['creditcard'], FILTER_SANITIZE_NUMBER_INT);
$ExpYear = filter_var($_POST['ExpMon'] . "/". $_POST['ExpYear'], FILTER_SANITIZE_STRING);
$CVV = filter_var($_POST['CVV'], FILTER_SANITIZE_NUMBER_INT);
$trans->cardnumber = $creditcard;
$trans->expiry = $ExpYear;
$trans->cvv = $CVV;
$trans->orderId = "PO123456";
$trans->amount = str_replace(".","",urlencode("100.00"));
$responseCode = $trans->processPayment();
/* If the response code is 00 (success from securePay), send an email and redirect to confirmation page, if not display an error */
if ($trans->getLastResponseCode() == "00") {
//Email information
$to = getEmail($shId);
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header("Location: $ConfirmationPage", false);
} elseif ($trans->getLastResponseCode() == "") {
echo "<h1> Card details incorrect, please enter correct details.</h1>";
} else {
echo "<h1>".$trans->getLastResponseCode()." ".$trans->getLastResponseText()."</h1>";
}
}
?>
</form>
</div>
<div>
<?php include("sidebar.php"); ?>
</div>
<div>
<?php include("footer.php"); ?>
</div>
</body>
</html>
Use ob_flush
. In the start of the code, use
ob_start ();
After
header("Location: $ConfirmationPage", false);
ob_flush();
PHP header()
will not work if you have any output on page.
Solutions:
1) Add ob_start()
in the beginning of the page.
ob_start()
will save you output to a buffer and redirection will occur.
2) Use javascript redirection. Javascript redirection will occur even if you have any output on the page.
<?php
echo "<script>window.location.href='" . $ConfirmationPage . "';</script>";
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.