Welcome to WebmasterWorld Guest from 54.160.131.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

redirect not finding required link

picking default with no value

     

smallcompany

1:07 am on Mar 16, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



I have redirect scripts like this:

$link="http://www.example.com/"; //this is if the variable is not found
if ($g == "link1") {$link = "http://www.example.com/";}
if ($g == "link2") {$link = "http://www.example.com/";}
if ($g == "link3") {$link = "http://www.example.com/";}

header("Location: $link");


Some are small, some are big.

I noticed that sometimes the right link does not get picket, so the end redirect is the default page without $g value (the top one).

Why would this happen?

Thanks

gsmith

1:30 am on Mar 16, 2010 (gmt 0)

10+ Year Member



Could it be that an unexpected value of $g is being input?

Instead of the series of "if" statements, why not try

if () {}
else if () {}
...
else {something that would notify you of an unexpected value}

smallcompany

7:00 am on Mar 16, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Could it be that an unexpected value of $g is being input?


No.

I use proven link and get redirects onto two different locations: expected and unexpected.

Please note that the unexpected happens only occasionally, can't really measure it.

Thanks

Anyango

7:47 am on Mar 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is the value for $g trimmed ? could it be due to some white space that ocassionaly is there in its value? just a guess

gsmith

1:17 pm on Mar 16, 2010 (gmt 0)

10+ Year Member



You have to use the "if else" control structure in order to prove that things are working as expected, and to handle the unexpected.

jatar_k

2:04 pm on Mar 16, 2010 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you could add a log to record values when the default case is hit

smallcompany

2:58 am on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Thanks.

I just experienced a few in a raw. I'm not sure if IE 8 could play a role here since it's PHP (server side).

Anyhow, then I changed the "empty" value from this:

$link="http://www.example.com/"; //this is if the variable is not found

to this form:

if ($g == "") {$link = "http://www.example.com/";}

As I could see it, it was still empty, but in more defined form.

It worked right after I applied this change.

Do you agree that the two versions of empty value differ?

Thanks

Anyango

7:20 am on Mar 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep the two versions are different imho.

Your first version, which is


$link="http://www.example.com/"; //this is if the variable is not found
if ($g == "link1") {$link = "http://www.example.com/";}
if ($g == "link2") {$link = "http://www.example.com/";}
if ($g == "link3") {$link = "http://www.example.com/";}


allows for 4 different values, link1,link2,link3, and "everything else"

and your second code, allows for 5 different values

link1,link2,link3,empty, and "everything else".

Just an opinion on the question, not any explanation of whats going on :)

smallcompany

8:05 pm on Mar 20, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



and your second code, allows for 5 different values

link1,link2,link3,empty, and "everything else".


Thanks.

Are you saying that

if ($g == "") {$link = "http://www.example.com/";}

is both "empty" and "everything else"?

Anyango

6:06 am on Mar 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



its like this

$link="http://www.example.com/"; //this is if the variable is not found
if ($g == "link1") {$link = "http://www.example.com/";}
if ($g == "link2") {$link = "http://www.example.com/";}
if ($g == "link3") {$link = "http://www.example.com/";}
if ($g == "") {$link = "http://www.example.com/";}


1st value, when g is "link1"
2nd value, when g is "link2"
3rd value, when g is "link3"
4th value, when g is ""
5th value, when g is niether of the above 4


if ($g == "") {$link = "http://www.example.com/";}

is only when empty but in that code the 5th value is not being taken care of. Thats why an elseif structure would be more suited cause it would take care of all the 5 possible values.

if(g is empty)
elseif(g is link1)
elseif(g is link2)
elseif(g is link3)
elseif(g is link4)
else(this else covers "everything else")

That way you wont have unexpected outputs, 1 of your 5 provided links will always be there.