I saw some website that after you have registered, you can use the service like posting a comment immediately.
You may have to wait for 5 or 10 minutes to be able to start, like this StackOverflow website.
Once you have asked question, you have to wait 20 minutes to select your answer.
If I want to do it in JavaScript or PHP can anyone show me how to do it? I assume you have to compare the time with current time-stamp, but don't know how to exactly implement it. Thanks in advance!
Assuming you have a datetime string stored in a variable, you could do something like:
// TODO: get a datetime string into $time
$minutes = 5;
if (strtotime($time) + $minutes * 60 < time()) {
// It's been 5 minutes since $time
}
In PHP:
$wait_time = 10*60; //10 minutes * 60 seconds
if(time() > $start_time + $wait_time) {
}
sure, you'll want to compare the date of posting, with the current date, but then formulate the conditions that will validate a new post or the selection of the same.
Each question that is posted will require server side storage, if not exclusively in a db then there will need to be at least a record in a table linking the question to the user.
Once a question has been posted you could either have the server side script that loads the question carry out the following tasks.
Get the question entry
Is the person that posted the question the current requesting user
[yes]
Is the request time minus the post time greater than (number of mins * 60)
[yes]
Include select answer buttons (html objects or buttons)
Alternatively you could always show the buttons and carry out the above logic and return some indication that the user must wait x minutes before they can select an answer.
You have to store the creation time (of whatever object or saving) along with the item being created in the database. So for example, if you create a question on SO, they probably store the time you posted the question. Then when you go to view your question, they probably get the difference between the current time and the time stored.
So say that you want to implement a 20 minute wait after posting a question before you can select an answer. Then you upload a question at 5:00 PM. When you attempt to view your question at 5:05PM the same day, the difference in time is 5:05PM - 5:00PM, which is 5 minutes. Take 20 minutes - 5 minutes to get the time the user must wait, and send that back to the page.
From javascript you can get that time limit (15min), and create a setTimeout method to "unlock" the answer feature. The code would look something like this:
<input type="hidden" id="waitfor" value="15">
$(document).ready(function () {
var waitfor = $('#waitfor').val();
timer = setTimeout(function () {
// let the user know they can post an answer
}, waitfor * 60 * 1000);
});
Also, I'd let the user post an answer whenever they'd like, but prevent it being submitted on the client side if it hasn't been 20 minutes.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to add a new feature in my website, i.e I want to fix the time limit of 1 month and after that it expires that means the status of that user will change from 0 to 3.
I have many fields in my table including
id
status
today_date.
In this, today_date stores the time and date of registration and by default the status is 0.
Is this possible using php-mysql? If possible, please help me out.
Thanks in advance.
You should take another approach to achieve this.
You already have registration date in DB right, so when user enters valid credentials in the login form, compare the current date with the registration date, if that difference is more than 30 days then update the status columns with 0 and also do not allow that user in at that time.
How to calculate number of days between two dates in php:
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24)); // This will be number of days. Store it in a variable and set a condition on it.
If you have a page to process the Login POST data, then build your sql query to fetch the column containing the user's time of registration.
Take $time_of_reg as user's time of registration.
What ever timestamp you're using, convert it to a UNIX
timestamp using strtotime() function.
Now adding 30 Days to the user's time of registration $time_of_reg_plus_30 = strtotime('+30 Days',$time_of_reg)
Now use an if condition to perform an update query if the current time is greater than or equal to 30 Days + users time of registration. This way you can perform an update query to your status column, only if the user has been registered for 30 or more days.
if(time()>=$time_of_reg_plus_30)
{ //perform sql update query}
(having these values in a UNIX timestamp, makes it easier to compare)
<?//perform sql query to fetch user's time of registration, '$time_of_reg' in this case.
$time_of_reg = strtotime($time_of_reg); //Convert to UNIX timestamp
$time_of_reg_plus_30 = strtotime('+30 Days',$time_of_reg); // Add 30 Days To Registration Time
if(time()>=$time_of_reg_plus_30)
{ //perform sql update query if user has been registered for 30 Days or More
}
Remember to place this in the page that handles your Login POST data, this is a good way to achieve your objective without a cron job, as the user's status is updated whenever the user attempts to Log In.
Cron job is the best solution for this. Think you don't want to apply cello tape on balloon.
If you would like follow the steps below:
Create a PHP script & save in a file e.g. myCron.php
If you have Linux server with Centos create a scheduler script with bash How to create a cron job using Bash automatically without the interactive editor?
If you have cPanel go to cron jobs, set the file path & time to run.
I'm basically looking for something that follows:
Player completes transaction
System updates MySQL with 10 min countdown
Player is unable to complete more transactions until countdown finishes
Continued..
Imagine it would work something like this:
mysql_query("UPDATE `players` SET `time`= 10:00 WHERE `id`=$player[id] LIMIT 1");
and
if ( 0 <$player['time']) {
$error='yes';
Also what field type do I give the MySQL field, that's what confuses me most. How it's stored and if it needs to be 'translated' back for the php.
Thought I'd post my answer for future people with the same problem and only complex answers.
All you have to use is time()+900 or $timePlus = time()+900 which inputs the current time + 10 minutes, as an integer into the MySQL field. You can then use time() or $time = time() whilst verifying against the 'time' field to see if 10 minutes is up. Example below:
$player['time'] > time() // where the time stored in 'time' is greater than the actual time causes an error.
Hope I explained that right and credit to #flyingeagle413 for also suggesting this method (although admittedly a bit late) :P
So basically, What I wanted to happen was, I wanted to submit an article which submits my current time() and the amount of hours until a certain event. I wished for it to calculate the amount of time in hours from the time() in the database to the amount of hours since that article was posted.
So if I posted an article for an event happening at 3pm, and the current time is 12pm, It would say "Event in 3 hours". And if I were to check again at 2pm, It would say "Event in 1 hours".
Sorry if this seems like a noob question, But I am quite the stupid person when it comes to maths and stuff.
Thanks in advance for any input, I appreciate it!
Presuming you store the time of the article as a timestamp, and you retrieve the value in the variable $articleTime:
$hoursLeft = floor(($articleTime - time()) / 3600);
Ok basically I have a form that a user submits but I need a 'cool down timer' so that the user cannot submit it again for a given amount of time.
I could not find out how to do this in php which would be preferred if possible.
Thanks in advance.
You can store in database (example: MySQL) timestamp of last successful submit and on every submit check if value from database + cool down time smaller or equal to current timestamp.
You can get current timestamp in PHP with time().
You have to use sessions for that!
When the user submits the form you have to add the timestamp to his sessions:
$_SESSION['last_submit'] = time();
Now when he submits the form again simply compare the timestamps, e. g. :
if(isset($_SESSION['last_submit']) && ((time() - $_SESSION['last_submit']) < 60 * 5)) { //time in seconds! 60 seconds = 1 Minute and 1 minute * 5 = 5 minutes!
die('Wait a few');
}
$_SESSION['last_submit'] = time();
// regular form processing here!
What we do here is to check first: is a previous timestap set? And then if time() - lastsubmit is less then 5 minutes.
If these all return true, the form was submitted to "early" and we simply die. If not we refresh the sessions' s timestamp and can move on.
And of course don' t forget to start the session!
session_start();
On the very top of the page!
As I' ve read in a comment (thanks again!) a user just could use another browser or clear the cookies. To prevent this (as good as possible) you have to take the IP into account as well.
For this you have to use a server-side database! Store the client' s IP into this database with the timestamp along and then in your if statement you don' t need to get the timestamp from the session, but from the database. Use the client' s IP to get the assoc. timestamp.
Im adding a comment section to my forums and i want to add a delay, Maybe 30 seconds or so to stop people spamming along with a captcha .
Whats the best way to do this? Only way i can think is using cookies, any other suggestions?
You could use PHP sessions to do something like this, but not sure how 'fool' proof it is.
The idea would be do something:
if(isset($_POST['cmdComment'])){
$_SESSION['comment_posted'] = time();
}
Then you could have a function say checkTime() which you could put inside there to subtract the session value from the time it is now. If the difference is >= 30 seconds, then continue with the post and set the $_SESSION['comment_posted'] to the new time, otherwise ignore the post request.
When a user post their comment, write the current timestamp in a session and add it additional 30 seconds:
$_SESSION['postedTime'] = time() + 30;
When you want to check if 30 seconds are passed, get the current timestamp and compare them: if the current timestamp is bigger then one in session, then 30 seconds are passed:
$timestamp = time();
if($timestamp > $_SESSION['postedTime'])
{
// allow posting
}
else
{
// decline
}
For more security, you could insert user's IP + post timestamp in the database.
When the comment is inserted in the database, make sure to keep track of the user and timestamp.
When the user goes to post again, check if the last comment he posted was 30seconds before.
Deal with this accordingly
A forum usually has members, well atleast I don't know any that do not have it.
Now if you post a comment I assume you put that in a table called something like Comment with an ID, POST_ID, USER_ID, MESSAGE, POST_DATETIME
Atleast thats how I would do it.
Now you have the datetime when the user last commented on the a certain post. Now you can query your database whether he or she can comment again