?
Solved

Why won't my php header location redirect work?

Posted on 2012-04-04
12
Medium Priority
?
600 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 111

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

777 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