howto: let associates link to multiple pages

This is an archive of old posting to the User Forum

howto: let associates link to multiple pages

Postby scott raymond » Tue Apr 23, 2002 5:19 pm

In December someone here asked if it was possible for associates to link
directly to multiple pages on a site, without having to set up multiple
associate accounts. It's certainly a useful feature in a lot of cases; we
sell music, so our associates would often want to link directly to certain
artists or albums, not just the homepage.

I think I've found a way to do it, so I thought I'd through it out here for
the general benefit... or, if I've overlooked some fatal flaw you can see,
I'd appreciate the tip.

When setting up a new associate account in the back office, you'll set their
"Store page" URL to a new file that you'll create; in my case, I'm using
PHP, but you could use any dynamic environment: perl, ASP, coldfusion i
suppose, whatever. In my case, each associate's "Store page" is set to
"http://mystore.com/associategateway.php".

Now, just create that file. It needs to look at the environment for the
variable containing the referer URL, which will be the actual URL that the
user linked to. You can then parse the URL and pick out any GET values it
contains, and redirect the user accordingly. Here's the PHP script I wrote.

////////
$referer=$HTTP_SERVER_VARS[HTTP_REFERER];
if(!$referer) { // if there's no referer, just go home
$loc='/';
} else {
$url=parse_url($referer);
$parts=split('&', $url['query']);
foreach($parts as $part) {
list($k, $v) = split('=', $part);
if($k=='artist') {
$loc="/artist.php?id=$v";
} else if($k=='album') {
$loc="/album.php?id=$v";
} else if($k=='channel') {
$loc="/channel.php?channel=$v";
} else {
$loc='/';
}
}
}
Header("Location: $loc"); // causes an http redirect
exit;
////////

As you can see, in my case I'm checking to see if certain variable names are
passed as GET values in the URL, and then assembling the correct URL for
them. A more general approach, which I'll leave as an excercise for the
reader, would be to URL-encode an actual path and/or filename, and simply
redirect the user to that path. With such a script, your associate could
link directly to your product X with a URL of this form:

http://yourstore.com/cgi-bin/sb/ref.cgi ... tename&url
=productx.html

Obviously, this implementation requires PHP, but the idea should be portable
to any environment.

One possible problem: I'm not sure how the referer URL is passed. If it
comes from the server, this should always work; if it comes from the
browser, it's possible that some browsers wouldn't report it, or wouldn't
report it the same way. If anyone knows the answer, I'd appreciate it.

Hopefully this is helpful.

:scott raymond
sco@grassrootsmusic.com
scott raymond
 

Re: howto: let associates link to multiple pages

Postby loren_d_c » Tue Apr 23, 2002 6:06 pm

Scott,

Good idea.

I don't think that you would need to encode a URL value if you were to pass it
like that, unless there was something funky in the URL like a space in the
filename, which is a bad practice anyway (or perhaps if you wanted your script
to redirect to yet ANOTHER script with options in the URL for the second
script). I would think passing a full URL like

&url=http://www.mydomain.com/product1.html

would work for most people. Then you can always specify the full url to redirect
to right in the link you give the associates.

I think that the way environment variables work is that they are shared between
the browser and the webserver, and they can each supress certain variables, so
really all you can do is make sure that it works on your server with the major
browsers. If the particular environment variable doesn't exist and you have
programmed a default, then you aren't any worse of than you were before without
this script. I think the referrer is a pretty standard variable though, so there
shouldn't be a problem.

