exit link tracking / urlencode help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
admedia
Forum Newbie
Posts: 1
Joined: Mon Nov 28, 2005 6:45 am

exit link tracking / urlencode help!

Post by admedia »

I need to be able to log users clicking on external links, and i was provided with a PHP script to link to the outside ->
The first example is what i put where the Link is
Code:

<A href='exit.php?url=www.insertlinkhere.com'>


Then the code within the exit.php file is
Code:

Code: Select all

<?php
  $url = @$_GET["url"];
  header("Location: http://".$url);
  exit;
?>
So i have used this script but since my links are quite sophisticated using other link tracking etc.. so i found out that i'm supposed to use the PHP "urlencode" function, now i'm a PHP Beginner and i thought that i should put the urlencode in like this

Code:

Code: Select all

<?php
  $url = @$_GET["url"];
  urlencode($url);
  header("Location: http://".$url); 
  exit;
?>

but it doesn't work?! am i doing something wrong? it passes through properly i think but it doesn't actually go to the page?!

the external links that i use are similiar to this format:

http://www.sampleurl.com/x/?s=r&t=u&v=X ... 1&p=000000

so the PHP urlencode is supposed to change the ampersands i assume and pass it on thru the exit.php and refresh to the actual external link.
Hope i haven't confused.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: exit link tracking / urlencode help!

Post by foobar »

Please use [ PHP ] tags around your code! ;)

You can only urlencode the parameter, not the actual URL. What urlencode allows you to do is pass a URL as a _paremeter_ via GET without the server thinking it's part of the _actual_ URL.

So, if you have the following URL:

http://example.com/out.php?url=http://s ... t_Id=65841

The server will think that cololor and product_Id are parameters of out.php rather than product.jsp on a different website.

What you want is:

http://example.com/out.php?url=http%3A% ... Id%3D65841

To do that, you'll need something along the lines of:

...in your out.php (whatever the name may be):

Code: Select all

/* Since the URL is encoded, you need to decode it! */
$url = urldecode($_GET["url"]);
header("Location: http://".$url);
exit;
...and in your calling script:

Code: Select all

<a href="out.php?url=<?= urlencode('http://blabla.com/xyz.htm?a=b') ?>">Link Text Here</a>
Post Reply