Jump to content

How to encode url and then Redirect? (affiliate link)


phpL

Recommended Posts

Hi, Iam a phpNewbie, but I want to learn. I have tried hours in search of the ultimate code for redirecting affiliate links. I also search the forums but no answer to my question.

 

For explanation purposes, Ill use a simplification of my problem:

 

I have a affliliate link that looks like this: 

http://affliliatecompany.com/c/?ab=1234&cd=products/1/2&ef=

 

I use a file called 'bye.php' and I link to the affiliate link as the following:

http://www.mysite.com/bye.php?webshop=http://affliliatecompany.com/c/?ab=1234&cd=products/1/2&ef=

 

I have a script that grabs (GET) the url (after webshop):

<?php
$u=htmlspecialchars($_GET['webshop']."&".$_GET['ab']."&".$_GET['cd']."&".$_GET['ef']); 
?>

 

Then I use an echo in the meta refresh:

<?php echo($u); ?>

 

I have 2 problems:

 

1. My affiliatecompany says that I have to send the url encode to make this work, but how?

<?php echo(rawurlencode($u)); ?> //something like this leaves url full of hexicode %2F etc

 

2. $_GET['ef'] is always empty but if so, it will not print so I use instead the following?

<?php
$u=htmlspecialchars($_GET['webshop']."&".$_GET['ab']."&".$_GET['cd']."&ef=); 
?>

 

 

I hope someone can provide me with some knowledge on how to make this work  :-\

Thank you in advance!

 

 

@tran_dinh_ba: When I urlencode the url will show with hexcode, that doesn't work. Altough the affiliate company suggested to encode the url before it is sent, but I don't know why it should be in the first place...

 

@milesap: If I use your code it will show: http://www.mysite.com/$link

I use the code below:

 

<?php
$u=htmlspecialchars($_GET['ab']."&".$_GET['cd']."&ef="); 
$link = urlencode($u);
header('Location: $link');
?>

 

Is the following possible?

<?php header("Refresh:4; URL='".urlencode($u)."'") ?>

 

Short HTTP protocol tutorial:

 

 

GET params are separated by & and go in key=val pairs.  Therefore, any = needs to be escaped as well as any &.

 

 

So, if you want to append anything that has a & or = in it to a GET string, you must urlencode() it.

 

 

If you are redirecting the client somewhere or something, but not passing it as a parameter, you do not need to urlencode it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.