How to protect click to chat Whatsapp button from bot web scraping?
P粉786800174
2023-09-03 12:52:21
<p>I am running a classified website where people can post their items and others can contact them via Whatsapp for more details. My website was attacked today and I'm seeing a huge amount of link clicks on the Whatsapp button in Google Analytics, it can't be artificial. I think it's some bot grabbing the phone number from the button link. </p>
<pre class="brush:php;toolbar:false;">$phone= $row_posts['phone'];
// for example $phone=" 212612345678"
<div class="contact">
<a class="btn btn-success" target="_blank" rel="noopener noreferrer" href="https://wa.me/212$phone?text=TextMessage">
More Details
</a>
</div></pre>
<p>I use it for Whatsapp buttons. As you can see, the phone number is easily displayed in the link href. I saw some websites like "www.mubawab.ma" using Whatsapp button but you can't see that number first time in inspecting the code. I want to implement similar functionality so that I can protect my user phone numbers from bot web scraping. </p>
There are many ways to do this. Using a reCaptcha style interceptor is probably the best option. However, here is a simple implementation that will stop most non-targeted bots.
In PHP I encoded the phone number via base64_encode and then decoded it in JavaScript using atob.
The code generates this content in HTML: