-2

I made a ban script where it updates the members list and put SET banned=Yes WHERE username=$_POST[username]

But now I would like to make timed bans, like if a user get banned for a day, he will be un-banned after his ban time.

Does anyone know how I could do this? I'm not pretty good with MySQL and times.

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
user2091820
  • 1
  • 1
  • 3
  • 2
    this is dangerous `SET banned=Yes WHERE username=$_POST[username]` You need to wash your code before putting it into sql statements. – Iesus Sonesson Feb 24 '13 at 16:06

4 Answers4

3

Simply change the column to something like "BannedUntil (datetime)" set that to one day into the future:

UPDATE Users SET BannedUntil = DATE_ADD(NOW(), INTERVAL 1 DAY) WHERE username = <username>

And to check if the user is banned

SELECT 1 FROM Users WHERE BannedUntil > NOW() AND username = <username>

If we get a row back, the user is banned, otherwise not.

hank
  • 3,748
  • 1
  • 24
  • 37
2

You save the time of the ban, and the duration of the ban.

Pseudo-code:

if (current_date == ban_time + ban_duration) { unban_user }
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1

Store the time when the user was banned and the duration of the ban. When the user attempts to access any page which needs to know whether he or she's banned or not - to display a "you're banned" message or whatnot - query the database for the user info. If he's banned, add the ban duration to the ban time and compare it to your current time. You can lift the ban if you've passed that computed time.

Mel Kicchi
  • 188
  • 1
  • 12
0

You must save ban time (unix timestamp field or datetime) and also duration of ban, when user try to login, you can simply check how much time passed after ban, if ban time passed, update row, and set banned column to 'No'

gaskar
  • 113
  • 8