1

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.

royhowie
  • 11,075
  • 14
  • 50
  • 67
Alan DeLonga
  • 454
  • 1
  • 10
  • 27
  • Turn on `error_reporting` (php.ini or index script) and/or look into the webservers `error.log` if you got a blank page. And if it's really just the failing redirect, then look into [headers already sent](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – mario Jan 11 '12 at 01:54

4 Answers4

2

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.

davidethell
  • 11,708
  • 6
  • 43
  • 63
  • Ya, you are right about the error reporting I just have to figure out where the php.ini file is on this host. I'm new to hosting on a 3rd party server I just got it yesterday, I've been running things off of my schools servers. I will check if there are carriage returns after the braces in the include files. When you say omitting the closing tags do you mean just putting all the includes into one block instead of individual ones? I just don't understand how it works perfect on my local host but this one thing doesn't work when uploaded to their server... – Alan DeLonga Jan 11 '12 at 05:43
  • The include commands can still be as they are but the included file itself must not have any output. So assuming the included file is all pho code, just remove the closing end tag at the end of the file. – davidethell Jan 11 '12 at 07:13
0

Are you using Lynda Exercise Files? Headers must be stated before anything else in PHP, including whitespace. There is something in one of your other includes files that is violating PHP rules for headers. Your database constants should be stated in constants.php.

If you are using the Lynda ex. files, I'd suggest moving along to the next level (I think it's called PHP/MySQL Beyond the Basics), there are a lot of helpful code snippets in there.

As for solving this problem, if you want the user to be immediately redirected to the login page, include nothing but your functions file and the redirect_to. Move the other includes to a file called "initialize.php", and load that file on your login page.

dyelawn
  • 750
  • 5
  • 17
  • I was going through the Lynda tutorial, but I didn't have the exercise files so I coded as it went along. Then I modified it for my own means to be integrated as a CMS back-end to make my AJAX site more dynamic. Allowing visitors to customize their experience via a form. I read it a few times that headers have to happen before any html is rendered, but not that the header call has to be the first thing in PHP even before a space. The thing that doesn't make sense is why does it work on the localhost but not the server... – Alan DeLonga Jan 11 '12 at 05:29
  • My best guess is that during the process of putting together your code, the order of your `require_once`s got changed. Because you don't have an existing session with the remote server, it's firing some code that generates output. While working on your local server, the session was likely established, so the offending code is not generating any output. This is only my best guess, but I'd definitely suggest moving your other initialization files to one "initialize.php" and only call it once you've handled all headers. – dyelawn Jan 11 '12 at 05:38
0

If uncommenting any of the require_onces causes the page to be blank then there are either fatal errors in the required file or the fatal error is that the required file cannot be found. It sounds like you are never even getting to the redirect_to function. At the top of your file add these lines which will provide more information on the error:

error_reporting(E_ALL);
ini_set('display_errors', 1);

So your code would look like:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once("includes/session.php");
require_once("includes/functions.php");
require_once("includes/constants.php");
require_once("includes/connection.php");

define("DB_SERVER", "mysite.ipagemysql.com");
define("DB_USER", "root");
define("DB_PASS", "notreal");
define("DB_NAME", "myCMS");

redirect_to("login.php");

Also check that you are not defining DB_SERVER, DB_USER, DB_PASS, DB_NAME in any of the files you are including (such as constants.php) as defines may only be defined one time and check that nothing is being output prior to sending the header.

And remember to set display_errors to 0 after you have fixed the problem if this is a live site.

ini_set('display_errors', 0);
None
  • 5,491
  • 1
  • 40
  • 51
  • Ignore Paul. Brackets are allowed around php variables. In fact some times they are needed if the variable has illegal characters such as {$ADDRESS-STATE} – None Jan 11 '12 at 02:24
0

Thank you very much for the help and insight, because of your input I was able to figure out the issue. First it was as the stupid mistake davidethell pointed out of making sure none of the php include files have any spaces or returns

    before the <?php or after the closing ?>

Then I ran into an issue where I was encountering this error on every page:

Warning: Cannot modify header information - headers already sent by...

But it was complaining about a line number in an include file that was 3 lines after the last line in the actual file. I searched around and found that if in every displayed page I put:

    <? ob_start(); ?> 

at the very start of the file (before includes) then at the very end I put:

    <? ob_flush(); ?> 

That took the error message away and everything still runs fine. I didn't take the time to look up what that function does, I am going to look it up now. I'm pretty sure it has something to do with buffering. Which would explain why it seems to slow down the interactions on the page, mostly in page redirection. But that might just be the lag from the server.

I am new to php and deploying on a server I am not familiar with. Hopefully if you had similar problems you found some of this helpful.

royhowie
  • 11,075
  • 14
  • 50
  • 67
Alan DeLonga
  • 454
  • 1
  • 10
  • 27