View Full Version : "Auto-Expire" DB Entries?

03-11-2007, 05:46 AM

I am currently in the process of redoing a DJ Panel script that I created and am looking into sending out "IP Alerts" to users... what it does is stores the ip and message to the user in the db until the users ip matches the one in the db, then it gives them a javascript popup with the message and then deletes the db entry....

What I was wondering is if I could somehow make it so the entry in the db would show a popup to every user that visits the site in the next 5 minutes and then delete itself after say 5 minutes?

My current syntax is below:

$ipaddr = $_SERVER['REMOTE_ADDR'];
$result = mysql_query("SELECT id, toip, message, fromdj FROM rp_alerts ORDER BY id ASC") or die(mysql_error());
$row = mysql_fetch_array($result);
if($ipaddr == $row['toip']) {
echo '<script language="JavaScript" type="text/javascript">
alert("DJ '.$row['fromdj'].' Says: '.$row['message'].'")</script>';
$result = mysql_query("DELETE FROM rp_alerts WHERE id = '$row[id]'") or die(mysql_error());

Any help is appreciated!

03-11-2007, 06:27 AM
I'd go about it something like this...

Add a field to the DB 'expiretime.' This field initialized to NULL.

Pseudocode Algorithm:
clear alertbuffer
if expiretime is set {
if current time > expiretime {
delete row;
} else {
add message to alertbuffer;
} else {
if ipaddr=toip {
add message to alertbuffer;
set expiretime=current time + 5 minutes;
if alertbuffer { display alertbuffer }
When the appropriate visitor comes to the site, the alert will be displayed (along with all other unexpired messages) and expiretime will be set to 5 minutes (or whatever) later.

Once the expire time has been set, the message will be displayed (along with all other unexpired messages) to all users until the current time is later than the expire time. The first visitor after the message has expired will trigger the code that deletes the message.

Now you just need to turn the pseudocode to actual code, which shouldn't be too difficult.

Hope this helps.