Вопрос

I am trying to integrate PayPal in my site but after I press the PayPal button this error appears:

Warning: Cannot modify header information - headers already sent by (output started at X:\home\test\www\view\main.php:29) in X:\home\test\www\view\frontend\paypalfunctions.php on line 377

The part of the main.php responsible for the error is this:

<div class="container">
        <?php require 'view/frontend/'.$tpl.'.php';?>
</div>

This line of code is essential for my whole website to work. It conflicts with this particular function from PayPal's paypalfunctions.php:

function RedirectToPayPal ( $token )
{
global $PAYPAL_URL;

// Redirect to paypal.com here
$payPalURL = $PAYPAL_URL . $token;
header("Location: ".$payPalURL);
exit;
}

I carefully read this question about the issue and tried some things, but did not find a solution. Is there anything I need to change in the code for it to work?

Это было полезно?

Решение 2

ob_start is a work-around. I prefer fixing the actual problem. It's usually a white space issue.

Try adjusting the following...

<div class="container">
        <?php require 'view/frontend/'.$tpl.'.php';?>
</div>

to

<div class="container">
<?php require 'view/frontend/'.$tpl.'.php';?>
</div>

See if that helps. Also make sure you don't have any extra white space going on in the included file.

Другие советы

The problem is, that your view is outputting content and headers.

A solution is to buffer the output with ob_start and ob_get_contents(). That stops all direct output from the view file. You might then output the buffer, when needed. And view output is not needed, before doing the paypal redirect via header.

So the logic is: render the view buffer OR do a header redirection.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top