The problem with your original logic was that PHP is a server-side language; it doesn't know what a 'link' is, let alone when a user 'clicks' one. All PHP code is executed on the server, and then the output is sent to the browser.
Don't forget to mark this thread resolved, if it is.