Solved

Why won't my php header location redirect work?

Posted on 2012-04-04
12
593 Views
Last Modified: 2012-04-05
I'm trying to make a page that uses php (without mysql) to determine whether a user is logged in or not, and to direct that user to the appropriate page. If the php detects a cookie, then it takes the user to a pictures page, and if not, it sends the user to a login page. But the code below isn't working. I'm getting this error message below in my browser. Any idea what I'm doing wrong?

Warning: Cannot modify header information - headers already sent by (output started at /home/prebek/public_html/wdi/page7/page7.php:9) in /home/prebek/public_html/wdi/page7/page7.php on line 11

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>Log in</title>
	<link type="text/css" rel="stylesheet" href="page7.css" />	
</head>
<body>
	
<?php 
	if($_SESSION['loggedin']=true){
		header("Location:pictures.php");}
	else{
		header("Location:login.php");
		exit();}
?>

	<div class="containerDiv">
		<div class="headerDiv">
			<h1>My Photo Blog</h1>		
		</div>		
		<div class="contentDiv">
				
		</div>	
	</div>
</body>
</html>

Open in new window

0
Comment
Question by:shampouya
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 1

Expert Comment

by:hintco
Comment Utility
<?php 
	if($_SESSION['loggedin']=true){
		header("Location:pictures.php");}
	else{
		header("Location:login.php");
		}
?>

Open in new window

I suggest to omit the function "exit();" after the header location and it well be fine.
Please try it.

:)
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 250 total points
Comment Utility
try placing this at the top of your page:
<? ob_start(); ?>

then at the bottom of the page place this line of code:

<? ob_flush(); ?>
0
 

Author Comment

by:shampouya
Comment Utility
Hmmm, still didn't seem to work. Here is the error it's showing me on my website:

website
0
 
LVL 1

Expert Comment

by:hintco
Comment Utility
Put the code with the final modification here.
0
 
LVL 1

Expert Comment

by:hintco
Comment Utility
/page7/page7.php

look at your theme's functions.php file
clear the empty space or line break at beginning of file or end of file (or both)

try it.
0
 
LVL 17

Expert Comment

by:sonawanekiran
Comment Utility
problem causing a "headers already sent" is any kind of character in your php-File that stands before the first

<?php 
	if($_SESSION['loggedin']=true){
		header("Location:pictures.php");}
	else{
		header("Location:login.php");
		
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>Log in</title>
	<link type="text/css" rel="stylesheet" href="page7.css" />	
</head>
<body>
	


	<div class="containerDiv">
		<div class="headerDiv">
			<h1>My Photo Blog</h1>		
		</div>		
		<div class="contentDiv">
				
		</div>	
	</div>
</body>
</html>

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:hintco
Comment Utility
small correction, u seems forget the end }
<?php 
	if($_SESSION['loggedin']=true){
		header("Location:pictures.php");}
	else{
		header("Location:login.php");
		}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>Log in</title>
	<link type="text/css" rel="stylesheet" href="page7.css" />	
</head>
<body>
	


	<div class="containerDiv">
		<div class="headerDiv">
			<h1>My Photo Blog</h1>		
		</div>		
		<div class="contentDiv">
				
		</div>	
	</div>
</body>
</html>

Open in new window

0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
Comment Utility
remove white spaces and try like this,

<?php
if($_SESSION['loggedin']=true){
      header("Location:pictures.php");}
else{
 header("Location:login.php");            
?>
0
 
LVL 3

Expert Comment

by:gnemi
Comment Utility
PHP headers have to be called before any output. That includes doctype and any html tags.

http://php.net/manual/en/function.header.php
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
a page that uses php (without mysql)...
That is not really the right way to do this sort of thing.  Here is an article that explains client authentication in PHP.  Please read it over carefully so you understand what you must do to password-protect a web page.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391.html

The warning you're seeing happens because it is a law of HTTP that all headers must come before, and be complete before, any browser output whatsoever.  Cookies are headers. The PHP session uses cookies.  And your script must use session_start() before it can find any useful information in the $_SESSION array.  The first line of the script posted above is an HTML "doctype" statement, and that is browser output.  This will cause the cookie to fail, and by the domino effect, the session will not work correctly.  If it were my problem, I would put the PHP at the top, before the browser output, maybe something like this.
<?php 
// ALWAYS SHOW ALL THE ERROR, WARNING, AND NOTICE INFORMATION
error_reporting(E_ALL);

// ALWAYS START THE SESSION ON EVERY SCRIPT
session_start();

// WHEN YOU TEST FOR EQUIVALENCE USE TWO EQUAL SIGNS
if($_SESSION['loggedin'] == true)
{
    header("Location: pictures.php");
    die();
}
else
{
    header("Location: login.php");
    exit();
}
?><!doctype html>
<!--  ... WAHTEVER YOU WANT HERE ... -->

Open in new window

HTH, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Just one last note then I will sign off on this question.  If the value of $_SESSION['loggedin'] is not empty and non-zero, the test for TRUE will probably work the way you want.  But to be sure, please read this page about the difference between equal and equivalent.  My comment above should really have said EQUALITY instead of EQUIVALENCE.
http://us.php.net/manual/en/language.operators.comparison.php

Best of luck with your project, ~Ray
0
 

Author Closing Comment

by:shampouya
Comment Utility
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The viewer will learn how to dynamically set the form action using jQuery.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now