Forum Moderators: coopster

Message Too Old, No Replies

redirect

         

smallcompany

5:06 am on Apr 20, 2017 (gmt 0)

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



I have a redirect script that looks like this:

if ($m == "link1") {$link = "http://www.example.com";}
if ($m == "") {$link = "http://www.example.com";}
header("Location: $link");


Now, if a redirect link has no value for "m" there's that "empty" entry at the end and that works.

But, if there's a non-existing value, redirect does not work properly, and there's an error like "The page isn’t redirecting properly"

So, what kind of code would make non-existing "m" value to redirect like there's no value?

Thanks

Peter_S

11:56 am on Apr 20, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



$m might be undefined too, or contain something else than an empty string.

I am only an amateur at PHP, but I think you can use a switch statement.

switch ( $m )
{
case 'link1' : $link='xxxx'; break;
case 'link2' : $link='yyy'; break;
default: $link='http://www.example.com'; break;
}

header("Location: $link");

Also, note that such redirection produces a 302 http code.

cffrost2

2:02 pm on Apr 20, 2017 (gmt 0)

10+ Year Member



if ($m == "link1") {$link = "http://www.example.com";}
if ($m == "") {$link = "http://www.example.com";}
header("Location: $link");


The above code does not have an ELSE statement to capture a case where the $m variable may not equal the 2 $link variables. And the location output will fire every time this script is ran because it's not encapsulated within the block of code. So the redirect will fire every time with our without a defined variable.

So header is trying to redirect to an empty $link variable.

I.E. (A fast simple solution)

if(isset($m)) {
if($m == "link1") {
$link = "http://www.example.com";
} elseif($m == "") {
$link = "http://www.example.com";
} else {
$link = "http://LinkNotFoundPage.html"; /*a catch page for any links that don't match */
}
header("Location: ".$link);
}


In the above case, if variable $m is set, and it matches either condition, then it set's the $link variable. ELSE, if there is not match, header will redirect to a page telling the user the link was no good.

There are lot's of different ways to accomplish this. A Switch would work too as peter mentioned. My pick would be store the links in an array and loop the array for a match. This allows easy expanding as the links grow (I assume they may).

Hope this helps.

Peter_S

2:50 pm on Apr 20, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



If you want to use an array like suggested by cffrost2, you can do it like that :

$list_links = array (
'link1' => 'http://xxxx' ,
'link2' => 'http://yyyy' ) ;

header ( 'Location: ' . ( isset ( $list_links [ $m ] ) ? $list_links [ $m ] : 'http://LinkNotFoundPage.html' ) ) ;

lucy24

4:59 pm on Apr 20, 2017 (gmt 0)

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



Why do you need an ELSEIF at all? Structurally it's (obviously this is not intended to be the actual php syntax, ahem)

"IF ($variable == blahblah || empty($variable)) {define $link and do stuff}
ELSE {do other stuff involving a different value of $link}"

Now, personally I tend to do it by setting a preliminary value for $link and then changing it if certain conditions are met--so there's no ELSE at all, just a shared "do stuff"--but that's probably more about individual coding style.

smallcompany

8:49 pm on Apr 20, 2017 (gmt 0)

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



Thanks very much to all. Here is what I've come up with, with your help and the help of online search:

$link = array(

/*Links*/
'e1'=>'http://www.example1.com/',
'e2'=>'http://www.example2.com/',
'e3'=>'http://www.example3.com/',

);

/*Send Headers*/
header('Content-Type: text/html; charset=utf-8');
header('X-Robots-Tag: noindex, nofollow, noarchive', true);

if (array_key_exists('m', $_GET) && isset($link[$m]))
{
header('Location: '.$link[$m]); // Valid m
}
else
{
header('Location: http://www.example.com'); // Invalid m
}

exit();


Please note that $m comes from another script which is included at the beginning of this one. I tested it and it worked fine.

Now I wonder if you have any comments? For example, do you see any issues, or what is the impact of the two HTTP tag headers?

Thank you

Peter_S

9:19 pm on Apr 20, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



The part : "if (array_key_exists('m', $_GET) && isset($link[$m]))" looks awkward to me. I would assume testing if the "m" was passed as parameters should be tested earlier. But all depend of the whole script and its purpose.

smallcompany

9:57 pm on Apr 20, 2017 (gmt 0)

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



Thank you. I wish I could say something, but my PHP knowledge is limited.

But I do have an additional question, about keys that would have same value.

For example, instead of having:
'e1'=>'http://www.example1.com/',
'e2'=>'http://www.example1.com/',


I would like something like:

'e1' || 'e2' =>'http://www.example1.com/',


Now, this does not work, and I wonder about easiest way of achieving it.

Thank you

Peter_S

10:46 pm on Apr 20, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



I am not sure I understand all your concerns but :

$link = array (
'e1'=>'http://www.example1.com/',
'e2'=>'http://www.example1.com/',
) ;

or

$link = array ( ) ;

$link [ 'e1' ] = $link [ 'e2' ] = 'http://www.example1.com/' ;

smallcompany

11:38 pm on Apr 20, 2017 (gmt 0)

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



Yes you do! Thank you!