Redirect stress

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jameshm26
Forum Newbie
Posts: 1
Joined: Thu Jan 24, 2008 6:00 am

Redirect stress

Post 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!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Redirect stress

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Redirect stress

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Redirect stress

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Redirect stress

Post by Jonah Bron »

In other words, you have to use your redirect_to function before you include those other files, or echo anything.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Redirect stress

Post 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
Warning: I have no idea what I'm talking about.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Redirect stress

Post 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().
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply