Forum
Can I use PHP header redirects above the Perch runtime inclusion?
I've added some code to my homepage to handle a redirect back to the homepage from a plug-in I'm using to handle a transaction flow:
<?php
if (!empty($_GET['payment_ok'])) {
if ($_GET['payment_ok'] !== null) {
header("Location:reservation-processed.php?succeeded=1");
}
}
if (!empty($_GET['payment_error'])) {
if ($_GET['payment_error'] == 1) {
header("Location:reservation-processed.php?failed=1");
}
else {
header("Location:reservation-processed.php?unknown=1");
}
}
?>
Basically, I get the URL token appended to the homepage URL by the plug-in, and based on that token, I redirect to another page of my own, which has a PHP conditional to output different perch_content regions depending on which of my own URL tokens has been sent. I like this little system thingy, and it works great in my dev environment.
Published live, it was throwing these PHP warnings:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/3871919/html/redesign/index.php:20) in /home/content/19/3871919/html/redesign/perch/core/lib/PerchUtil.class.php on line 1211
Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/3871919/html/redesign/index.php:20) in /home/content/19/3871919/html/redesign/perch/core/lib/PerchUtil.class.php on line 1212
Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/3871919/html/redesign/index.php:20) in /home/content/19/3871919/html/redesign/perch/core/lib/PerchUtil.class.php on line 1213
Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/3871919/html/redesign/index.php:20) in /home/content/19/3871919/html/redesign/perch/core/lib/PerchUtil.class.php on line 1219
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/content/19/3871919/html/redesign/index.php:20) in /home/content/19/3871919/html/redesign/perch/addons/apps/perch_shop/lib/PerchShop_Session.class.php on line 15
Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/3871919/html/redesign/index.php:20) in /home/content/19/3871919/html/redesign/perch/addons/apps/perch_shop/lib/PerchShop_Session.class.php on line 34
I disabled the warnings from being output. I tested and saw that it still works on the live website!
Am I doing something inadvisable, by messing with the PHP headers before Perch gets a chance to do its thing?
I notice Simon Clay and Jannis Borgers were doing PHP header redirects below Perch runtime inclusion a couple years back:
https://forum.grabaperch.com/forum/12-05-2014-redirect-parent-to-subpage
You need to send headers before anything is output to the screen - so any whitespace is "something". The runtime does not output anything so is neither helping nor hindering you, I imagine you have some whitespace in one of your files.