0

I'm looping through my database in a while loop, displaying data like this:

while($data = mysqli_fetch_array($mysql))
{
  <a href='" . $data['link'] . "';
}

what I would like is for the link target to change when the link is clicked to

<a href='/links.php?id=" . $data['id'] . "';

So if you copy link location you will get http://google.com but if you click the link on the site you will get to links.php?id=1

Hope anyone has a good idea :)

Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35
Gruffer
  • 1
  • 1
  • 4
  • 6
    So basically you are tricking your users into thinking they are clicking on a link to google but in actual fact you are sending them somewhere else? Do you think your users will enjoy this behavior? Maybe I'm missing the point.. why exactly are you wanting to mislead your users like this? – Lix Nov 19 '14 at 15:02
  • add the `id` as a data attribute and change the href when clicked using a click handler – charlietfl Nov 19 '14 at 15:06
  • use javascript onclick with window.location.href = 'http://www.google.com'; – ponciste Nov 19 '14 at 15:07
  • There's nothing malicious going on here guys. I want to record link clicks with links.php but I also want to be able to copy link location so that people can share the original link directly. Right now the only way to share the links is to click on them yourself, then share the link. If you copy link location all you get is the links.php?id link which doesn't even work for non-members. If I can set it up like described above it would simplify link sharing and you could share links without messing up your click counter. – Gruffer Nov 19 '14 at 15:12
  • I was very surprised when I tried building something like this (purely for educational purposes) and figured how easy it is to make this. I think this a serious flaw in browser security and should be fixed asap. – icecub Nov 19 '14 at 15:36
  • possible duplicate of [Changing a links href after click with jQuery](http://stackoverflow.com/questions/16425260/changing-a-links-href-after-click-with-jquery) – GG. Nov 19 '14 at 15:56

1 Answers1

-1

if as what you said (There's nothing malicious)

$('a').on('click', function(){
var url = $(this).attr('href');
$(this).attr('href','/links.php?id='+ url);
});
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28