Solved

Header location does not work

Posted on 2015-01-13
1
63 Views
Last Modified: 2015-01-27
Hi,

I have a small problem with a PHP redirect, it does not work
However, the die('test') output the text okay.
The redirect simply redirect me to the frontpage instead of google.com. Why is that?

if(mysql_num_rows($conf_query) == 0) {
		
		if(isset($_SESSION['conf_id']))		
			unset($_SESSION['conf_id']);
			
		#die('test'); //this works
		header('Location: http://www.google.com');
		
	}

Open in new window

0
Comment
Question by:kgp43
1 Comment
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 40546834
header() tells the browser what to do ("Go to this next location"). If you die() before the header(), then the header code never gets executed and it will never reach the browser.

header() also needs to be executed BEFORE anything else gets sent to the browser. So if you echo() or print() or have ANY other output (even blank lines) before the header() line, it will not work. You would get an error about output already sent.

You'd have to use output buffering if this is the case (put ob_start() at the beginning of your script), which basically holds all the output in memory before sending it to the browser at the end. Since all the output is in memory and hasn't been sent, the header() line can work wherever you put it.

Finally, if you DO use a header() to redirect the browser, put a die() or exit() AFTER the header() line so that additional code isn't processed that might conflict with the redirect:

if(mysql_num_rows($conf_query) == 0) {
		
		if(isset($_SESSION['conf_id']))		
			unset($_SESSION['conf_id']);
			
		header('Location: http://www.google.com');
		die();
		
	}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

10 Experts available now in Live!

Get 1:1 Help Now