Jump to content


Photo

defining variables in links within other links


  • Please log in to reply
4 replies to this topic

#1 kigoe

kigoe
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 August 2006 - 10:47 PM

I've got a rather simple script for links, where all my links point to a single PHP page (I'll call it the link redirection script), which then redirects users to the intended link (definened in the variable link) and records their passage in a MySQL database, so I can see which links are the most popular. This has, up until this point, worked quite well; the structure is such:

http://mydomain.com/link.php?e=i&link=index.php

where e indicates an external or internal link and link indicates the intended URL (in the above case, relative to the root directory, since its an internal link).

However, I run into problems when I try to specify a link to another PHP page with variables defined in the inteneded URL. For instace, if I were to link to index.php?foo=bar&user=happy, or something, the link would look like this:

http://mydomain.com/link.php?e=i&link=index.php?foo=bar&user=happy

PHP then interprets this link as contating more variables for use by the link redirection script, and sends me to the page index.php?foo=bar, leaving out the subsequent &user=happy or any other variables defined afterward.

Is there any way for me to write this link so that only the e and link variables are used by the link redirection script, and the foo and user variables are ignored -- until they get to the intended PHP page?

thanks!

-nik

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 17 August 2006 - 10:51 PM

A better way would be to store the actual links (addresses) in a database, then just have your links pass an id to your script which loads the url from the database and does the redirect.

#3 kigoe

kigoe
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 August 2006 - 10:55 PM

eh - too tedious for me. that requires a) inserting every link I want to use into the database and b) pulling these ids everytime I want to link to something. I could make a page with, say, a short form to add links above and a list of all links and their ids below, for reference, but simply making them and looking them up seems a bit overly tedious.

thanks for the suggestion, though.

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 17 August 2006 - 11:25 PM

what you need to do is replace those characters in the url to be directed to. A browser would recognize as a variable by the &x=y.

So replace & with somthing trivial like || and = with |

so your link would look like this

http://mydomain.com/...bar||user|happy

Then in your redirection page you can do this....

$find = array(
'/\|\|/',
'/\|/'
);
$rep = array(
'&',
'='
)

$link = pregreplace($find,$rep,$_GET['link']);

This would yield:

$link = 'index.php?foo=bar&user=happy';

you may even want to do similar with the '?' in the link variable....
follow me on twitter @PHPsycho

#5 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 17 August 2006 - 11:55 PM

You make a database call to record that a link was clicked.

How much harder is it to do one more query right below the first and grab that same record, grab the URL and make a header() call to it.

???

Doesn't sound like that much more work.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users