fromage
07-29-2005, 01:32 AM
Ugh I think I worded that right.
I'm coding my own little message board type php script, and I need to know the functions, that basically counts the time elapsed since last execution of a script from a specific IP address. After a preset time has elapsed, the script can then be run again from that IP address.
Basically, it's a flood-limiting thingy that you see on all forums, or something...
I've already made a field for IP address, so it gets recorded in the mySQL database, along with everything else, I just need to know how to check the mySQL db for A) if that ip exists and B) if its been more than say 30 seconds since last post, allow execution of the posting script once again.
I think my logic is right, I just have no clue how to go about this, or what functions to use. I'm still completely new to php.
maxymizer
07-29-2005, 04:17 AM
The easiest way is by using sessions. When, let's say, script search.php is accessed from 192.168.0.1 you set a session with acccessing time to the script for the appropriate IP address (actually, you won't need to record IP address).
I will presume that session is allready started so we can avoid the usual fuss with sessions and you can consider following example for usage:
if(isset($_SESSION['search_accessed']))
{
if($_SESSION['search_accessed'] < time())
{
$allow_access = true;
}
else
{
$allow_access = false;
}
}
else
{
$allow_access = true;
}
if($allow_access )
{
/* execute important code that this script contains */
/* after executing the code, set a session named search_accessed to time() + number of seconds you want your timeout to last */
$_SESSION['search_accessed'] = time() + 30 // so the timeout is now 30 seconds
}
else
{
/* redirect a user or inform him that he cannot use this script until certain number of second passes
}
I've put comments into the code, but i will explain again.
First, script looks for a session named search_accessed (for this example, I used a search script).
If such session exists, script checks if the give time interval has passed (in your case - 30 seconds).
If the interval passed, user can use the script again ($allow_access is true) else he cannot ($allow_access is false).
If session isn't set, that means the user didn't access the script at all and this is the first time he is accessing the script so we will allow him to do so ($allow_ccess is true).
After setting variable $allow_access to true or false, we check it and decide what to do - if it's true, search code is executed and a time interval is set, but if it's false user is notified that his 30 seconds aren't up yet.
I hope this example helps. Since you said you're new to PHP, you might improve your knowledge on PHP (I don't know your knowledge level on sessions, but many new users mistake sessions and decide not to use them).
fromage
07-29-2005, 04:32 AM
Originally posted by maxymizer
but many new users mistake sessions and decide not to use them).
Heh, I'm one of those. Thanks alot though, I'm going to read up on sessions, and get those out of the way! Then I'll try your approach.
maxymizer
07-29-2005, 04:39 AM
Your approach would be creating something simillar to a session but that would be just reinventing the wheel :)
Lanuage has such constructs allready, they are fairly easy to grasp (just find the appropriate tutorial) and once you get to know what sessions do and how they do it - you will see that they are quite useful.