Forum Moderators: coopster
<?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?
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.
<?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?
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
That should work if your condition is true
Try simplifing first then build into itie
<?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>
echo "\$b = $b<br>\n";
if ($b == 1) {
header ("Location: [webpage1");...]
exit;
}
- 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]
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.
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
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
- 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
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]
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
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
<?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
<?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?
<?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