Forum Moderators: coopster

Message Too Old, No Replies

php header

         

spica42

4:50 am on Nov 28, 2004 (gmt 0)

10+ Year Member



<body>

<?php
header("Location: [example.com");...] /* Redirect browser */

?>
</body>

This page is suppose to redirect to another page right? Anyone knows why doesn't it work?

olwen

4:57 am on Nov 28, 2004 (gmt 0)

10+ Year Member



Headers have been sent with the <body>.

You need the header before anything else. Like:


<?php
header("Location: [example.com");...] /* Redirect browser */
?>

Make sure there are no blank lines at the start.

Salsa

5:04 am on Nov 28, 2004 (gmt 0)

10+ Year Member



It looks like you're trying to call the header in the middle of an html page. You have to call header before any output is sent to the client. PHP: header - Manual [us2.php.net]

spica42

5:58 am on Nov 28, 2004 (gmt 0)

10+ Year Member



How do I do it if I want to redirect a page when it meets an if condition then.

For example,

if( a == b){

goto (http://example.com); <------ what can I use for this?

}

olwen

6:19 am on Nov 28, 2004 (gmt 0)

10+ Year Member



You need to do the condition first.

<?php
if(a == b){
header (http://example.com);
exit;
}
?>
<html>.....

You might also want:


header ('HTTP/1.0 301 Moved Permanently');

spica42

1:26 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



I tried...

<?php
if ($b % 11 == 1) {
header ("Location: [webpage1");...]
exit;
}

if ($b % 11 == 2) {
header ("Location: webpage2");
exit;
}
?>

<html>
.
.
.
<?php
Find out what is $b;
?>
.
.
</html>
------------------------------------------

Problem is when $b == 1, the page does not redirect. However, when I just add <?php header ("Location: [webpage1");?>...] right at the top of the page, it redirects correctly.

How do I get it to redirect properly?

eltreno

2:19 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



That should work if your condition is true

Try simplifing first then build into it

ie

<?php
if (1 == 1) {
header ("Location: [webpage1");...]
exit;
}

if (2 == 2) {
header ("Location: webpage2");
exit;
}
?>

This way you are sure it's dropping into the if statement

Trent

spica42

1:55 am on Dec 2, 2004 (gmt 0)

10+ Year Member



That should work if your condition is true
Try simplifing first then build into it

ie

<?php
if (1 == 1) {
header ("Location: [webpage1");...]
exit;
}

if (2 == 2) {
header ("Location: webpage2");
exit;
}
?>

I tried that and it works.

However, I when I did this, it didn't work. I need it to redirect to webpage1 in this case:

<?php
if ($b == 1) {
header ("Location: [webpage1");...]
exit;
}

if ($b == 2) {
header ("Location: webpage2");
exit;
}
?>

<html>
.
.
.
<?php
$b = 1;
?>
.
</html>

Salsa

4:15 am on Dec 2, 2004 (gmt 0)

10+ Year Member



Where does $b come from before the header calls? From GET or POST? Try adding this test line just before your if.

echo "\$b = $b<br>\n"; 
if ($b == 1) {
header ("Location: [webpage1");...]
exit;
}

ergophobe

9:36 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can't send any output whatsoever before sending a header. So what do you do if you need to get part way through the script before you know whether or not you need to send a header?

- save all output in variables and plug them into a template and send output only once you have everything completely sorted out

- use output buffering [php.net]

Salsa

6:34 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



You can't send any output whatsoever before sending a header.

Owlen and I pointed that out in msgs #2 and #3. Most recently I was just suggesting the test echo to see what value $b contains before trying to call the headers. When debugging why a function call isn't working as you wish, who cares if you create an known error while you're trying to figure out why? Maybe:

if (!isset($b) ¦¦!$b ¦¦ $b > 2) die("\$b not correctly defined: \$b == $b");

...would be better, but I'm not so concerned about spending extra time making error-free tests when I'm trying to debug permanent errors.

My real question was, if spica is meaning to get $b into the script via GET or POST or SESSION..., that's where the problem is.

I wish you well.

ergophobe

8:02 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Owlen and I pointed that out in msgs #2 and #3.

Er... I wasn't expressing myself well at 3:00am and didn't actually mean that as a response to your post Salsa. Of course, I do the same for error checking and totally agreed with your suggestion.

I just didn't see where anyone had mentioned ways of putting off output until after all processing was done. I just wanted to pass that on for spica's future reference.

So yes, spica, where does the value for $b come from (get, post, cookie, session, db query) and is there a default value for it set no matter what?

Tom

spica42

1:19 am on Dec 3, 2004 (gmt 0)

10+ Year Member



It comes from a from where people can key in variables into a text box. After that the script will manipulate the variable to get $b.

spica42

1:44 am on Dec 3, 2004 (gmt 0)

10+ Year Member



Actually I think it's getting a little complicated for me. Anyway for pass the $b value into 'var b' in javascript?

I know I can do this in javascript, it'll look something like

pass $b into var b;

var out = b;
if (b ==1){ out == page1)
if (b ==2){out == page2)

location.href = out + ".php";

ergophobe

