Home > Mobile >  execute iplog.php on click of links
execute iplog.php on click of links

Time:12-09

i am logging visitors IP by the following code below

$iplogfile = 'ips.txt';
    $ipaddress = $_SERVER['REMOTE_ADDR'];
        $webpage = $_SERVER['SCRIPT_NAME'];
            $timestamp = date('d/m/Y h:i:s');
            $browser = $_SERVER['HTTP_USER_AGENT'];
        $fp = fopen($iplogfile, 'a ');
        chmod($iplogfile, 0777);
    fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$webpage.' '.$browser. "\r\n");
fclose($fp);

but i want it to be execute only when user clicks on this particular links

<a href="tel: 1 (000) 000-0000" >Book Now</a>
<a href="tel: 1 (111) 111-1111" >Call Now</a>

can someone help me out here? i cannot figure out how to acheive it :(

Thanks alot for your help and input.

CodePudding user response:

You can call a function using onclick attribute in the HTML anghor tag. onclick="myFunction()"

<a onclick="myFunction()" href="tel: 1 (000) 000-0000" >Book Now</a>
<a onclick="myFunction()" href="tel: 1 (111) 111-1111" >Call Now</a>

<script type='text/javascript'>

function myFunction()
{
    // add your code here
}

</script>

CodePudding user response:

In order to execute this on clicking particular links then you should follow one of the following ideas as example:

1- Query strings, You can add a URL query parameter so you can check if it exists or not using $_GET, If it exists you can call your code.

2- You can create a new PHP file, Let's name it (track.php), And let's use AJAX to send requests to track.php on clicking the links you define using a class as example.

3- You can check what is the current path and URL and to set conditions using STRPOS to define whether the URL has the page you need to run the function for or not.

  • Related