Jump to content

Archived

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

kigoe

defining variables in links within other links

Recommended Posts

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 [b]link[/b]) 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:

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

where [b]e[/b] indicates an external or internal link and [b]link[/b] 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 [i]with variables defined in the inteneded URL[/i]. For instace, if I were to link to [b]index.php?foo=bar&user=happy[/b], or something, the link would look like this:

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

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

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

thanks!

-nik

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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/link.php?e=i&link=index.php?foo|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....

Share this post


Link to post
Share on other sites
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 [b]header()[/b] call to it.

???

Doesn't sound like that much more work.

Share this post


Link to post
Share on other sites

×

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.