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.
Related
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.
I am using php and Mysql.
I have a record in my database and i want to check status value of the record after 24hrs of its insertion if it is still in pending or no changed i want to send an SMS and an Email to some person relevant to the status.
To achieve this i have implemented a solution in which i used a php code but it need to continuously execute and check status of record that may cause DoS.
In Another solution i create a trigger but i can't able to send db values as parameter to the php file.
so what should be its solution????
Two ways came into my mind for this.
First, (the good way) using cronjob. It's a really good way.
Second, (bad way for your purpose) Check to see which rows has age of 30 days in each request coming to website. There are cons for this way as the website doesn't get visitors for some days then SMS or Emails won't be sent on time. Another bad thing is it executes on every request so loading time and memory will be consumed during this process.
created a php file which contains a functionality of send sms and email.
then create cronjob:
wget http://example.com/x.php
provide full address of your file.
while creating cronjob set time interval 24hrs
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.
I'm building a site where the users can control a webcam to turn it left and right.
Every user gets one minute of action. There is going to be a queuing system on the site that will allow one by one the users to control the camera.
So my question is the following, does anyone have a suggestion on how to build this queuing system? Are there any tutorials or code I can use?
Thanks a lot!
Have a database table to track the queue for example:
queue (id, session_id, start_time, last_update)
When users hit your page, insert them into the queue table. Use a regular ajax call (perhaps 30 seconds) on the page to poll the server to see if the current users turn is up.
If the user is the first record in the table then it's his turn, so update the start_time to the current time and send your ajax response telling the browser to display the UI with the buttons for the camera movement.
When a button is pressed, verify on the server side that it is infact this users turn and his start_time was < 1min ago, before allowing the action. If his turn is over, delete him from the table so that the next user becomes the first record and gets his turn, then send a response to the browser so that it can hide the camera UI and give a message.
In addition to inserting into the queue on hitting the page, also check to see if the user that is controlling the camera has had his 1min, if so then delete his record (or could be done on the cronjob below).
Each time the ajax poll fires, update the users last_update with a timestamp. Use a cronjob or just on the server side calls to check if any of the records in he queue have a last_update that is older than a short time, e.g. 30 seconds., if any are found then delete them because these are users that are no longer on the page. This will also prevent attackers trying to fill up your queue.
On the same cronjob, check if the user who's turn it is has the start_time populated, if after 30 seconds he hasn't started, delete from the queue.
The ajax calls would make it nice and seamless, but they aren't essential, if the user has Javascript disabled you can still detect that and use a meta refresh of the whole page instead.
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.