Solved

Header location does not work

Posted on 2015-01-13
1
71 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
[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
1 Comment
 
LVL 35

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

Industry Leaders: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

623 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