PHPBB Inactive Member Removal Cron Job

A commenter to a previous article asked exactly how I delete inactive members from a PHPBB forum that I run. So I’ll try to explain. This solution runs on Linux/Unix systems…I’m sure it could be done for Windows, but I’ll leave the particulars to you.

It’s really two separate steps. First, you need a script which will handle the deletion of inactive members. I called mine cron.php. It deletes all inactive PHPBB users who don’t activate within 48 hours. It looks like this:

!/usr/bin/php -q

<?php
  // cron job to delete inactive users older than 48 hours
  $db=mysqlconnect(‘server’,'user’,'password’);
  mysql
selectdb('yourphpbbdatabasehere’,$db);
  $strSQL=“DELETE phpbbusers u, phpbbusergroup ug, ” .
    “phpbb
groups AS g FROM phpbbusers u, ” .
    “phpbb
usergroup ug, phpbbgroups g WHERE ” .
    “u.useractive=0 AND u.userid>0 AND ” .
    “u.userid=ug.userid AND ug.groupid=g.groupid ” .
    “AND g.groupsingleuser=1 AND ” .
    “FROMUNIXTIME(u.userregdate)<” .
    “DATESUB(NOW(),INTERVAL 2 DAY);”;
  mysql
query($strSQL,$db) or die(mysqlerror());
  mysql
close($db);
?>

You’ll need to make sure the /usr/bin/php points to the location of PHP on your system, and replace the MySQL server name, user, and password with yours.

Now that you have a script, you need to tell the system to run it daily. You can do this with a cron job. If you have command line access to your website, you might be able to do this with “crontab -e”. But my webhost has an administrative panel that lets you set up cron jobs on the web. If you can’t set up a cron job, you could put the script into a web-accessible folder and periodically call its URL, either manually or through an automated process on your local PC.

This idea works great if the majority of your spam registrations don’t activate their account. Usually they just want their spam links in your member list. But I’m finding that more and more spammers are activating and posting, so it remains that we want to stop spammers from registering in the first place. I’m experimenting with another method, which I’ll post about when I see some results.

Update 2007-11-28: I replaced my original SQL statement with the SQL in comment #1 below, which I finally tested and it seems to work well.