4:28 am on Dec 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Did you try to echo out the value of $b as Salsa suggested? What do you get?

I'm guessing now that you have a page with a field like this

<input type="text" name="b">

and you're trying to access that data on the next page by using $b whereas you need to by using $_POST['b'] (or $_GET['b'] as the case may be).

So on the page that you're having problems with, add these lines

echo "<br><br>Value of \$b is: $b";
echo "<br><br>Value of \$_POST['b'] is:" . $_POST['b'];
exit;

See what that prints.

Tom

spica42

5:17 am on Dec 3, 2004 (gmt 0)

10+ Year Member



I did try to echo $b. THe value prints fine.

ergophobe

8:38 am on Dec 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So...

- the value of $b prints out fine
- the header works to redirect when you have the condition as "if (1==1)";
- but when $b == 1, you can't get it to work.

Try this

if ($b == 1)
{
echo "<br>B equals 1!<br>";
}

Also, if you don't have error_reporting set to E_ALL, put this line at the *very top* of you script

error_reporting(E_ALL);

What happens now?

Tom

spica42

2:12 am on Dec 4, 2004 (gmt 0)

10+ Year Member



It doesn't go into the if loop at all when I do
if ($b == 1)
{
echo "<br>B equals 1!<br>";
}

I'm not sure how I can do it.
First I place this right at the top of my script,
if ($b == 1)
{
echo "<br>B equals 1!<br>";
}

//THen somewhere below I do this-->
$b = 1

Will the echo above still print? I dun think so...

[edited by: ergophobe at 4:41 am (utc) on Dec. 4, 2004]
[edit reason] full quote of previous post snipped - quote tags were incorrect [/edit]

ergophobe

4:47 am on Dec 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, naturally, you must assign the value then use it. You can't use a value that hasn't been assigned yet.

I think we need to back this discussion out a bit and ask you to summarize in a paragraph or so what you're trying to achieve in general.

- What does the script do?
- What determines the value of $b

Tom

spica42

8:00 am on Dec 4, 2004 (gmt 0)

10+ Year Member



Basically I need the user to input something through a text field. This value will be converted to $b.

If $b == 1, redirect the page to page1, if $b == 2, redirect to page2 etc....

Thanks for all the help..

DaButcher

10:11 am on Dec 4, 2004 (gmt 0)

10+ Year Member



Hi,

I see that the examples here lack one thing that may cause browser problems in some versions of IE.

When using the header-location, one should use this code:


header("HTTP/1.1 301 Moved Permanently");
header("location: [domain.tld...]
header("Connection: close");

the conncetion:close prevents some issues with IE.

I will however make one suggested version for you now.
ps. is it so that the user inputs 1, then it redirects to 1.php?

or is it so that the pages are stored in an array, then the user inserts 1, and it redirects to $page[$user_input]?

Can you please specify this?
How do you want it to work? eg. what will the users input, and what will the files be named?

I will eat breakfast now, so I'll BBL in maybe 10-15 minutes.

I have already a sollution for you, but it's in my head :P I have to code it and test it, before I present it to you.

but please, tell me more about your wanted functionality.

sincerely,
Olav Alexander Mjelde

DaButcher

11:17 am on Dec 4, 2004 (gmt 0)

10+ Year Member



ok, now I've played around a bit:

<?php
function redir($goto = 404, $timeout = 0, $basepath = "") {
if(is_file($basepath . $goto)) {
if($timeout == 0) {
echo "header:location";
}
else {
echo "meta refresh";
}
}
else {
echo "Sorry, the file was not found!";
}
} // end function re_dir
?>

if you call the function with only one parameter, it will use header-location.
if you call it with a countdown, it will use meta-refresh.

ps. I did not implement the meta-refresh or header-location, as I gave them above.

I think it's easier for you to understand the logic, if you first read it like this, not too advanced.

ps. you might want to check if the filename has some slash (/) in it, as then it might be used for abuse!

you might also want to specify an redirection path.. eg. basepath for redirections.

If you specify more how you want it to work, I can again give you some ideas and thoughts.

good luck,
Sincerely,
Olav Alexander Mjelde

spica42

2:16 am on Dec 5, 2004 (gmt 0)

10+ Year Member



<form name="form1" method="post" action="<?php $php_self?>">
<p>Enter number
<input type="text" name="a">
(enter number 1 or 2) </p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>

<?php
$b = $_POST['a'];

if ($b == 1) {
header ("Location: page1");
exit;
}

if ($b % 11 == 2) {
header ("Location: page2");
exit;
}

?>

I know header:Location must be place at the top of the page, but how do I write a code to achieve this?

baze22

2:39 am on Dec 5, 2004 (gmt 0)

10+ Year Member



You just can't have any output before the header is called. So as long as your html is below the php and you php doesn't output anything, you'll be OK.

<?php
$b = $_POST['a'];

if ($b == 1) {
header ("Location: page1");
exit;
}

if ($b % 11 == 2) {
header ("Location: page2");
exit;
}
?>
<form name="form1" method="post" action="<?php $php_self?>">
<p>Enter number
<input type="text" name="a">
(enter number 1 or 2) </p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>

baze