Jump to content

[SOLVED] header() function


snorky

Recommended Posts

I can't get the header() function to work correctly.

 

on datamine2.php there is a form after a bunch of stuff happens. That part works as expected.

      <form name="end_of_report" action="datamine4.php">

<input type="radio" name="nextone" value="Quit" />Quit<br />

<input type="radio" name="nextone" value="Another" />Run Another Report<br /><br />

<input type="submit" value="go!" />

      </form>

when the user selects "Quit' or "Another" from the form, datamine4.php opens and runs

datamine4.php has no HTML and therefore no display

 

The following is all of the code in datamine4.php

<?php

global $nextone;

// create a var

$chosen1 = "another";

// get input from form on datamine2.php

$chosen2 = $_GET["nextone"];

So far, so good.

 

// test input from datamine2.php and run another report (datamine1.php) or quit

if ($chosen1==$chosen2)

{

header("location:http://www.mysite.net/bbp/reports/datamine1.php");

exit;

}

else

{

header("location:http://www.mysite.net/index.shtml");

exit;

}

?>

I know that the if/else and the variables are correct, because

[*]if $chosen is "Another" a blank screen appears with the URL

http://ww2.ohsd.net/bbp/reports/datamine4.php?nextone=another

[*]if $chosen is "Quit" a blank screen appears with the URL

http://ww2.ohsd.net/bbp/reports/datamine4.php?nextone=quit

 

 

datamine4.php does the if/else part, but doesn't execute either header() call

I know that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. That is exactly what the manual says. However,

 

  • there is no HTML
  • there are no other files opened and therefore no blank lines
  • PHP doesn't send any other output before calling either of the header() commands

 

The PHP manual gives this example:

 

<?php

header("Location: http://www.example.com/"); /* Redirect browser */

 

/* Make sure that code below does not get executed when we redirect. */

exit;

?>

Link to comment
Share on other sites

Put this at the top of your page, it will tell you what the error is:

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

You may have a whitespace issue.

 

NOTE: Please use the built in


tags around you code for proper formatting/syntax highlighting.

Link to comment
Share on other sites

I know this is very simple, but whenever I use header location, location has  capital L. This might not be the issue but from my experience its the little mistakes that go unnoticed.

 

  header("Location: addeventform.php");
  exit();

Link to comment
Share on other sites

I know this is very simple, but whenever I use header location, location has  capital L. This might not be the issue but from my experience its the little mistakes that go unnoticed.

 

  header("Location: addeventform.php");
  exit();

 

You're right about the little mistakes. In this case, however, "L" didn't help.

Link to comment
Share on other sites

What do these pages output that u are redirecting to....and if error_reporting(E_ALL) does not work

try error_reporting(E_ALL ^ E_STRICT); see if you get anything.

 

Also try redirecting to another blank test page and see if it produces anything.

Link to comment
Share on other sites

I know this is very simple, but whenever I use header location, location has  capital L. This might not be the issue but from my experience its the little mistakes that go unnoticed.

 

  header("Location: addeventform.php");
  exit();

 

Headers are case insenstive. You could use LoCaTiOn: if you really wanted. But yes, I'd use Location: cause it just looks better.

Link to comment
Share on other sites

Put this at the top of your page, it will tell you what the error is:

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

You may have a whitespace issue.

 

NOTE: Please use the built in


tags around you code for proper formatting/syntax highlighting.

 

Those were already set globally to On & 1 respectively. Adding or removing from this script made no difference: I still don't see error messages.

Link to comment
Share on other sites

What about the error_reporting?  Did it output anything?  Are you sure you don't have any HTML whitespace seeping through?

 

There is no white space. I even tried removing the comments.

Link to comment
Share on other sites

What do these pages output that u are redirecting to....and if error_reporting(E_ALL) does not work

try error_reporting(E_ALL ^ E_STRICT); see if you get anything.

 

Also try redirecting to another blank test page and see if it produces anything.

I attempted to redirect to a different (blank) page.

 

Same result.

 

Cleared IE cache and tried in FireFox (which had never seen that site). Same error:

Warning: Cannot modify header information - headers already sent by (output started at /home/.sites/28/site1/web/bbp/reports/datamine4.php:1) in /home/.sites/28/site1/web/bbp/reports/datamine4.php on line 8

Link to comment
Share on other sites

Can you put this after $chosen2?

 

var_dump($chosen2);

 

What did you get?

 

string(7) "another"

+

Warning: Cannot modify header information - headers already sent by (output started at /home/.sites/28/site1/web/bbp/reports/datamine4.php:1) in /home/.sites/28/site1/web/bbp/reports/datamine4.php on line 9

Link to comment
Share on other sites

That's due to output/whitespace before you call the header function.  Please post the code from the last header call to the top of the page, in


tags.

 

I don't see what qualifies as output/whitespace:

	<?php
		error_reporting(E_ALL ^ E_STRICT);
		global $nextone;
		$chosen1="another";
		$chosen2=$_GET["nextone"];
		var_dump($chosen2);
		if ($chosen1==$chosen2)
			{
				header("Location:http://ww2.ohsd.net/bbp/reports/datamine1.php");
				exit; 
			}
		else	
			{
				exit; 
			}	
	?>

 

Here's a sticky for an in depth explanation:

 

http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

Link to comment
Share on other sites

Whitespace:

 

         error_reporting(E_ALL ^ E_STRICT);
         global $nextone;
         $chosen1="another";
         $chosen2=$_GET["nextone"];
         // var_dump($chosen2); DON'T NEED THIS
         if ($chosen1==$chosen2)
            {
               header("Location:http://ww2.ohsd.net/bbp/reports/datamine1.php");
               exit; 
            }

Link to comment
Share on other sites

Also, you got $chosen1 wrong. Capital A?

The value passed from the form is all lower case: "another"

     <form name="end_of_report" action="datamine4.php">
		<input type="radio" name="nextone" value="quit" />Quit<br />
		<input type="radio" name="nextone" value="another" />Run Another Report<br /><br />
		<input type="submit" value="go!" />
      </form>

Link to comment
Share on other sites

And the winner is.....

 

Whitespace:

 

<?php // move this in
         error_reporting(E_ALL ^ E_STRICT);
         global $nextone;
         $chosen1="another";
         $chosen2=$_GET["nextone"];
         // var_dump($chosen2); DON'T NEED THIS
         if ($chosen1==$chosen2)
            {
               header("Location:http://ww2.ohsd.net/bbp/reports/datamine1.php");
               exit; 
            }

 

I changed the depth of the indentation. Not eliminated, just reduced the overall indentation until there was zero whitespace in front of <?php.

 

Thanks to all. I learned several techniques in this exercise, and I made it through without resorting to my meds.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.