Forum Moderators: coopster

Message Too Old, No Replies

How Do I Pass Variables On Links Page?

         

wildguy

8:44 am on Jun 19, 2007 (gmt 0)

10+ Year Member



Hello,

My apologies if this question has been asked
in the past.

I am building a PHP links page that contains
all of the outgoing links for my site. For
example...

domain-1.com/links.php

Here is the PHP code that I am using on my
domain-1.com/links.php page...

<?PHP

$go = $_GET['go'];
if ($go == "") {$link = "http://www.domain-1.com/";}
if ($go == "GX40T") {$link = "http://domain-2.com/?masked=GX40T";}
if ($go == "YR57W") {$link = "http://domain-2.com/?masked=YR57W";}
if ($go == "PB24Y") {$link = "http://domain-2.com/?masked=PB24Y";}

header("Location: $link");
exit();
?>

Here is what I am doing...

I am loading a .php page with an URL that
includes a "masked" variable. For example,
the URL would look like this...

domain-1.com/mypage.php?masked=GX40T

I am then using PHP on that .php page to
pull the "masked" variable into the links
on that .php page. The "masked" variable
would be passed as the "go" variable in
the links. For example, the links on the
.php page would look like this...

domain-1.com/links.php?go=GX40T

I am then using the PHP code above to
pass the "go" variable back to a "masked"
variable in the outgoing link. In this
case, the outgoing link would be...

http://domain-2.com/?masked=GX40T

Here is what I would like to do...

Currently, I am forced to list list tons of
outgoing links on my domain-1.com/links.php
page because I have tons of "masked" values.

I would like to be able to insert some PHP
in the example code above to dynamically
pull whatever the original "masked" variable
is.

In effect, I would only have the following
code (it is only an example and I think it's
bad code, but hopefully you'll get the idea)...

<?PHP

$go = $_GET['go'];
if ($go == "") {$link = "http://www.domain-1.com/";}
if ($go == "<?php echo $_GET['masked'];?>") {$link = "http://domain-2.com/?masked=<?php echo $_GET['masked'];?>";}

header("Location: $link");
exit();
?>

Is this possible? If so, does anyone know
how I can easily do this?

I'm a real knucklehead with PHP. I've been
looking for hours in various forums for the
solution, but most of it is way above my
head.

Any advice would be greatly appreciated.

Thanks!

[edited by: eelixduppy at 5:02 pm (utc) on June 19, 2007]
[edit reason] delinked [/edit]

Habtom

9:54 am on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I somehow lost what you wanted, but I can help you out fix the code you have posted there.

<?php
$go = $_REQUEST['go'];
if ($go == "") {$link = "http://www.domain-1.com/";}
if ($go == $_REQUEST['masked']) {$link = "http://domain-2.com/?masked=$_REQUEST['masked']";}

header("Location: $link");
exit();
?>

Now, in brief let us know what is not working.

Habtom

wildguy

3:04 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Thank you for your response, Habtom.

Rather than having thousands of "if" lines
in the code for all of my "masked" values,
I would love to have just one "if" line
that will dynamically pull whatever the
"masked" value is.

Thanks again!

mattclayb

4:56 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



You could use this code -

<?PHP

$go = $_GET['go'];

if (&go!= ""){
header("Location: http://domain-2.com/?masked=$go");
}
else {
header("Location: http://www.domain-1.com/");
}

?>

[edited by: eelixduppy at 5:02 pm (utc) on June 19, 2007]
[edit reason] delinked [/edit]

wildguy

6:58 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Hi mattclayb,

Thanks for the code! When I used it initially,
I was receiving an error about an unexpected "&"
in line 5 of the code.

I deleted the "&" in line 5 and it worked great.
Everything is tracking well. The variables are
being passed.

Thanks again for your help. I appreciate it.

mcibor

8:06 pm on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more thing Wildguy,

before you pass variables as checked, better check them if they are what you allow:

The easiest to expand is:

<?PHP

$go = $_GET['go'];

$allowed = "GX40T, YR57W, PB24Y";//here pass all values that you allow, they have to be delimited by $delimiter

$delimiter = ", ";
$allowed_arr = explode($delimiter, $allowed);

if (in_array($go, $allowed_arr)){ //if value is in array, then pass it further
header("Location: http://domain-2.com/?masked=$go");
}
else {//if not in array, then show main page
header("Location: http://www.domain-1.com/");
}

?>

Security is not to be omitted :)
Michal

[edited by: eelixduppy at 8:13 pm (utc) on June 19, 2007]
[edit reason] delinked [/edit]

mattclayb

8:24 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



yes,

sorry about the rogue ampersand ;-) I just copied and pasted your variable and missed that off!

Also, about the secrity check, it's a good shout. But I can't see why in this instance there would be any security issues? The variable handling is quite harmless, presumably they are being converted into URL's via hardcoded PHP and no SQL calls?

If the latter is true, there would need to be other, quite simple security in place.

mattclayb

8:27 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Just to clarify. this is the code (ampersand replaced with dollar)

<?PHP

$go = $_GET['go'];

if ($go!= ""){
header("Location: http://domain-2.com/?masked=$go");
}
else {
header("Location: http://www.domain-1.com/");
}

?>

[edited by: eelixduppy at 8:30 pm (utc) on June 19, 2007]
[edit reason] delinked [/edit]

wildguy

9:13 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Hi mcibor and mattclayb,

mcibor, thanks very much for the security code.
mattclayb was correct in that I'm not using any
SQL calls (that stuff is way beyond my knowledge).

mattclayb, no problem about the rogue ampersand. :-)
I'm so happy to have the help!

Thanks again.