Solved

Why won't my php header location redirect work?

Posted on 2012-04-04
12
595 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
ID: 37809608
<?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
ID: 37809615
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
ID: 37809621
Hmmm, still didn't seem to work. Here is the error it's showing me on my website:

website
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Expert Comment

by:hintco
ID: 37809624
Put the code with the final modification here.
0
 
LVL 1

Expert Comment

by:hintco
ID: 37809628
/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
ID: 37809634
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
 
LVL 1

Expert Comment

by:hintco
ID: 37809642
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
ID: 37809645
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
ID: 37809648
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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 37809655
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 109

Expert Comment

by:Ray Paseur
ID: 37809661
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
ID: 37809784
thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to count occurrences of each item in an array.

813 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

15 Experts available now in Live!

Get 1:1 Help Now