Solved

Header location does not work

Posted on 2015-01-13
1
68 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

860 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