Posted in php functions
489
12:24 pm, November 22, 2021
 

Spam IP Database and delete button

<h3>The Background and Reason for the SPAM IP Database and Delete</h3>
<p>I actually wrote up half of this article and was still testing the function and lost the whole post, which will teach me for adding it straight into my cms... Anyway I'll write it up again.</p>
<p>So i still have been getting quite a bit of bot comment spam, even after a few other attempts to block this with google recapture, and other techniques, but they still manage to get through which is annoying.</p>
<p>So i had been taking the ip addresses from these spam comments and adding it to an array which i had to update each time i had a new one, so i had to go into the main file and add the new ip, then i had to go into the database and run a delete command for all of the comments matching an IP address.</p>
<p>I was doing this because i thought im not spending time writing a procedure to handle spam. well im sure i have wasted many hours on this now, so im going to make it easy for my self and write a function that you can list the comment and click a button where it will then add the IP address to a block list and also delete all the related spam from that IP.</p>
<p>Sounds easy now that im typing about it, and it is actually i have done the function and page in about an hour ish once i got into the groove. I love that coding groove, but its hard to get into sometimes.</p>
<p>Anyway this is what i did. Some of the code links to other bits in the CMS so this example code probably wont work for you out of the box.</p>
<h3>New Spam Block Class</h3>
<p>Create a new class extend for the block list to live in.</p>
<h4>PHP</h4>
<pre><code class="php hljs">class ipblock extends core {

&nbsp; public $classextend = true; // this must be set to true! :)

&nbsp; public $addtomenu = false;
&nbsp; public $add
sectionbutton = true; // add this class to the section menu
&nbsp; public $add
toadminmenu = false; // add this class to the main admin menu

&nbsp; public $nicename = "IPBlock";
&nbsp; public $nice
description = "Blocked IP Addresses - If you made it here you should not see this...";
&nbsp; &nbsp; public $dbtablename = "ipblock";
&nbsp; public $imagesenabled = false;

&nbsp; public $loadarray = [
&nbsp; &nbsp; "id",
&nbsp; &nbsp; "uid",
&nbsp; &nbsp; "insdate",
&nbsp; &nbsp; "title",
&nbsp; &nbsp; "additional",
&nbsp; &nbsp; "category",
&nbsp; &nbsp; "ip",
&nbsp; ];

}</code></pre>
<p>Load the ips and block them once they are added to this new class and database.</p>
<h4>PHP</h4>
<pre><code class="php hljs">//Load the IP Blocked List
$ipblockarray = $ipblock-&gt;listdistinct(
&nbsp; "ip",
&nbsp; 0,
&nbsp; 5000,
&nbsp; "ip",
&nbsp; "DESC"
);
foreach ($ipblockarray as $ipblockval) {
&nbsp; if($
SERVER['REMOTEADDR'] == $ipblockval) {
&nbsp; &nbsp; echo "no spam thx";
&nbsp; &nbsp; return false;
&nbsp; }
}</code></pre>
<p>Change the template for the comments admin list and add a delete button</p>
<h4>HTML</h4>
<pre><code class="html hljs xml">&lt;a href='/block-delete/comments/[@user
ip]/' class='btn btn-danger'&gt;Block + Delete [@userip]&lt;/a&gt;</code></pre>
<p>Create a new block-delete page to handle the blocks and delete functions. this will also check that the ip should be blocked and deleted before proceeding and then allow the user to go back to the comments list if there is any more spam to be blocked or deleted.&nbsp;</p>
<h4>PHP</h4>
<pre><code class="php hljs">$alert
template = new template("alert.html");<br />$alerttemplate-&gt;set("alerttype", "danger");<br />$alerttemplate-&gt;set("content", "This will delete all $p2 from IP $p3 and block it. Are you sure?");<br />$alerthtml = $alerttemplate-&gt;output();<br />$pagecontent .= $alerthtml;<br />$pagecontent .= "&lt;p&gt;&lt;a href='/$p1/$p2/$p3/confirm/' class='btn btn-danger'&gt;Yes Delete and Block&lt;/a&gt;&lt;/p&gt;";<br />if($p4 == "confirm") {<br />&nbsp; $ipblock-&gt;title = $p3;<br />&nbsp; $ipblock-&gt;ip = $p3;<br />&nbsp; $ipblock-&gt;add();<br />&nbsp; $p2safe = $comments-&gt;db-&gt;escapeString($p2);<br />&nbsp; $p3safe = $comments-&gt;db-&gt;escapeString($p3);<br />&nbsp; $sql = "delete from $p2safe where userip = '$p3safe' ";<br />&nbsp; $result = $comments-&gt;db-&gt;query($sql);<br />&nbsp; $alerttemplate = new template("alert.html");<br />&nbsp; $alerttemplate-&gt;set("alerttype", "success");<br />&nbsp; $alerttemplate-&gt;set("content", "Blocked and Deleted. &lt;a href='/list/$p2/' class='btn btn-primary'&gt;Back to $p2 List&lt;/a&gt;");<br />&nbsp; $alerthtml = $alerttemplate-&gt;output();<br />&nbsp; $pagecontent .= $alert_html;<br />}</code></pre>
<p>And done. This was actually easier than i thought for some reason i had been putting it off.&nbsp;</p>
<p>Here is a graphical flow of the process.</p>
<p><img src="https://i.imgur.com/nodvY1w.png" /></p>
<p>Click Block + Delete</p>
<p><img src="https://i.imgur.com/aQ1PHo3.png" /></p>
<p>Click on Confirm or the Yes Delete and Block and onto the next step, running the delete function and blocking the IP.</p>
<p><img src="https://i.imgur.com/6rULmsm.png" /></p>
<p>Ok all should be deleted and blocked, check the IP is in the ipblock db.</p>
<p><img src="https://i.imgur.com/eW9b8y2.png" /></p>

View Statistics
This Week
387
This Month
2233
This Year
595

No Items Found.

Add Comment
Type in a Nick Name here
 
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
Ever tried. Ever failed. No matter. Try again. Fail again. Fail better. You won't believe what you can accomplish by attempting the impossible with the courage to repeatedly fail better.
Unknown
Latest News
## 🚀 AI Giants Hit Bullseye: Anthropic & OpenAI Achieve Product-Market Fit Anthropic and OpenAI have reached a significant milestone, finding product-market fit with their AI technologies, which means their products effectively meet the needs of their customers, driving growth and adoption. This achievement showcases the practical value of their innovations, enabling businesses and individuals to leverage AI for enhanced productivity and efficiency. With this alignment of product and market needs, these companies are poised to transform industries and shape the future of technology.