Quick Search
Sunbelthost Hosting - Colocation and Dedicated Servers

Threads: 21,763
Posts: 83,205
Members: 24,315
Newest member: joemccue

Web Hosting


 
  #1  
Old 01 Mar 2010, 06:02 AM
nmyster nmyster is offline
Newbie
 
Join Date: Feb 2010
Location: Scotland
Posts: 4
Why cant i re-direct users!

I am making a blog, it allows for a user to read stories and an admin to add, edit and delete stories. What i want to do is put a login page if the user has not already...

What i do is if a user accesses the page that is for admins, and they dont have access (a cookie thats created when they login) then it will re-direct them to the login page...

When i try and re-direct the user, using the code below:
<?php
$cookie = $_COOKIE["auth"];
if (!$cookie =="denied")
{
header("Location:./loginpage.php");
}
?>

i get the error: "Warning: Cannot modify header information - headers already sent by etc"

What do i do to fix this error!!!
Reply With Quote
  #2  
Old 03 Mar 2010, 01:55 AM
visionstudio visionstudio is offline
Established Member
 
Join Date: Feb 2010
Posts: 18
Headers are sent to the browser before any content. The error message suggests the you have outputted content to the browser before the header.

Make sure that you dont have anything printed to the page before the header.

An alternative method would be to use output control.

http://www.php.net/manual/en/intro.outcontrol.php

Adding <?php ob_start(); ?> to the very top of your page should resolve the issue.
Reply With Quote
  #3  
Old 03 Mar 2010, 11:21 AM
Alan's Avatar
Alan Alan is offline
Moderator
 
Join Date: Feb 2007
Location: Ireland
Posts: 843
Quote:
Originally Posted by visionstudio View Post
Headers are sent to the browser before any content. The error message suggests the you have outputted content to the browser before the header.

Make sure that you dont have anything printed to the page before the header.

An alternative method would be to use output control.

http://www.php.net/manual/en/intro.outcontrol.php

Adding <?php ob_start(); ?> to the very top of your page should resolve the issue.
You will have to call ob_clean(); just before the header function. Also, you should call exit(); after the header function, just to avoid any further code execution before the redirection takes place.

PHP Code:
<?php
ob_start
(); // Creates a new output buffer

echo 'oh oh... here\'s some text.'// Goes into the buffer rather than being sent to the client

if($someCondition)
{
    
ob_clean(); // Deletes the contents of the buffer

    
header('Location: http://www.anothersite.com'); // Redirection
    
exit();
}

echo 
'There was no redirection.';

ob_end_flush(); // Sends the contents of the buffer to the client (Implicit but you should stick it in anyway)
exit();
?>
__________________
“The best thing about a boolean is even if you are wrong, you are only off by a bit.”

Last edited by Alan; 03 Mar 2010 at 11:35 AM.
Reply With Quote
  #4  
Old 04 Mar 2010, 12:44 AM
mtimofiiv's Avatar
mtimofiiv mtimofiiv is offline
Established Member
 
Join Date: Feb 2010
Location: Vancouver, Canada
Posts: 19
There is also another possibility.

PHP Code:
<?php
$cookie 
$_COOKIE["auth"];
if (!
$cookie =="denied")
{
header("Location:./loginpage.php");
}
?>
is there a space (the SPACE character I mean) after the <?php and BEFORE the new line? If so try removing this. This is a well known culprit that causes those exact header errors.
Reply With Quote
Reply

Tags
blog, cookie, denied, login, php

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Does this site sucks too busy/clutter? retxedz Website Design Reviews 14 11 Aug 2010 09:01 AM
Question, making a profile for users with custom content.. cjay175 General Questions 0 04 Apr 2008 11:48 PM
Allowing users to sell on my site? NickPaul General Questions 2 13 Mar 2008 08:22 AM


All times are GMT -5. The time now is 01:14 PM.


 

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Forums Copyright © 2004-2010, WebDevForums.com. Web design by Miami Web Design. All Rights Reserved.