Solved

Comparing mysql now() timestamp with php

Posted on 2003-12-03
13
42,347 Views
Last Modified: 2013-12-12
hi,

i'm trying to check if the user has submitted less than 10mins ago in the database but the code i got, dont seem to work. (the code is grabbed from the php file so start/end stuff might not be there.)

$squ = "select subip, dte from dbwr WHERE subip='$usip' ORDER BY dte ASC LIMIT 1";
$res = mysql_query($squ) or die(mysql_error());
while ($newarray = mysql_fetch_array($res)) {
$mytime = $newarray['dte'];

//this is where it checks if the time in the db is less than 10mins
if ($mytime >= time()-600) $abuse = 1;
else
$abuse = 0;

the query i submit is to grab the ip and timestamp from the db and return the newest one. (dte = timestamp, subip = userip)

hope you can help me asap.

cheers
0
Comment
Question by:classic_gaming
13 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9870433
hi,

your code is missing a }, or you could see it as an extra while clause (i've taken it out)

<?php
$squ = "select subip, dte from dbwr WHERE subip='$usip' ORDER BY dte ASC LIMIT 1";
$res = mysql_query($squ) or die(mysql_error());
$newarray = mysql_fetch_assoc($res);
$mytime = $newarray['dte'];
//this is where it checks if the time in the db is less than 10mins
if ($mytime >= (time()-600)) $abuse = 1;
else
$abuse = 0;
?>
0
 

Author Comment

by:classic_gaming
ID: 9870576
hi,

but does that time compare if statement work?

that is the prime thing that needs sorting.

for example:

dte = 20031203133421
and if time() = 20031203134321

that should equal 9mins.

i dont have a clue if the if statement works or not, that is why im asking :(

cheers
0
 
LVL 14

Expert Comment

by:ThG
ID: 9870964
Yes it should work.. isnt it?
Please provide some test cases.. like

print "$mytime -- " . (time() - 600) . " -- $abuse";

i hope you are inserting as php's time() in the database, because if you are doing it with MySQL function UNIX_TIMESTAMP() the clock difference might be fatal.
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9871160
that time stamp that you've provided for dte isn't a php timestamp, so you need to decide whether you want to use a php timestamp or mysql one.. personally i use php

loz
0
 

Author Comment

by:classic_gaming
ID: 9871302
<?php
//this is what i do to submit the data
$newt = time();
mysql_query("INSERT INTO dbwr VALUES ('$usip'," . $newt . ",'0')");

//this is what i do to get the time from the db
$squ = "select subip, dte from dbwr WHERE subip='$usip' ORDER BY dte ASC LIMIT 1";
$res = mysql_query($squ) or die(mysql_error());
$newarray = mysql_fetch_assoc($res);
$mytime = $newarray['dte'];
//this is where it checks if the time in the db is less than 10mins
if ($mytime >= (time()-600)) $abuse = 1;
else
$abuse = 0;
?>
0
 

Author Comment

by:classic_gaming
ID: 9871318
is there anything wrong im doing?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

by:lozloz
ID: 9871438
i assume $newt is being inserted for the column dte - this is a php timestamp and so correct, the code should work. if it isn't, do the debugging ThG suggested

loz
0
 

Author Comment

by:classic_gaming
ID: 9871537
dte = 20001210021847

print "$mytime -- " . (time() - 600) . " -- $abuse"; = 20001210021847 -- 1070501259 -- 1

does that help enlighten anything?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9871543
i think you've got the wrong type of mysql column, what is it? it should be int(11)

loz
0
 

Author Comment

by:classic_gaming
ID: 9871671
ah must be why, it was timestamp(15), changed it to int(11)

i've also changed the select query to DESC

dte = 1070503722

new submission:
Abuse!
1070503722 -- 1070503221 -- 1
-699

echo "Abuse!<br>";
$nt = $mytime - time() - 600;
print "$mytime -- " . (time() - 600) . " -- $abuse<br>$nt";

why is it going past 600 and denying me to submit new data?
0
 
LVL 13

Accepted Solution

by:
lozloz earned 200 total points
ID: 9871722
because you're doing it wrong - you're saying minus 600 from the old timestamp, then minus the time from that. so if $mytime is 5 minutes ago, you're making it 15 minutes ago compared to the timestamp, and then turning it in to a negative number in relation to 0

you want $nt = $mytime - (time() - 600);

loz
0
 

Author Comment

by:classic_gaming
ID: 9873729
ah it works now, thanks for your help
0
 
LVL 6

Expert Comment

by:neorush
ID: 13640527
A faster a possibly simpler solution depending on what else you have to do....
<?PHP

$10mins_ago = time() - 600;

$squ = "select `subip`,`dte` from `dbwr` WHERE `subip`='$usip' AND `dte`>=$10mins_ago";
$res = mysql_query($squ) or die(mysql_error());
$num_returns = mysql_num_rows($res);

if($num_returns==0){
      //abuse
}
else{
      //no abuse
}

?>
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now