[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 604
  • Last Modified:

Why won't my php header location redirect work?

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
shampouya
Asked:
shampouya
  • 4
  • 2
  • 2
  • +3
2 Solutions
 
hintcoCommented:
<?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
 
sonawanekiranCommented:
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
 
shampouyaAuthor Commented:
Hmmm, still didn't seem to work. Here is the error it's showing me on my website:

website
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
hintcoCommented:
Put the code with the final modification here.
0
 
hintcoCommented:
/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
 
sonawanekiranCommented:
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
 
hintcoCommented:
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
 
Loganathan NatarajanLAMP DeveloperCommented:
remove white spaces and try like this,

<?php
if($_SESSION['loggedin']=true){
      header("Location:pictures.php");}
else{
 header("Location:login.php");            
?>
0
 
gnemiCommented:
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
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
shampouyaAuthor Commented:
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!

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now