Page 1 of 1

Redirect stress

Posted: Thu Jan 24, 2008 6:09 am
by jameshm26
Hi! Im having some problems with getting a redirect to work and for the life of me i have no idea why. Ive got a function been pulled in from a separate php page, and ive also got a session page all set up.

What i want to happen is for the page to redirect to login if you try to get to a page that you have got to by logging in.
Basically when i clear cookies and refresh staff.php i get this message:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\widget_corp\includes\header.php:10) in C:\wamp\www\widget_corp\includes\functions.php on line 23

Here are the function and staff page code:

function.php:

***** PLEASE USE THE [CODE] TAG *****

Code: Select all

    function redirect_to( $location = NULL ) {
        if ($location != NULL) {
            header("Location: {$location}");
            exit;
            }
        }
 
staff.php:

Code: Select all

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php 
    if (!isset($_SESSION['user_id'])){
        redirect_to("login.php");
        }
    ?>
 
Be great if anyone could shed some light on this. Thanks!

Re: Redirect stress

Posted: Thu Jan 24, 2008 6:22 am
by VladSun
Read your error messages ;)
You have echoed something at C:\wamp\www\widget_corp\includes\header.php:10
You can't send headers if you had echoed something.

Re: Redirect stress

Posted: Thu Jan 24, 2008 11:49 am
by Kieran Huggins
even whitespace can prevent you from sending more headers - a common practice is to use "output buffering" to solve that particular issue.

ole showed me a neat little trick as well: if you leave the closing php tags off the end of your file you eliminate the risk of there being whitespace included. How cool is that?

Re: Redirect stress

Posted: Thu Jan 24, 2008 12:05 pm
by JAM
I'd personally only adapt ob_start() and similiar until I fixed the issue on line 10 in the header.php file. Code just shouln't produce notices.
Headers is often a mess to deal with, but often very easy to fix.

Re: Redirect stress

Posted: Thu Jan 24, 2008 5:44 pm
by Jonah Bron
In other words, you have to use your redirect_to function before you include those other files, or echo anything.

Re: Redirect stress

Posted: Thu Jan 24, 2008 7:17 pm
by thinsoldier
if your text files are encoded as utf-8 there might be a byte order mark character somewhere at the beginning or end of your include files. To aviod this use UTF-8-No BOM if available in your text editor

Re: Redirect stress

Posted: Thu Jan 24, 2008 7:49 pm
by s.dot

Code: Select all

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
Inbetween your closing tag ?> and your opening tag on the next line <?php there new line characters. You can't have those in there before performing a header().