Solved

Header location does not work

Posted on 2015-01-13
1
62 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

743 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

21 Experts available now in Live!

Get 1:1 Help Now