Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Comparing mysql now() timestamp with php

Posted on 2003-12-03
13
Medium Priority
?
42,368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…

721 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