Jump to content

Archived

This topic is now archived and is closed to further replies.

xyn

Time!

Recommended Posts

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...
[code=php:0]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');
}
}
}
}
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
How would i use that?
[code=php:0]$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
    }
}[/code]

Share this post


Link to post
Share on other sites
[quote author=thedarkwinter link=topic=109831.msg443146#msg443146 date=1159458115]
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


[/quote]

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

Share this post


Link to post
Share on other sites
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)
[code]

<?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'");
}


?>
[/code]

Share this post


Link to post
Share on other sites
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:
[code]

<?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..";
}


?>

[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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<[url=http://uk2.php.net/manual/en/function.mktime.php]http://uk2.php.net/manual/en/function.mktime.php[/url]> 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...

Share this post


Link to post
Share on other sites
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.

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

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

[code]

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

{



[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.