I have a website that let users download PDF files, I made a download limit 10 files per day for each user and I stored the numbers inside MySQL table under column named "downloads".
Each day I use this command to set all values to 0 for all users:
update users set downloads = 0
I contact my web-host provider and they refused to give me super user privilege to make schedule for this column to reset itself each 24 hour.
Is there any other way to make little code in PHP to reset that column from outside or any other way to do that?
Update: I can not run cron job either!
Save in the database the date and the downloads number in the same field, every time you update this field ask if the date inside is today if not save today's date and zero downloads for example your field will be:
date_downloadsTimes
03-02-2019_2
Store the last reset time in a file or the database and upon every new download request check if the last reset was 24h+ ago. If so, reset the download counter and update the time. That way you are not reliant on a background job.
Alternatively you could set up a route (some URL to be called) that is periodically called from an external source (either your pc or some web service), and resets your download counters.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I am currently working on a application with a edit button:
My edit button:
<th title="Edit task" class="edit" style="float: right; $color;">
<?php
echo "<a href=edit.php?id=" . $row["id"] . ">
<i class=material-icons>edit</i>
</a>";
?>
</th>
My goal is that only one person can edit a article at a time. Is this even possible? If it is how can I do it?
If you only want to disable button to avoid concurrency editing then it is a terrible idea because users always can open the edit page by direct link. In fact, you need to implement concurrency control.
You can implement locking of record to concurrency control.
For example, you can use the following simple algorithm for the implementation of concurrency control:
You add param that will contain an end time of locking to article
On the edit page: you compare this time with current time if locking time greater then current time then you inform a user about the impossibility of article modified, else you show edit page to the user
On the edit page: if you show edit page of an article to the user then you increase end time of locking param for example in increments of 5 minutes
If the user finishes a record modification and saves it then you should reset end date of locking
But instead of this algorithm, you can implement optimistic locking.
Optimistic locking provides users open an edit page at the same time, but it forbids to save record in a parallel way. It is a better way to avoid troubles of parallel modification of a record
Optimistic locking consists in acceptance or rejection сhange of record to depend upon record version.
It works following: each article has a 'version' param that contains number of record modification. One user opens the edit page of an article that has version equal to 1 and another user opens it at the same time. Both of them save the same article. At first, you get one request for record modification and compare version of a stored record with version of an updated record if they equal one another then you should accept change and save an updated record in a storage and increase 'version' param in increments of 1 otherwise, you should reject change. As a result of this algorithm, the stored record will have version equal 2. Next, you get the second request and compare versions again, but since version of a stored record is equal 2 and version of an updated record from second request equal 1 this change is rejected. Of course, you must inform the user about the rejection of its change and give the user an ability to update the new variant of record
This is what I have done in my application:
When a user presses edit button, log the timestamp in database and redirect user to edit page. In edit page set a timer and show it to user that can edit record until timer runs out.
For example based on your record you can consider 2 minutes for user to edit a record. Time will start from 01':59'' and reach to 00':00''.
For other users when click on edit button, you should check if another user is editing that record and you can do this by calculating time from the log in database and now.
If user edits record under 2 minutes and saves it, you can remove edit log to let others users to edit.
it's not completely possible as of my point of view.
but try to do with below way.
you could add two columns in your database 'open' and 'time'
Set 'open' to 'true' or 'false' if someone is editing it. Also set a timestamp to the 'time' column. Replace the timestamp every so many seconds while editing takes place.
If someone else opens it, check the 'open' column and of it's 'true', calculate the time passed from the 'time' column. If it's over a certain time (say 2 or 3 minutes), assume the other user isn't editing anymore and allow this user to edit.
To make things clear,
you need to add one ajax call which is update your timestamp after every min/sec.
if you are in edit page.
so if someone close the browser then timestamp will not update and after next user is tried to edit then if timestamp is older then 2/3 min then allow to edit that user. beacuse we assume that user is close the tab or browser so flag is not updated but timestamp is older.
this is not perfect solution but you can try it.
There is the easy way and the hard way.
Easy way: Check concurrency on save with a timestamp.
You can simply add a timestamp (usually named update_at) in your database along with your data.
You put the timestamp in the page (or a $_SESSION variable which is safer) so that when you POST your changes you also post the timestamp. On the server side you verrify if the timestamp in your database is the same as the one you posted and if it's not you return a message saying that the post was editted.
If you care about what the user was typing (and most of the time you should) you can always display both edit side-by-side and allow him to choose/edit one of them.
Hard way: Store lock timestamp in your database
You can add a timestamp with your data the same way as in the easy way (but this time we'll call it last_edit_time).
When a user enters on the edit page you do this:
You check if the last_edit_time is 30 seconds away from the current time
If it's been less than 30 seconds you return a message saying the data is being editted
If it's greater than 30 seconds you set the value of your last_edit_time to the current time and show your edit page
Once the user is on the edit page you start a javascript interval of 15 seconds
Every 15 seconds you send an asynchronous request (Ajax) to the server telling it to update the last_edit_time to the current time. We send it every 15 seconds so that if the asynch call takes a long time the user comming to edit right on the 30th second doesn't load the page.
Doing this will assure you that only one user can access the page at the same time. If you want to make it even more secure you would add the user Id next to your last_edit_time to verrify on the moment of saving if it is the same user.
You can even set the last_edit_time to null once the user is done editting. This will unlock the data for everyone as soon as possible.
Of course there can be some edge cases where two users opens the page in the same second. To fix this you can always increase the precision of your timestamp (milliseconds instead of seconds).
You can do this very easily Please follow these step.
1)Create one table name as editLog where you column
id,member_id/user_id,is_editable(default 1)
2)Now in initial stage table is empty first user came to edit fire AJAX when user click on edit at the same time check from the database if any data is not exiting in the table then allow him to make the changes(and when you save or update the data in table check with member id also to ensure member are same) and if data exit then give him the error of warning..
Follow these step if you still have confusion comment.
The table from which you are getting $row["id"] add one more column in it and name that column as ipAddress with default value of null.
Now when user go to "/edit.php?id=$id" update the ipAddress column of table with user ip. You can easily get the user ip address by JS or by Server variables.
After that in edit.php you can easily check if current ip is equal to the ipAddress like:
if($_SERVER['REMOTE_ADDR'] == $ipAddress){
// Allow edit
}
else {
// Donot allow edit
}
And after edit is done or browser close you can set ipAddress to null.
I did a little research and I found out that it is really hard to get this done... What you can do is run a check if there is a recent update on the article (same as pending edits on SO)
According to my understanding, the solution to your problem is very simple.
First, make a table or tables in which you will record the update queries made by users in your own way
Make a script which will execute the oldest query of the table which you made in the first step and then delete that query on success mean after updation is run successfully
Learn Cron Jobs
Create a cron job for that script for a certain interval of time
And enjoy your life :)
I have a condition that I have to update data automatically on my website, which is triggered by time that I've set before.
EXAMPLE :
i've set the update for 29-05-2018 20.20 then the website will do update in that date and that time without any trigger, and no need to
open the website.
Create a Controller name Cron.php
than make a function run_every_day()
now put all your condition in this function than go to your cpanel search for a cron tab. Click on it create a cron task
give it the url root/index.php/cron/run_every_day
in the cron seeting set the time 1 day so it will run each day on specific time you set.
Hope this helps
I'm doing a module of rental videos, I've done the shopping cart but I only need to execute an update query if the user don't submit his order before within five minutes, I need a timer (I'm using PHP and MySQL) that is not reset when changing page, some like an $_SESSION, when user add a item in the cart his status on his table be "unavailable" (just to mention something) and the timer begins, and when it reaches zero do a update query and change his state to "available". What interests me most is getting the timer is maintained and not restart when changing page.
I need ideas from you, your suggestions are welcome, Thank you!
You Can store the time-stamp into Mysql table when user adds item to cart, And then create function in a separate php file that checks for time stamps that are expired and updates the status of items that are having expired times,
Now You can either use 'Crons job' to auto check for expired timers or run that function on any user interaction with system as per your needs...!!! :)
Firstly start session and store time stamp both in database and in session when user registers. Then write code that checks last request time is more than 5 mins by subtracting last time stamp with current time. If it's more than 5 mins then change state to available.
Also you can achieve this with MySQL triggers.
http://www.mysqltutorial.org/mysql-triggers.aspx
having read noumerous guides I still cannot find a solution that solves my problem..
I have a download.php page that serves downloads.
Ive created a new mysql table called "downloads" where I want to store downloads or whatever.
What I want to do with timestamps is basically a check to see if that IP has downloaded in the last 24 hours and if it has not, add it to the downloads table for the next check.
Or even easier: Add every downloads.php call to the downloads table, saving an IP address and a timestamp. That timestamp is zhen checked before a download and if the users IP is present in the DB, do x.
Any suggestions?
Notes: A file.php exists. Users visit pages like file.php/filexxx and click on download. download.php then performs a bunch of checks and serves the file. With that I want to limit downloads if the user has downloaded x files in the last 24 hours. Im currently using cookies and this is not an optimum solution. Any help?!
Much appreciated!!
Use Mysql TimeDiff function to get total hours.IF it is more than 24 hours. Insert again as a new count.Also combine the IP validation with your query.
SELECT TIMEDIFF(now(),first_downloaded_time) ,ip from downloads where ip='$ip'
first_downloaded_time is the column which stores timestamp;
Run the above query and you will get timestatmp like '23:43:44';
Use substr() method to get only hours
substr('23:43:44',0,2);//this will output 23
Use this value to insert downloads ip, if it is greater than 23 or no value(for newer entry)
I am trying to build an online attendance system where employees sign in and check in daily except for weekends and vacations.
so , my idea was to create a daily attendance record as a table in the database.
Attendance_date_daily date
Employee_ID number(auto generated)
Check_in_time time
Check_out_time time
Attendence_status varchar
I am using codeigniter v 3.0.0
it's easy to create a model to get the current time and save it in the database when the user check in/out.
but the problem is that, if the user was absent for a day or more , then the system will not create a record for those days.
moreover, i can't create the records beforehand. since i don't know when the user will have his/her vacation and working days may differ.
what is the best way to create and manage those daily records?
One possible solution may be to allow the employees to do their check-in each day normally which would populate the database for those days.
In order to add the absence records for those who have not checked in you could use CRON or something similar to schedule a task at perhaps midnight each day. Use this task to target a url that would run a method in a controller that will check employees against the daily records. Obviously for those whom have checked in no action will be performed, although for those who have not and are not marked as on vacation or not working you update the database to add the absence records.
With a system like this you would typically invoke the url to perform the update using whatever system you use with wget or something similar to 'load' the url and force it to run. A security consideration also would be that you'll want to add in a secret key as a GET parameter or something similar so that the method can check that it's being invoked via the task and not e.g. someone visiting the url by comparing the GET parameter with a stored key that you've set.