1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How can i create a temporary ban with MySqli?

Discussion in 'Development' started by LewBr, Dec 23, 2017.

  1. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Hello, i have created a ban system with database server and i don't have idea how to do a sys that when player is banned then remove that ban for example on the next day or 01/01/2018 or after hours..

    How can i do that? Tasks? Calendar function(has on pmmp?)?
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    When banning player just store time() + 1 day and then check if time() if bigger than time that's saved when you was banning player
     
    xXNiceAssassinlo YT likes this.
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    strtotime()
    PHP:
    $tomorrow strtotime("+1 day");
    $nextMin strtotime("+1 minute");
    $tomorrowPlus30Mins strtotime("+1 day 30 minute");
    You can use a plugin task that runs throughout OR check out mysql events, its an interesting topic. I'd go with mysql events if I were you.
     
    [deleted] and OnTheVerge like this.
  4. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    But have any example? Like if i use strtotime on a database then the time updates automatically? Like.. They goes to null alone?
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That's where the tasks come in. You can't automate the process so you use tasks or mysql events.

    strtotime() returns Unix timestamp
    strtotime("+5 minutes") is the same as time() + 300[citation needed]

    I don't think you got me on that, strtotime() doesn't exist in MySQL but you can have a MySQL event run every 1 minute or so which executes
    Code:
    DELETE FROM table WHERE time < UNIX_TIMESTAMP();
    
     
    Last edited: Dec 24, 2017
    LewBr likes this.
  6. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    He don't want to use tasks
    PHP:
        /** @var \mysqli $mysql */
        
    public $mysql;

        public static function 
    addBan($player,$days){
            
    $time time() + $days 24 3600;
            
    $this->mysql->query
            
    (
                
    "UPDATE players SET bantime = '".$time."' WHERE name = '".$this->escape_string(trim(strtolower($player)))."'"
            
    );
        }
    Then check if is banned or not when joining
    PHP:
    if(time() >= $bantime){
        
    //Unban
    }
    You can use strtime() as Muqsit said, this is just only example
     
  7. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
  8. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Thunder33345 likes this.
  9. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Why delete the ban entirely? Just check the timestamp to make sure that they’re still banned before kicking them.
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    He never said he doesn't need any tasks involved. Plus you'll be stacking up on a crap ton of expired entries in the table that way.
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That's just an example of using unix timestamp in mysql, read the OP once more:
     
  12. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    When he use task, then ban time is not changed when server is disabled
    So it's not good idea
     
  13. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    That's right! I prefer without using tasks.
    So if i use that, the 'bantime' of that player goes to null automatically?
     
  14. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    No it doesn't. Everything will remain as it is, nothing gets changed. You'll need to compare the time field in the table with the current Unix timestamp.
    If you use tasks, you'll only need to check if the entry exists.
    Code:
    SELECT EXISTS(SELECT 1 FROM table WHERE player=?)
    
    But it's up to you, bans are manually handled so having 10k+ entries will take years and yet it's an insignificant number for mysql db. Use tasks only if you want to clean out the expired entries automatically.

    Assuming you have a well-structured table, you will need to call ...ON DUPLICATE KEY UPDATE... because you aren't sweeping off the expired entries. Or alternatively a delete query before insert.
     
    Last edited: Dec 25, 2017
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Note that time string parsing is an expensive operation, and this is totally avoidable. If you write it clearly (e.g. write 60 * 60 * 24 * 7 rather than 604800), everyone (who knows that PHP's time unit is in seconds) knows you're trying to write "one week", especially when you put it after time().
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.