Forum Moderators: coopster

Message Too Old, No Replies

php redirect with switch

switch php redirect

         

dvagnieres

12:20 am on Jul 7, 2005 (gmt 0)



Hello there,
I am trying to redirect different urls to specific urls using switch and header commands. Here is my code.. I know it looks stange but I am not a programer and really need help.
Thanks,
D

switch ($Header)
{
    case Header(location:"http://www.1.com"):
Header(Location:"http://www.main.com/index1.html");
        break;
    case Header(location:"http://www.2.com"):
        Header(Location:"http://www.main.com/index2.html");
        break;
case Header(location:"http://www.3.com"):
        Header(Location:"http://www.main.com/index3.html");
        break;
case Header(location:"http://www.4.com"):
        Header(location:"http://www.main.com/index4.html");
        break;
default:
        Header(Location:"http://www.main.com/index.html");

jatar_k

1:44 am on Jul 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld dvagnieres,

what is going wrong?
are you getting errors?
is it not behaving properly?
is it doing nothing?

ergophobe

7:49 pm on Jul 7, 2005 (gmt 0)

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



If you aren't getting error messages all over, you should change your PHP error settings. I would think you would be getting fatal errors


Header(location:"http://www.example.com")

This should give you a parse error. It needs to be

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

Even if you do that, though, your case statement will evaluate to the return value of the header function (which is void, so there is no return value in reality), not to a string of characters. So if your $header variable is set to "www.1.com" and you have the switch statement

switch ($header)
{
case header("Location: http://www.example.com"):
}

That is the same as

switch ($header)
{
case :
}

and I don't actually know what happens there (error, warning, notice, or silent), but it will never trigger that case.

You probably want something more along the lines of

switch ($header)
{
case "example.com":
case "example2.com":
case "example3.com":
header("Location: [$header");...]
exit;
default:
echo "Sorry, where was that you wanted to go?";
}