Cron Job Automate Social Network Web Site Maintenance Tutorial

Published :
Author :
Adam Khoury
Learn to automate PHP MySQL web site maintenance tasks with Cron Jobs. You can set them to execute at any interval you like. In this example we create a PHP cron job that will delete all non-activate users after X days. If you do not clear the database of these users periodically your system will become clogged with non-activated users. once_daily.php <?php require_once("../php_includes/db_conx.php"); // This block deletes all accounts that do not activate after 3 days $sql = "SELECT id, username FROM users WHERE signup<=CURRENT_DATE - INTERVAL 3 DAY AND activated='0'"; $query = mysqli_query($db_conx, $sql); $numrows = mysqli_num_rows($query); if($numrows > 0){ while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) { $id = $row['id']; $username = $row['username']; $userFolder = "../user/$username"; if(is_dir($userFolder)) { rmdir($userFolder); } mysqli_query($db_conx, "DELETE FROM users WHERE id='$id' AND username='$username' AND activated='0' LIMIT 1"); mysqli_query($db_conx, "DELETE FROM useroptions WHERE username='$username' LIMIT 1"); } } ?>