Link to home
Start Free TrialLog in
Avatar of shampouya
shampouya

asked on

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

Avatar of hintco
hintco

<?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.

:)
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shampouya

ASKER

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

website
Put the code with the final modification here.
/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.
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

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

remove white spaces and try like this,

<?php
if($_SESSION['loggedin']=true){
      header("Location:pictures.php");}
else{
 header("Location:login.php");            
?>
PHP headers have to be called before any output. That includes doctype and any html tags.

http://php.net/manual/en/function.header.php
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
thanks