Php Wizards, I Summon Thee Forth!

ScinetScinet Join Date: 2003-01-19 Member: 12489Members, Constellation
<div class="IPBDescription">I need to jump back in time...</div> ...for about 5 minutes.

Here's the story:
I've been putting HTML and PHP together for a month now for a complete revamp of my website. I've got just about everything else done, but I need to create a feedback form too. I want the form to have the following features:

1) Mail the feedback to one of my email addresses
2) Log all feedback in a table in my site's database
3) Prevent spamming by denying feedback from and IP for a five minute timespan after the last feedback from the same IP.

Number one and two I've got firmly under control, but number three's a problem since I've never handled time functions with PHP before.

Now, assuming I'd like to get number three working, I know the function has to first access the database and search the feedback table for entries containing the same IP. Upon finding them, it must pick up the last time an entry was submitted from that address and compare it with the current time.

Assuming that current time is 19:00:00, and last feedback was entered at 18:59:00, I think that the best way to go would be to subtract 5 minutes from the current time and compare it with the entry's time, like this:

<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->$curtime=$curtime - 5 minutes
$lastEntryTime = (an SQL function to get the time of the last entry from the same IP);
if ($curTime<=$lastEntryTime){
   echo("Sorry. No feedback was logged, spammer");
} else {
   log the feedback and so on...
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

Now, if I have a variable that is time, how do I subtract or add seconds, minutes or hours to it? And also, are time variables comparable like in the code above (just like any other numeric variable), or is there some special trick to it?

Comments

  • SkySky Join Date: 2004-04-23 Member: 28131Members
    Bumped.

    I have no idea what to do, but I figure no one's gonna help if it's off the first page.
  • SwiftspearSwiftspear Custim tital Join Date: 2003-10-29 Member: 22097Members
    Wow, I'm gonna run a bot that spams you input every 5min 1second...

    J/K, I have no clue about your problem, sorry.
  • Dorian_GrayDorian_Gray Join Date: 2004-02-15 Member: 26581Members, Constellation
    edited June 2004
    Linky: <a href='http://www.php.net/manual/en/function.time.php' target='_blank'>PHP Man page on Time()</a>

    Assuming $lasttime is the last time that feedback was recorded (in the format returned by time()<!--emo&;)--><img src='http://www.unknownworlds.com/forums/html//emoticons/wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /><!--endemo-->,

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->function isSpam($lasttime) {
    if ((time() - $lasttime) < 300) {
    return true;
    }
    else {
    return false;
    }

    function recordFeedback(blah) {
    if (!isSpam(blah)) {
    blah
    }
    else {
    echo "No feedback recorded, spammer."
    }
    }
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    Theoretically that should work, but I haven't had much sleep lately (LAN party) so I might have missed something. Time() simply gets the number of seconds since a certain date (its in the link). So if time() - $lasttime > 300 (5 minutes), then its valid.
  • ScinetScinet Join Date: 2003-01-19 Member: 12489Members, Constellation
    Thanks, Dorian Gray.

    The feedback form is now timestamped, controlled, and also logs IPs.
  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    Is <a href='http://www.radiumclub.net/' target='_blank'>http://www.radiumclub.net/</a> the site you're working on Scinet?
    I'll have a browse through that later. (In work at mo, working... haha)
  • ScinetScinet Join Date: 2003-01-19 Member: 12489Members, Constellation
    <!--QuoteBegin-DY357LX+Jul 1 2004, 04:58 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (DY357LX @ Jul 1 2004, 04:58 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Is <a href='http://www.radiumclub.net/' target='_blank'>http://www.radiumclub.net/</a> the site you're working on Scinet?
    I'll have a browse through that later. (In work at mo, working... haha) <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Yes it is, but the new site won't be up 'till friday or saturday, and it will be (apart from a short english summary) completely in finnish.
  • Hand_Me_The_Gun_And_Ask_Me_AgainHand_Me_The_Gun_And_Ask_Me_Again Join Date: 2002-02-07 Member: 178Members
    edited July 2004
    Use time functions in the database?

    Summat like:

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    $query = "SELECT id FROM email_logs WHERE client_ip = '".mysql_escape_string( $ip_address )."' AND date_sent > DATE_SUB( NOW(), INTERVAL 5 MINUTES )";

    $result = mysql_query( $query ) or die( mysql_error() );

    if ( mysql_num_rows( $result ) )
       die( "OMG TEH SPMAMM0R!!1" );
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    Edit: The query can easily be expanded to prevent someone sending more than, say, ten emails a day from a particular IP address. Do be aware that you may get many people behind a proxy server, all with the same effective IP. It's possible to guess what the real IP is (there's usually something in the HTTP request), but it's very easily spoofed, or could be wrong or useless. Mine's probably reporting my machine as 10.0.0.7...
Sign In or Register to comment.