User-specific timer actions in a MySQL database - php

I have a database full of users and each user has timestamps for a specific events they've executed. I would like to fire off an action for each individual user based on their timestamps. So for example: user1 hasn't logged in for 5 days, send her an email or put her user status to "idle" mode.
What would be the best way about doing this?

I don't know if it's the best way but usually you setup cron jobs to that kind of things.
Your cron job would be a simple PHP script, it would query the database to find all the users that the system should process and send emails etc ...
If you are on windows the scheduled task should be a drop in replacement for cron jobs.

Related

Scheduling emails with cron jobs from a mysql table

I am trying to set up a system where a user enters some information in a form and an email will be constructed where the information is saved into mysql.
I am trying to figure out how to make it so the email will be sent, for example, 20 minutes after the user makes their input. (Without the user staying on the browser).
I need this delay as I need the ability for an admin to log on to a page to look at the email and possibly edit it before it sends.
Is this possible through a cron job. Am I able to set one up that automatically checks sql table for an update and then sends the email after a certain time?
Or is it possible to delay a php script with the sleep function and then send the email. Can I make the PHP script still run when user has closed site and left?
you can use mySQL to store the data sent by the user. this data will be accessed later using another script triggered by a cron Job: if you have the ability to set cron jobs in the control panel or via access to the server, go ahead, use cron tab syntax to define when the job will be triggered, this website may help you:
https://crontab-generator.org/
another approach is to use external service to trigger an event every interval, the event could be accessing the cron job script via HTTP.
if you want your email to be sent exactly after 20 mins, please add a field to your mysql table indicating the desired send date(beware of timezones).
you may also want to add a flag indicating if the email is sent, so you do not send the same email twice.
You can't (easily) have a PHP script stay alive that long.
Your best strategy, IMO, would be to have the PHP script create the email file, and notify the human.
Then you can have PHP run a shell script which uses the "at" program to schedule a task to happen in 20 minutes. At is a cousin of cron, but is better suited for this job.
That scheduled task will be to take the e-mail message, move it some place else (like a "done" directory), and pipe it through your mailer. tip: /usr/sbin/sendmail -t < myEmailFile will work on most Linux boxen.

Schedule alarm notification system php

I need some help regarding the implementation of the following alarm.. Here's the flow of the program, user login to the system and then they can click the hyperlink create schedule and then from there they can create schedules using the form. After which, user can choose to start this schedules that they created and they can allocate a timeframe to it. For eg, if the user assign the schedule to run at 2pm the system will have a pop up to notify and inform the user to run this schedule probably 15 mins before 2pm..
I would like to know what are the ways to implement this in php and if possible is there any reference i can use on the website? I've tried to find but apparently most scripts are paid etc.
I think you are looking for scheduler kind of thing, to do this in php
you can use scheduler for windows os
cron job for *nix based os
Yes the answer to this question is pretty much related to CRON job. Take a look at this answer
Execute Query on a Specific Date and Time
You need to write a PHP script that scans the db and sees for the user who should be notified (i.e their deadline has arrived). Run this script in the scheduler (maybe every 1 hour) and it will do the magic for you. Good luck with your notifications :)
No cron job is needed if you only notify user with popup on the web site, however, you will need a cronjob if you want to send emails.
You will have to store users schedules (possibly in a db table)
When the user logs in php should check if there's a task due in the schedules table for that user: if yes show the pop up.
You will need to create an ajax call (with users id) that will call a certain php (say ajax.php) file every minute. You can help yourself with jquery.
The ajax.php should check if there was a task due in the past. If yes it returns job details (as json or html, you chose) else it just returns that there are no jobs.
When the calling ajax code recieves an answer from ajax.php, it does nothing if the answer is that there are no jobs or displays a popup with job details recieved from ajax.php.
The user can dismiss the call (delete it from the schedules db table) snooze or reschedule it (update the due date in the schedules db table).
Don't forget about security especially in the ajax.php: it has to check if the user is logged in.
Only if you want to prompt non logged in users by email you will need to set up a cron jobs, that will ping the ajax.php file periodically.

In PHP, how to keep a process running to monitor for an event?

