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

Spam IP Database and delete button

The Background and Reason for the SPAM IP Database and Delete

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.

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.

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.

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.

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.

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.

New Spam Block Class

Create a new class extend for the block list to live in.

PHP

class ipblock extends core {

  public $class_extend = true; // this must be set to true! :)

  public $add_to_menu = false;
  public $add_section_button = true; // add this class to the section menu
  public $add_to_admin_menu = false; // add this class to the main admin menu

  public $nice_name = "IPBlock";
  public $nice_description = "Blocked IP Addresses - If you made it here you should not see this...";
    public $db_table_name = "ipblock";
  public $images_enabled = false;

  public $load_array = [
    "id",
    "uid",
    "insdate",
    "title",
    "additional",
    "category",
    "ip",
  ];

}

Load the ips and block them once they are added to this new class and database.

PHP

//Load the IP Blocked List
$ipblock_array = $ipblock->list_distinct(
  "ip",
  0,
  5000,
  "ip",
  "DESC"
);
foreach ($ipblock_array as $ip_block_val) {
  if($_SERVER['REMOTE_ADDR'] == $ip_block_val) {
    echo "no spam thx";
    return false;
  }
}

Change the template for the comments admin list and add a delete button

HTML

<a href='/block-delete/comments/[@user_ip]/' class='btn btn-danger'>Block + Delete [@user_ip]</a>

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. 

PHP

$alert_template = new template("alert.html");
$alert_template->set("alert_type", "danger");
$alert_template->set("content", "This will delete all $p2 from IP $p3 and block it. Are you sure?");
$alert_html = $alert_template->output();
$page_content .= $alert_html;
$page_content .= "<p><a href='/$p1/$p2/$p3/confirm/' class='btn btn-danger'>Yes Delete and Block</a></p>";
if($p4 == "confirm") {
  $ipblock->title = $p3;
  $ipblock->ip = $p3;
  $ipblock->add();
  $p2_safe = $comments->db->escapeString($p2);
  $p3_safe = $comments->db->escapeString($p3);
  $sql = "delete from $p2_safe where user_ip = '$p3_safe' ";
  $result = $comments->db->query($sql);
  $alert_template = new template("alert.html");
  $alert_template->set("alert_type", "success");
  $alert_template->set("content", "Blocked and Deleted. <a href='/list/$p2/' class='btn btn-primary'>Back to $p2 List</a>");
  $alert_html = $alert_template->output();
  $page_content .= $alert_html;
}

And done. This was actually easier than i thought for some reason i had been putting it off. 

Here is a graphical flow of the process.

Click Block + Delete

Click on Confirm or the Yes Delete and Block and onto the next step, running the delete function and blocking the IP.

Ok all should be deleted and blocked, check the IP is in the ipblock db.

View Statistics
This Week
406
This Month
2360
This Year
4348

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...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

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

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

scroll-margin-block-start

The scroll-margin-block-start property defines the margin of the scroll snap area at the start of the block dimension that is used for snapping this box to the snapport. The scroll snap area is determined by taking the transformed border box, finding its rectangular bounding box (axis-aligned in the scroll container's coordinate space), then adding the specified outsets.
scroll-margin-block-start css reference