Solved

Comparing mysql now() timestamp with php

Posted on 2003-12-03
13
42,361 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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