Disclosure: I make WaBio at Murabyte, which is mentioned at the end.
Click counters look easy: add one to a number when someone clicks. The details decide whether the numbers are right and whether the link is safe. Here is a small PHP redirect that does both.
The idea
Instead of linking straight to the destination, your page links to go.php?id=12 . The script counts the click, then redirects.
<? php $id = ( int ) ( $_GET [ 'id' ] ?? 0 ) ; $st = $pdo -> prepare ( "SELECT url FROM links WHERE id = ? AND active = 1" ) ; $st -> execute ( [ $id ] ) ; $url = $st -> fetchColumn ( ) ;
if ( ! $url || ! preg_match ( '#^https?://#i' , $url ) ) { http_response_code ( 404 ) ; exit ( 'Not found' ) ; }
$pdo -> prepare ( "INSERT INTO clicks (link_id, day, n) VALUES (?, CURDATE(), 1) ON DUPLICATE KEY UPDATE n = n + 1" ) -> execute ( [ $id ] ) ;
header ( 'Location: ' . $url , true , 302 ) ; exit ;
Enter fullscreen mode
Exit fullscreen mode
Why it is written this way
Atomic counter.…