MySQL timestamps and PHP - php

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)

Related

How to set values inside column to zero every 24 hour?

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.

plan a effective algorithm to check who is online to the web site, with PHP and AJAX

i need some help to plan an effective algorithm to check who is online to my web site, with PHP and AJAX.
(i dont need you to write any sourse to me!!)
the biggest problem is how to know when the member get off.
in the end, it will be a list of online members that alwayes update, like what facebook have.
You can use a timestamp, with PHP you can do strtotime("now");
You add that timestamp to the user's table in MySQL, or their session info. Then, you update that timestamp whenever the user changes pages or accesses the site. To determine if the user is offline, you can remove the timestamp when the user logs off, and if the user just exits without logging off, you would just compare the timestamp to the current time.
For example, you could look to see if 15 minutes has passed by checking the timestamp variable like this:
if($timestamp <= strtotime("-15 minutes")) echo "User is offline";

Limiting page visit for same IP with php

I have a page, i want so that 1 IP could use the page for lets say 10 times a day at most. 1 refresh = 1 use. After some research i read that this can be done with javascript or php but with cookies. Couldnt the user just delete his cookies and use the page again?
Is there a way to do it only with php and no cookies? Any suggestions?
Note: i am not using any databases.
It can't be done without a database. You simply need to keep track of all IPs and you can't do that without a database.
Of course, a simple text file can also go for a database. For every visitor that accesses a certain page, log that IP address to a text file. To see whether they can access the page, just count how many times the IP address is in the file.
You can create a database table and then on each view either insert the IP (if its not already in the database) or increment views by 1. If views = 10 don't allow the user to visit.
You can use htaccess file too. But you could write ipaddress/nrofrefreshes records to a text file located on the server.

Expirable links to profile pages

I'm working on a project in core php with mysql for a tutor agency in which i have send mail to people with links to the tutor profile pages matching their required criteria. I want these links to be temporary links and want them to expire in say 72 hours, I'm not sure how to go about it. Any ideas?
originally link is something like this
"http://mysite.com/mysubdirectory/index.php?action=view_credentials&tutorid=".$tutor_id;
Thanks for help.
For anyone looking for the same answer as I was, I followed shadyyx and with a bit of effort i was done!! thanx everyone!
Some solution that bumped into my mind:
Create a DB table for these links where You would save the link, unique identifier (some hash), date it was created and date when it will expire.
Then create a page (script) that will get that link (containing not the full URL but the URL of that script and a unique identifier of the link stored in the DB). This script will try to search for the link identified by the unique hash while conditioning current date and time to the link expiry date.
If the link is found and not expired then You would redirect user to that page otherwise You will end up with a message that the link is expired or not found.
Should be pretty easy to implement.
Hope this idea will help You.
Use a table to store the TIMESTAMP when sending the link and redirect to an error if the link is accessed after TIMESTAMP+<72 hours>
Your tutor_id shall be stored in database before you send it. This would help sending unique id (just in case :), and do some checks if i.e. ID used come with is valid. So when anyone enters the link, your index.php should check if all parameters are valid, query DB agains value of $_GET['tutorid'] and see if it is not expired. And you'd know this because your DB record shall hold TIMESTAMP with creation date. Having creation date you can check how old it is and accept or reject the tutor_id
Protected Links is a PHP Script from codecanyon, it expires links after a fixed time and much more..
It can be used to expire a tutorial link or any other link in 72 hours or any number of hours, by IP address, for single user or mutiple users. A php coder can integrate this in their application with some effort.
http://codecanyon.net/item/protected-links-expiring-download-links/2556861

How to handle user online status when he/she close the browser?

I am having table to track user online status by setting 'is_online' field to 1. Now User get logged in the site and shut down his/her system or power down at that time he/she logged in.
Here in database 'is_login' has the value '1' which means he is still using the site.
So I have to do some logic to change that value to '0'.
How can i handle this situation using PHP ?
The normal solution is to store a timestamp in the table which you update every time the user does something. Users with a recent timestamp (say, five minutes) are shown as logged in, everybody else are logged out.
It doesn't even have to be updated on every page load. Keep a timestamp in the session with the last update time as well, and only update the table when the database flag are about to expire.
Store an time-since-last-activity. When it's been longer then a specified time - treat as if offline. You should replace the is_online with an DateTime field, and update it every time the logged in user visits the website.
On the place you want to select all online users, instead of :
SELECT * FROM users WHERE is_online = 1
You could use:
SELECT * FROM users WHERE is_online >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)
you can't. HTTP closes the connection once the page has been delivered. On the server side alone all you could do is checking if a user has requested a page within a certain amount of time.
JavaScript might be your friend. You could use it to create a ping to a php script which then updates the last request timestamp. Once there is no more pinging, the user is gone and you'd know.
If you have trouble with javaScript, a good point to start would be here: http://www.w3schools.com/JS/
You might want to focus on the AJAX and HTTP request stuff.
cu
Roman
You can use jquery to update your site each X time, so a function is executed each 10 seconds, as following code:
function updateTheBBDD(){
... calling mysql_connect -> mysql_insert -> mysql_close
}
setInterval("updateTheBBDD()",10000);
Also, when painting the user status monitor instead of consulting the BD and:
1->green
0->red
You should:
actual - time > 10 ---> red
actual - time <= 10 ---> green
Due to the stateless nature of the web it is not possible to know exactly when the user left your website. So you can't really trigger any code to change the mentioned value.
As the other have said one has to store the time-since-last-activity to get an approximate idea when was the last time the user was actually doing something with the website.
HTH :)

Categories