PHP Header Redirect Issue

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1242 Views - Last Post: 28 November 2012 - 10:34 AM Rate Topic: -----

#1 MixedUpCody  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 24-December 11

PHP Header Redirect Issue

Posted 26 November 2012 - 12:33 PM

Hey all,

I'm trying to make this simple PHP script for work function correctly, but I keep getting this error when I try to run it from the actual site (it works fine on local host).

The error is:
Warning: Cannot modify header information - headers already sent by (output started at /home/cmccorma/public_html/index.php:15) in /home/cmccorma/public_html/index.php on line 17

I thought it was a white space issue at first so I tried to eliminate all white space, but I'm not having any luck.

Here is the code:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Test</title>
	</head>
	<body>
		<p>Now, we must verify that you are, indeed, a linguist!</p>
		<br />
		<p>Whats is the last name of the linguist most associated with the Linguistic Determinism hypothesis?</p>
				
		<form>
			<input type="text" name="name"><br />
			<input type="submit" rext="submit"/>
		</form><?php $name = strtolower($_GET["name"]);
			if ($name=="whorf" || $name=="sapir"){
			header("Location: correct.html");
			}else{
			header("Location: incorrect.html");
			}?></body>
</html>



Any ideas? Thanks for your help.

Cody

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Header Redirect Issue

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3382
  • View blog
  • Posts: 9,569
  • Joined: 08-June 10

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 12:53 PM

see List of common PHP problems, first item.
Was This Post Helpful? 4
  • +
  • -

#3 MixedUpCody  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 24-December 11

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 06:33 PM

Dormilich,

I read the FAQ you posted, but I'm still not getting it. What am I doing wrong?

Cody
Was This Post Helpful? 0
  • +
  • -

#4 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 400
  • View blog
  • Posts: 1,711
  • Joined: 30-April 10

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 06:50 PM

There is HTML being sent before the location redirect header.
From PHP.net:

Quote

header() is used to send a raw HTTP header. See the HTTP/1.1 specification for more information on HTTP headers.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Was This Post Helpful? 1
  • +
  • -

#5 MixedUpCody  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 24-December 11

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 07:33 PM

So should I put the php code before the <head> tag, or before something else?
Was This Post Helpful? 0
  • +
  • -

#6 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 400
  • View blog
  • Posts: 1,711
  • Joined: 30-April 10

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 07:51 PM

Try it.
Was This Post Helpful? 1
  • +
  • -

#7 MixedUpCody  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 24-December 11

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 08:13 PM

laytonsdad and Dormilich, thank you very much! I got past that problem. But now, it redirects to "incorrect.html" automatically without giving the user a chance to fill out the input form. I know this is because it is evaluating the PHP before it gets to the HTML, but I can't figure out the solution. I had it working on my local host just fine, but now it won't function the way I want. Any ideas? Sorry I have so many questions, I've never used PHP before. Thanks again.

Cody
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • Joined: 08-August 08

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 08:25 PM

Do you see what's wrong with this pseudocode below?
If(A) {
header...
} else {
header...
}
some html here

It's always going to redirect, so the html after it will never be seen!

Why are you redirecting?
Was This Post Helpful? 1
  • +
  • -

#9 MixedUpCody  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 24-December 11

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 08:30 PM

CTphpnweb,

The whole point was to test the user's input and then redirect them to the appropriate page depending on their response. I originally had the php after the input form, and that worked fine on my lamp server, but when I uploaded it to the site it stopped working.
Was This Post Helpful? 0
  • +
  • -

#10 MixedUpCody  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 24-December 11

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 09:04 PM

Okay, I got it working. Thank you so much for all of your help. You all are the best.

Cody

<?php
	if(isset($_GET["name"])){
		test();
	}	
	function test(){
		$name = strtolower($_GET["name"]);
		if ($name=="whorf" || $name=="sapir"){
		header("Location: correct.html");
		}else{
		header("Location: incorrect.html");
		}
	}
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Test</title>
	</head>
	<body>
		<p>Now, we must verify that you are, indeed, a linguist!</p>
		<br />
		<p>What is the last name of the linguist most associated with the Linguistic Determinism hypothesis?</p>		
		<form>
			<input type="text" name="name"><br />
			<input type="submit" text="submit"/>
		</form>
	</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3382
  • View blog
  • Posts: 9,569
  • Joined: 08-June 10

Re: PHP Header Redirect Issue

Posted 26 November 2012 - 11:19 PM

there is one more improvement you can do.
<?php
	if(isset($_GET["name"])){
		test();
	}	
	function test(){
		$name = strtolower($_GET["name"]);
		if ($name=="whorf" || $name=="sapir"){
			include "correct.html";
		}else{
			include "incorrect.html";
		}
	}
?>

Was This Post Helpful? 1
  • +
  • -

#12 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 400
  • View blog
  • Posts: 1,711
  • Joined: 30-April 10

Re: PHP Header Redirect Issue

Posted 27 November 2012 - 11:12 AM

If you can include its better so you don't break the connection and create a new one, which is what header relocating does.
Was This Post Helpful? 1
  • +
  • -

#13 MixedUpCody  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 24-December 11

Re: PHP Header Redirect Issue

Posted 27 November 2012 - 06:23 PM

Oh. Why is breaking the connection to re-direct not ideal?
Was This Post Helpful? 0
  • +
  • -

#14 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 400
  • View blog
  • Posts: 1,711
  • Joined: 30-April 10

Re: PHP Header Redirect Issue

Posted 27 November 2012 - 06:35 PM

if you don't have to its best. It saves bandwidth for those that pay my GB and such.
using an include will keep the same connection to the server and it wont have to make a call for a new page.
Was This Post Helpful? 1
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • Joined: 08-August 08

Re: PHP Header Redirect Issue

Posted 28 November 2012 - 08:06 AM

It's not just the bandwidth for the user. It's slower, so it adds to the work your server needs to do. Think of it this way (I'm making up numbers here):

You have 10 people using your site, but your server can only handle 8 simultaneous connections.
When you redirect you send a message back to the browser and wait for a response if the connection doesn't close, although exit should do that. If the connection is left open that's wasted time that could be going to another user. If it's closed, then it takes time to close and time to reopen when the browser reconnects. Either way, it takes time you can't afford. If you include you immediately send back the needed page so you can close the connection and handle the next request.

This isn't to say there aren't times when redirecting is necessary. It just shouldn't be your preferred method.
Was This Post Helpful? 3
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2