Jump to content


Photo

Time!


  • Please log in to reply
10 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 28 September 2006 - 01:10 PM

Hey,
I've got a small problem with my ban script. The problem is
When i retrive the expiry date from the table i try to check
the time now to the time in the database, using the IF i tried
to check if today is older or equal to that time, but I think
I've used an incorrect method. Any know what i'm doing wrong?

my code...
include "../db/db.php";
	$result = mysql_query("SELECT pri_active,adm_ban FROM accounts WHERE usr_user='".strtolower($_POST['user'])."'");
	while( $dat = @mysql_fetch_array($result, MYSQL_NUM))
	{
	$ban = $dat[1];
	$active = $dat[0];
		if($ban=="y")
		{
			$SQL = mysql_query("SELECT * FROM ban WHERE user='".strtolower( $_POST['user'] )."'");
			while( $data = mysql_fetch_row( $SQL ))
		{
			$today = date("j, m, Y - H:i.s");
			if( $today >= $data[4] )		
		{
			mysql_query("UPDATE accounts SET adm_ban='n' WHERE usr_user='".strtolower( $_POST['user'] )."'");
			mysql_query("DLETE FROM ban WHERE user='".strtolower( $_POST['user'] )."'");
			}
			else
			{
			die('still banned');
			}
		}
	}
}


#2 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 28 September 2006 - 03:41 PM

Hi

i think the problem is that you are comparing the dates in string format. rather compare using timestamps as they are integers

you can try something like:


if($ban=="y")
{
  $SQL = mysql_query("SELECT * FROM ban WHERE user='".strtolower( $_POST['user'] )."'");
  while( $data = mysql_fetch_row( $SQL ))
{

    // get current timestamp
    $today = time();

    // create an array from your date format
    $datearr = explode(",",str_replace(array(" ","-",":","."), array("",",",",",","), $data[4]));

    // create a timestamp from the array
    $bandate = mktime($datearr[3],$datearr[4],$datearr[5],$datearr[1],$datearr[0],$datearr[2]);

    //compare the timestamps
    if( $today >= $bandate )

{



i believe in php5 there is a function to convert the date better from string to timestamp, but imstillon php4 :(

hope this helps


Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 September 2006 - 04:53 PM

use strtotime thedarkwinter its compatible for PHP3, 4 and 5

#4 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 28 September 2006 - 05:33 PM

How would i use that?
$Now = date("j, m, Y - H:i.s", strtotime("now"));
if( $Now >= "$data[expire]")
{
//unban code
}
else
{
     if( $data[expire] == "never" )
     {
     //perm banned
     }
     else
     {
     //still banned
     }
}


#5 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 28 September 2006 - 06:17 PM

Hi

i think the problem is that you are comparing the dates in string format. rather compare using timestamps as they are integers

you can try something like:


if($ban=="y")
{
  $SQL = mysql_query("SELECT * FROM ban WHERE user='".strtolower( $_POST['user'] )."'");
  while( $data = mysql_fetch_row( $SQL ))
{

    // get current timestamp
    $today = time();

    // create an array from your date format
    $datearr = explode(",",str_replace(array(" ","-",":","."), array("",",",",",","), $data[4]));

    // create a timestamp from the array
    $bandate = mktime($datearr[3],$datearr[4],$datearr[5],$datearr[1],$datearr[0],$datearr[2]);

    //compare the timestamps
    if( $today >= $bandate )

{



i believe in php5 there is a function to convert the date better from string to timestamp, but imstillon php4 :(

hope this helps



Now i get this error:
Warning: mktime() expects parameter 4 to be long, string given in /home/eumod/public_html/en/global/login.php on line 128

#6 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 September 2006 - 06:31 PM

Do you use the mysql NOW() when adding banned time into table ban ?

If so you can use interval like this when checking (change fieldname "bantime" to your actual fieldname containing time)
This will reset bans older than 1 day. You can also change it to formats like example interval 3600 second (see mysql manual)

<?php

$user = mysql_real_escape_string(strtolower($_POST['user']));

$sql = mysql_query("SELECT * FROM ban WHERE user='$user' AND bantime < date_sub(now(), interval 1 day)");
if(mysql_num_rows($sql) > "0")
{
$upt = mysql_query("UPDATE accounts SET adm_ban='n' WHERE usr_user='$user'");
$del = mysql_query("DELETE FROM ban WHERE user='$user'");
}


?>


#7 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 28 September 2006 - 06:34 PM

i'm lost... Lol

#8 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 September 2006 - 06:51 PM

I was asking in what time format you stamp the time when banning.

I'm not saying that you should change your current ban script, but let me explain my solution:

<?php

$user = mysql_real_escape_string(strtolower($_POST['user']));

// ban user
$ban_stamp = mysql_query("INSERT INTO ban (bantime,user) values (NOW(),'$user')");
$ban_stamp_accounts = mysql_query("UPDATE accounts SET adm_ban='y' WHERE usr_user='$user'");

// tikk takk - clock is ticking and time is passing by

// lets see if $ban_stamp is made past 1 day ago
$sql = mysql_query("SELECT * FROM ban WHERE user='$user' AND bantime < date_sub(now(), interval 1 day)");
if(mysql_num_rows($sql) > "0")
{
$upt = mysql_query("UPDATE accounts SET adm_ban='n' WHERE usr_user='$user'");
$del = mysql_query("DELETE FROM ban WHERE user='$user'");

echo "$user is now free to go!";
}
else
{
echo "$user is still banned..";
}


?>



#9 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 28 September 2006 - 06:57 PM

Oh i see, well my clock format is:
"j, m, Y - H:i.s"

But I don't want the ban to be 1 day, I wanted my mods
to choose from:
X minutes, X Hours, X Days, -> X being any value, could
be 1 minute or2348 minutes... etc.

#10 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 29 September 2006 - 09:48 AM

Hi

firstly, wildteen88, thanks ill check it out.

xyn: when you say your clock format is "j, m, Y - H:i.s", im assuming you mean your mySQL date format? If not then you need to rearrange the mktime<http://uk2.php.net/m...tion.mktime.php> so that the correct values are put in. You will also need to check that the str_replaces etc are removing the correct spaces hyphens etc.

On a personal note. I would advise using a more consistant date format in mySQL: i.e use d instead of j so that the length of the string is always the same, and use H:i:s instead of H:i.s - It may not help right now - but one day...
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#11 Fehnris

Fehnris
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 29 September 2006 - 06:13 PM

Would save yourself some work when testing for a banned date by using a UNIX timestamp to save the ban expiry date in the database.

X minutes, X Hours, X Days, -> X being any value, could
be 1 minute or2348 minutes... etc.


Im pressuming when the Mod enters X minutes, X hours, X days these values get passed to a script that creates a ban date for an account in the format "j, m, Y - H:i.s" and enters it into the database.  When creating the ban expiry date use strtotime() to create a timestamp instead:-

$BanTimeStamp = strtotime("+"."<X Days>"." days "."<X Hours>"." hours "."<X Minutes>"." minutes "."<X Seconds>"." seconds".

Using Unix timestamps in your database you could simplify your ban checking script to:-


if($ban=="y")
{
  $SQL = mysql_query("SELECT * FROM ban WHERE user='".strtolower( $_POST['user'] )."'");
  while( $data = mysql_fetch_row( $SQL ))
 {

    // get current timestamp
    $today = time();

    //No more need to convert from your string date format to Unix timestamp for comparing as
    //Data[4] is already in UNIX timestamp format.

    //compare the timestamps
    if( $today >= $Data[4] ) 

 {








0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users