Client-side vs Server-side scripting
Here, it is important to understand the difference between client-side scripting and server-side scripting. PHP code runs on the server, so once this code gets called, it will do server-side stuff like database queries and generate the relevant HTML code and sends it to the user's browser (client-side) and the PHP code's role ends here.
Now to track user's browser behavior while the page is open, you will need Javascript code, that runs on the user's browser (client-side), and tracking user clicks can be easily done using Javascript's onclick
event:
$(".link").on("click", function() {
console.log(this.id + " has ben clicked");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="page.php" id="link1" class="link">Link 1</a>
<a href="page.php" id="link2" class="link">Link 2</a>
<a href="page.php" id="link3" class="link">Link 3</a>
Or, using PHP:
If you want your page.php
script to see which link called it, you need to pass some extra parameter to the url, as Alex W. mentioned in his answer, so your URL would become something like:
page.php?callerLink=1
And then you can get this paramter from PHP, you would use something like:
$callerLink = $_GET["callerLink"];