Ok, ive got a minecraft server and im making a voting script for it.
I need a way to check the users ip in a mySQL.
If it doesn't exist it will put the ip in the database, and it will remove it after 24 hours in the database.
If anyone could help me do this, it would be great, thanks
There are plenty of tutorials online on how to achieve this, thus I wont give you the code. Just a few hints to get you started.
In PHP, you can get the user's ip address from the $_SERVER superglobal
$ip = $_SERVER['REMOTE_ADDR'];
To check if it exists in your table
$query = sprintf("SELECT COUNT(*) FROM yourtable WHERE `ip` = '%s'", mysql_real_escape_string($ip));
If it does not exist, store it using the INSERT command along with a timestamp.
As for removing after 24 hours, you can set up a cron job that runs through the table every hour or so and removes all entries that have expired. However, it would be easier to not remove the entries. Instead, when a user votes, just check the timestamp of his last vote. If 24 hours have passed since the last vote, just update the timestamp.
Basically, the workflow would be:
1. Get users's IP address.
2. Does this IP exist in table?
2a. If no, let user vote, and enter his ip in the table along with the current time in the timestamp column
2b. If yes, get the timestamp of his last vote. Has it been 24 hours since his last vote?
2b.a. If yes, let user vote and update timestamp column with current time.
2b.b. If no, do not let user vote / reject his vote. Leave timestamp column unchanged.
The easiest way is to do the following
$ip = $_SERVER['REMOTE_ADDR'];
// Run cleanup for given IP
$result = mysql_query("SELECT * FROM table
WHERE `ip`='$ip' AND `timestamp` > NOW() - 24*3600");
if (mysql_num_rows($result)==0)
// there is no record within the last 24h, let insert a new one
{
mysql_query("INSERT INTO table SET `ip`='$ip', `timestamp` = NOW()");
echo "Thank you for your vote!";
}
else echo "You may vote only once in 24 hours";
// run total cleanup query from time to time
if (rand(1,5)<2) mysql_query("DELETE FROM table
WHERE `timestamp` < NOW() - 24*3600");
ps: If you have different votings, each of them should have different unique ID. and the MySQL table, which had before two columns ip and timestamp, should have one more column voteid, for example. In that case the code above will change to
// voteid should be somehow set to the id of the voting.
// for example by GET request
$voteid = intval($_GET['voteid']);
$ip = $_SERVER['REMOTE_ADDR'];
// Run cleanup for given IP
$result = mysql_query("SELECT * FROM table
WHERE `ip`='$ip' AND `voteid`='$voteid` AND `timestamp` > NOW() - 24*3600");
if (mysql_num_rows($result)==0)
// there is no record within the last 24h, let insert a new one
{
mysql_query("INSERT INTO table SET `ip`='$ip',
`voteid`='$voteid', `timestamp` = NOW()");
echo "Thank you for your vote!";
}
else echo "You may vote only once in 24 hours";
// run total cleanup query from time to time
if (rand(1,5)<2) mysql_query("DELETE FROM table
WHERE `timestamp` < NOW() - 24*3600");
Related
Date, time resets every time i use UPDATE,
page1
mysql_query("INSERT INTO table (time) VALUES(CURRENT_TIMESTAMP()) ") or die(mysql_error());
page2 (not connected with page1)
echo $row['time'];
and after i use
$query = mysql_query("UPDATE tablica SET views = views+1 WHERE id = '".$id."' ");
date and time resets to current date and time
and if i delete UPDATE code everything is fine,
type is timestamp,
how to stop reseting date and time?
If table field is defined as timestamp, then it'll update automatically when record is changed / updated.
Read more about timestamps and issues here:
http://dev.mysql.com/doc/refman/5.1/en/timestamp-initialization.html
MySQL CURRENT_TIMESTAMP on create and on update
I would like to limit the access of a function i've created to once every 24 hour based on the users IP address.
I would also like the PHP script to delete the MySQL record if it's older than 24 hours.
If the user already has used the function within 24 hours, show them a message and prevent the script from continue running.
If the user already has used the function but 24 hours has passed since he used the function, delete the MySQL record and let the script continue running.
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$con=mysqli_connect("domain.com.mysql","domain_com","domain_password","domain_database");
$result = mysqli_query($con,"SELECT * FROM ipblock WHERE ip='".$ip."'");
while($row = mysqli_fetch_array($result));
if($ip == $row['ip']) //and code for checking how old the record is
{
// The user has used the function within 24 hours, kill the script.
echo "Come back in 24 hours";
exit;
}
else
{
// Looks clear, let them use the function
$MyFunction = true;
}
?>
I'm lost and as you can see i am also missing some statements for deleting old records (-24 hours)..
Could anyone provide me with an example of how to do this?
Thanks
Firstly store the IP and Access time as a pair in a table. Then you check is simple to see if there are any records in existence where the IP matches and the timestamp is less than 24 hours ago.
Before the function runs, complete an INSERT ... ON DUPLICATE UPDATE, and have the IP address as the primary key in the table.
The removal of old entries is then less of a priority and you can schedule this to be whenever convinient and remove entries where the access time is greater than 24 hours ago.
Store the access time as a myqsl date time, and do the comparison using the where clause:
WHERE accesstime >= DATE_SUB(NOW(), INTERVAL 1 DAY)
I'd create a table with function load
Store IP address and timestamp
When user loads the page, before running the function, select for IP adress and timestamp < 24 hours previous.
If you run the function: Store the users IP
No need to delete older records. But you could lik this to the verification function: delete all events older than 24 hours whenever the function is called for. Or set a cronjob to run every 12 hours to delete all older function load records
Basically, you can just create a timestamp field in the database. Then you compare using timediff() the stored values with the value of now().
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.
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.
You save the time of the ban, and the duration of the ban.
Pseudo-code:
if (current_date == ban_time + ban_duration) { unban_user }
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.
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'
I'm setting up basic cookie tracking and validating that by making sure the cookie is on their computer AND their IP matches the record I stored AND the record was stored within the past hour. I'm getting hung up on selecting the mySQL data from within the past hour.
As it stands, the column in my table is called 'timestamp', and it just contains the full timestamp inserted with NOW(). I checked around and thought I found the right call, but this didn't work:
$q = "SELECT * FROM conversion_data WHERE ip='$ip' AND timestamp < DATEADD(HOUR, -1, CURRENT_TIMESTAMP) ";
Taking out the timestamp call, it all works fine, so it's just that one part.
Thanks!
Try:
$q = "SELECT * FROM conversion_data WHERE ip='$ip' AND timestamp > DATE_SUB(CURDATE(), INTERVAL 1 HOUR)";
Your current query will select rows that are older than one hour. Changing the timestamp predicate so that it will fetch rows that have a time that is newer than or equal to, i.e. greater than or equal to, should work.
$q = "SELECT * FROM conversion_data WHERE ip='$ip' AND timestamp >= DATEADD(HOUR, -1, CURRENT_TIMESTAMP) ";
Please note that I'm not sure if dateadd works like this for mysql, apply a relevant function in your case.
I need to keep a field in a data-base and update it with a time somehow, then later I need to check that time to see if it was over 30 minutes ago or not, and if not, how minutes left until 30?
I am going to be doing this with PHP+MySql can anyone tell me the simplest way to do this?
Thanks!!
Let's assume you want to know how long ago the last update/insert in the table occurred.
You can set up a table with a timestamp field with an on update clause
CREATE TABLE foo (
id int auto_increment,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
primary key(id),
key(ts)
)
and then query the record with the largest value in ts
SELECT
TIMEDIFF(Now()-Interval 30 Minute, ts)
FROM
foo
ORDER BY
ts DESC
LIMIT
1
edit: This also works if you want to get all records that have been inserted/modified within e.g. the last 12 hours.
SELECT
TIMEDIFF(Now()-Interval 30 Minute, ts)
FROM
foo
WHERE
ts > Now()-Interval 12 hour
ORDER BY
ts DESC
edit2: there's also an off chance you might be interested in http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html:SHOW TABLE STATUS returns the following fields:
...
Update_time
When the data file was last updated. For some storage engines, this value is NULL. For example, InnoDB stores multiple tables in its tablespace and the data file timestamp does not apply. For MyISAM, the data file timestamp is used; however, on Windows the timestamp is not updated by updates so the value is inaccurate.
I could wrap all you insert and update MySql calls in a function something like the following:
function MySqlQuery($query, $res){
$result = mysql_query($qs, $res);
if($result === false){
mysql_query("QUERY STRING TO UPDATE FIELD IN DATABASE WITH NEW TIME", $res);
}
return $result;
}
Replace the "QUERY STRING TO UPDATE FIELD IN DATABASE WITH NEW TIME" with an actual update query and you should be good to go.
What I do is, put a Time Stamp on the latest record. Pull the latest record with a MySQL Query and then use the mysql fetch array function to get the time of that last record. This goes the same for using a database that is updated with the time only.
You would be able to manipulate that time with a function that compares the current time to the time on the record. From there you can display the time since last posting, and if it is over 30 minutes you can make it echo a message.
$currenttime = /* PHP Time Formatting you wish to use. */
$query = mysql_query("SELECT time FROM database");
$row = mysql_fetch_array($query);
echo "Last Record Posted #" . $row['time'];
$timesince = $currenttime - $row['time'];
echo "Time Since Last Post:" . $time - $row['time'];
if($timesince >= "30"){
echo "Over 30 Minutes!";
}
Let me know if you have any questions. The above code should give you an idea of how it would work, but it is a rough example.
Best of Luck!!
EDIT:::
Sorry, I misread the question, You would still need to enter the time into the database. You can still use the above code to pull the time and see if it is greater than 30 minutes or not.
For the Time Stamp check out the PHP Time Manual. You will want to pick the same time format for both the MySQL Input and the code I posted above.