Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Header location does not work

Posted on 2015-01-13
1
Medium Priority
?
72 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 2000 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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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 …

688 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