I am deploying a CMS I developed on my computer to my first domain server and I am running into issues with redirecting to pages in PHP (works in javascript) using a function that calls:
header("Location: {$location}");
Everything works properly on my home machine through WAMP. But after I deployed on the server it is redirecting to a blank page. I have tested the issue in a separate file and found that it WORKS as long as only the file that contains the redirect_to function is included, so it looks like this:
<?php //require_once("includes/session.php");?>
<?php require_once("includes/functions.php");?>
<?php //require_once("includes/constants.php"); ?>
<?php //require_once("includes/connection.php"); ?>
<?php
define("DB_SERVER", "mysite.ipagemysql.com");
define("DB_USER", "root");
define("DB_PASS", "notreal");
define("DB_NAME", "myCMS");
redirect_to("login.php");
phpinfo();
?>
This is the redirect function I have defined in functions.php:
// redirects the user to the page/path specified by $location
function redirect_to($location){
if($location != NULL){
header("Location: {$location}");
exit();
}
}
Notice that I can only include the functions.php file, if I include any other the redirect no longer works, just goes to a blank page… I can copy the code that was in the included files into this file like I did above with the constants.php file which looks like this:
<?php
// Database constants
define("DB_SERVER", "mysite.ipagemysql.com");
define("DB_USER", "root");
define("DB_PASS", "notreal");
define("DB_NAME", "myCMS");
?>
I CAN load all the CMS pages regularly from the Domain server, access the DB and save to it. Since I can save to the DB I am assuming there are no issues with including any of the files I am testing to include because they are all needed to access the DB. The problem is purely the redirect. Everything works fine how it is set up running on my comp as localhost.
I have been looking up this issues and trying solutions all day. I have narrowed it down to the problem of not being able to include other files, but I don’t understand why and it’s only happening on the Domain server…
Any help or insight would be greatly appreciated, thanks for taking the time to read this.
It might help if you turn error reporting to E_ALL for the time being. It looks like you probably have some output being sent to the browser from another included file. Often the output is easy to miss like an extra carriage return after your closing ?> tag. It is best to omit the closing tags on your included files as they aren’t necessary and will prevent accidental output.
If you are getting unseen output this means the headers have already been sent by the time the Location header is sent so it doesn’t work.