I have a DBMS assignment to create an entire online bidding website that allows users to do many things like bidding on a timed auction, buy-it-now based on timed availability, and search among MANY more things. I've completed the website, with one minor exception: once a user wins a timed auction, that user must be notified that s/he is the winner and given 24hrs to checkout the item.
My early approaches consisted of simple PHP scripts that queried the DB when the user was on the page to see if the bid time ended and to see who the winner is, but there are two things wrong with that approach. 1. The user has to constantly refresh to find out if s/he won -- if they're on the page shopping. 2. If the user doesn't log into the site for a month after the auction ended, s/he will not know for a month that s/he won the auction and therefore misses the 24 hr window to checkout.
I've been reading about cron jobs, but my concern is that, since they are scheduled jobs, there are no convenient times to schedule the jobs so that they get processed efficiently. What I mean is that I can't continuously monitor and let every user who wins know instantly after the auction ends.
I also read on PHP exec( ), but I'm not sure that it's the tool that I need to run this task because once exec( ) finishes, it doesn't re-run.
So I guess, my question is if anyone has an idea of what I could implement to help me with this task.
You should use cron - have it run every minute or so to check the DB to see if some auction ended. The greater problem I think you're construing is the notification to the user. If its the case they may not be on the site (or on some random page when they win) then don't rely on notifying them via the website and certainly not from a PHP request they may or may not make to the site. Instead, consider having the PHP called by cron to check for winners send an email to them to notify them.
Think about Facebook (only because given the user base its the most universally experience example) - they provide a notification area if you're on the site, but if you're not on for a while, they send you an email letting you know something has happened. This is what you should do.
You should reconsider using cron scheduling, you can run jobs at very fast intervals.

How can I run an infinite loop on my website? (for a reminder/deadline announcer)

I'm building a website and i want to implement a reminder feature that alerts people if they have any projects or activities that are due or simply alert them of any timer they may have set. I have a clue about how i could do it with javascript's timer functions but i want it to run all the time and add reminders to a queue if the users are not online when the event occurs. Is there any way of doing this or do i have to use bash or python on the server?
Further explanation on how it's supposed to work:
- infinite loop checks for the time every X seconds, if there's a a reminder up between now and X seconds ago, print it to the user it belongs to - if the user is not online, put it in a file or something which is checked, when the user logs in, for any missed reminders.
another way i thought of is to use a local script (pyton or something) to check the database for reminders that are due, every X seconds, and write them in a file on the web server. then, server-side scripting will read it every X seconds and print the reminders to each logged account (and delete it at that point). This way no reminders are skipped even if the person it belongs to is logged out.
Any idea on how to do this more elegantly?
Use Cron Job instead and run your script with that to connect to your database and to do other tasks.
this functionality can be done with ajax and php. I wouldn't check the db when the user isn't online. I guess it is better to check reminders on login (unless you want to for example email them when they occur->then you need cron like mentioned in the other answers). you'll need to make a php script that checks which reminders have to be set and return them in an array/json. on login call this page. when user is logged in you can request your reminder php periodically/timer with ajax.
Having an infinite loop is by default bad design. PHP won't like it at all (memory). Javascript isn't ideal for the same reason too. Apart from that you don't want a javascript sleep to block your UI when waiting between tries.
Also, I wouldn't check notices when the user is not online. This is a useless way of using resources. Simply save the last time a user was online and display past notices that he hasn't seen before on login.
The cleanest solution in my book is some javascript library capable of eventdriven actions. An example of that is node.js. With events you don't need to check every N time but simply have an event triggered if some condition is met.

Send a SMS text message after period of time via PHP

I have a flash application. I'm trying to send users text messages via php after they did certain things on the flash.
For example,
Text user 1 hour after they did
thing#1.
Text user 10 minutes after they did
thing#2.
Text user 1 day after they did
thing#3.
....
I'm thinking of setting up a table for the list of things that will trigger the text. Then have a cron job set up to check the timestamps of each user finishing those things.
Is there a better way out there for doing this?
That sounds like the approach I would take. Whenever you have an action that takes place at a different time than the web request, you need to "queue" that action up to be completed later. Then you need a script that runs (however often you want) that checks the queue (i.e. DB table) and processes the items in the queue.
You're on the right track.

Categories