Perl is generally on more servers than PHP (in fact pre-v6 ShopSite required
that Perl 5 be installed on the server). Perl and PHP are very similar, in fact
I am going to plagiarize your parsing in my Perl sample and hope it will work.
Here is my untested Perl rendition (in other words, nobody yell at me if this
doesn't work right off the bat).

#!/usr/bin/perl

use CGI;

$referer=$ENV{'HTTP_REFERER'};
if(!$referer) { // if there's no referer, just go home
$loc='/';
} else {
$url=parse_url($referer);
$parts=split('&', $url['query']);
foreach($parts as $part) {
list($k, $v) = split('=', $part);
if($k=='artist') {
$loc="/artist.php?id=$v";
} else if($k=='album') {
$loc="/album.php?id=$v";
} else if($k=='channel') {
$loc="/channel.php?channel=$v";
} else {
$loc='/';
}
}
}

print redirect ( -URL => $loc );
1;



-Loren


scott raymond wrote:

In December someone here asked if it was possible for associates to link
directly to multiple pages on a site, without having to set up multiple
associate accounts. It's certainly a useful feature in a lot of cases; we
sell music, so our associates would often want to link directly to certain
artists or albums, not just the homepage.

I think I've found a way to do it, so I thought I'd through it out here for
the general benefit... or, if I've overlooked some fatal flaw you can see,
I'd appreciate the tip.

When setting up a new associate account in the back office, you'll set their
"Store page" URL to a new file that you'll create; in my case, I'm using
PHP, but you could use any dynamic environment: perl, ASP, coldfusion i
suppose, whatever. In my case, each associate's "Store page" is set to
"http://mystore.com/associategateway.php".

Now, just create that file. It needs to look at the environment for the
variable containing the referer URL, which will be the actual URL that the
user linked to. You can then parse the URL and pick out any GET values it
contains, and redirect the user accordingly. Here's the PHP script I wrote.

////////
$referer=$HTTP_SERVER_VARS[HTTP_REFERER];
if(!$referer) { // if there's no referer, just go home
$loc='/';
} else {
$url=parse_url($referer);
$parts=split('&', $url['query']);
foreach($parts as $part) {
list($k, $v) = split('=', $part);
if($k=='artist') {
$loc="/artist.php?id=$v";
} else if($k=='album') {
$loc="/album.php?id=$v";
} else if($k=='channel') {
$loc="/channel.php?channel=$v";
} else {
$loc='/';
}
}
}
Header("Location: $loc"); // causes an http redirect
exit;
////////

As you can see, in my case I'm checking to see if certain variable names are
passed as GET values in the URL, and then assembling the correct URL for
them. A more general approach, which I'll leave as an excercise for the
reader, would be to URL-encode an actual path and/or filename, and simply
redirect the user to that path. With such a script, your associate could
link directly to your product X with a URL of this form:

http://yourstore.com/cgi-bin/sb/ref.cgi ... tename&url
=productx.html

Obviously, this implementation requires PHP, but the idea should be portable
to any environment.

One possible problem: I'm not sure how the referer URL is passed. If it
comes from the server, this should always work; if it comes from the
browser, it's possible that some browsers wouldn't report it, or wouldn't
report it the same way. If anyone knows the answer, I'd appreciate it.

Hopefully this is helpful.

:scott raymond
sco@grassrootsmusic.com
loren_d_c
 
Posts: 2571
Joined: Fri Aug 04, 2006 12:02 pm
Location: Anywhere

Re: howto: let associates link to multiple pages

Postby scott raymond » Wed Apr 24, 2002 9:31 am

Naturally, I've already found a small bug in my script. It seems that
ref.cgi tacks on some additional GET values sometimes, causing my script to
fail, because it's only looking at the last key/value pair in the string.

Here's an updated version of the script, with the original quoted below for
comparison.

Regards,
:sco


////////
$referer=$HTTP_SERVER_VARS[HTTP_REFERER];
$loc='/';
if($referer) {
$url=parse_url($referer);
$parts=split('&', $url['query']);
foreach($parts as $part) {
list($k, $v) = split('=', $part);
if($k=='artist') {
$loc="/artist.php?id=$v";
} else if($k=='album') {
$loc="/album.php?id=$v";
} else if($k=='channel') {
$loc="/channel.php?channel=$v";
}
}
}
Header("Location: $loc"); // causes an http redirect
exit;
////////


////////
$referer=$HTTP_SERVER_VARS[HTTP_REFERER];
if(!$referer) { // if there's no referer, just go home
$loc='/';
} else {
$url=parse_url($referer);
$parts=split('&', $url['query']);
foreach($parts as $part) {
list($k, $v) = split('=', $part);
if($k=='artist') {
$loc="/artist.php?id=$v";
} else if($k=='album') {
$loc="/album.php?id=$v";
} else if($k=='channel') {
$loc="/channel.php?channel=$v";
} else {
$loc='/';
}
}
}
Header("Location: $loc"); // causes an http redirect
exit;
////////
scott raymond
 


Return to User Forum Archive

Who is online

Users browsing this forum: No registered users and 40 guests