How to send on a specific day in php? - php

I want to a e-cards or something like that. The user can choose the e-cards, after chosen, he must enter the some fields like name(to and from), email(to and from), message and I want to let user to choose which date to send the e-cards.
How to send the e-cards on specific day? I need to write a script that run every new day? How to do that? Sorry, I am new to php... (but not beginner like not even know how to execute mysql query, get message from url etc)

Yes, you need a script that runs every day. (Barring ridiculous maneuvers like trying to fake this by checking on Web requests.) The usual way to do this in a Unix context is called a cron job; if your hosting provider is Unix-based, you should look into what they provide for making cron jobs available to you. On Windows there's a parallel service called Scheduled Tasks.

A similar question was disscussed here resetting-a-mysql-field-value-without-user-execution
I'll just reiterate: There are web based cron services too. This could come in handy if you only got a shared hosting plan and can't add cron jobs. They will call an URL at a regular interval that you can set. Usually very cheap. (Cheaper than upgrading to a root-access server anyway.)
Just search Google for web based cron
ciao!
/0

This is for *nix.
Let's say you have a php script that sends email on a specific day called mailer.php
<?php
//mailer.php
if (date("m/d/Y") == "06/02/2009") {
mail("client#email", "Subject", "Body");
}
?>
We are going to assume that you already have cron daemon running in the background.
If you have root access to your machine, then setting up a cron job is simple as editing a file.
Open up /etc/crontab file and add the following task:
1 14 * * * root php /path/to/your/scrip/mailer.php
This means, as a root, the mailer.php script will be running daily at 02:01PM. You can change the numbers to whatever you desire.

This is somehow complex. First it depends on your system. If it is Linux/BSD/Unix/Solaris then you have this handy utility as cron. If you are using Windows, you have Scheduled Tasks. Run your script daily (or as you wish) and check what cards you have to send today.

Related

what is the best practice to create a scheduled task to access a URL? [duplicate]

I have a site on my webhotel I would like to run some scheduled tasks on. What methods of achieving this would you recommend?
What I’ve thought out so far is having a script included in the top of every page and then let this script check whether it’s time to run this job or not.
This is just a quick example of what I was thinking about:
if ($alreadyDone == 0 && time() > $timeToRunMaintainance) {
runTask();
$timeToRunMaintainance = time() + $interval;
}
Anything else I should take into consideration or is there a better method than this?
That's what cronjobs are made for. man crontab assuming you are running a linux server. If you don't have shell access or no way to setup cronjobs, there are free services that setup cronjobs on external servers and ping one of your URLs.
I'm answering this now because no-one seems to have mentioned this exact solution.
On a site I'm currently working on, we've set up a cron job using cPanel, but instead of running the PHP Interpreter directly (because we're using CodeIgniter and our code is mapped to a controller function, this probably isn't a great idea) we're using wget.
wget -q -O cron_job.log http://somehost/controller/method
-q is so that wget won't generate any output (so you won't keep getting emails). -O cron_job.log will save the contents of whatever your controller generates to a log file (overwritten each time so it won't keep growing).
I've found this to be the easiest way of getting 'proper' cron working.
If you have a cPanel host, you can add cron jobs through the web interface.Go to Advanced -> Cron Jobs and use the non-advanced form to set up the cron frequency. You want a command like this:
/usr/bin/php /path/to/your/php/script.php
Have you ever looked ATrigger? The PHP library is also available to start creating scheduled tasks without any overhead.
Disclaimer: I'm among their team.
if you're wondering how to actually run your PHP script from cron, there are two options: Call the PHP interpreter directly (i.e., "php /foo/myscript.php"), or use lynx (lynx http://mywebsite.com/myscript.php). Which one you choose depends mostly on how your script needs its environment configured - the paths and file access permissions will be different depending on whether you call it through the shell or the web browser. I'd recommend using lynx.
One side effect is that you get an e-mail every time it runs. To get around this, I make my cron PHP scripts output nothing (and it has to be nothing, not even whitespace) if they complete successfully, and an error message if they fail. I then call them using a small PHP script from cron. This way, I only get an e-mail if it fails. This is basically the same as the lynx method, except my shell script makes the HTTP request and not lynx.
Call this script "docron" or something (remember to chmod +x), and then use the command in your crontab: "docron http://mydomain.com/myscript.php". It e-mails you the output of the page as an HTML e-mail, if the page returns something.
#!/usr/bin/php
<?php
$h = #file_get_contents($_SERVER['argv'][1]);
if ($h === false)
{
$h = "<b>Failed to open file</b>: " . $_SERVER['argv'][1];
}
if ($h != '')
{
#mail("cron#mydomain.com", $_SERVER['argv']['1'], $h, "From: cron#mydomain.com\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1");
}
?>
If you want to avoid setting up cron jobs and whatnot (though I'd suggest it's a better method), the solution you've provided is pretty good. On a number of projects, I've had the PHP script itself do the check to see whether it's time to run the update.
The down-side (okay, one of the down sides) is that if no one is using the app during a certain period then the script won't run.
The up-side is that if no one is using the app during a certain period then the script won't run. The tasks I've got it set up to do are things like "update a cache file", "do a daily backup" and whatnot. If someone isn't using the app, then you aren't going to need updated cache files, nor are there going to be any database changes to backup.
The only modification to your method which I'd suggest is that you only run those checks when someone successfully logs in. You don't need to check on every page load.
Cron is a general purpose solution for scheduling problems. But when you go big and schedules go high in frequency, there can be reliability/overlapping issues. If you see such problems, consider something like supervise or more sophisticated monit.
If you using cpanel u should add this like:
/usr/local/bin/php -q /home/yoursite/public_html/yourfile.php
I would outsource the cronjobs with www.guardiano.pm and call a url every X minute. When your url (i.e www.yoursite.com/dothis.php) is called than you execute some code. If you don't want to let the web request the page when you want you can allow only request in POST and send some parameter that only you know with guardiano.pm
Thats what I would do because I do that on my pet projects. Have fun!
The method you are using is fine, if you don't want to use cronjobs or anything external, but these can be heavy to check each time a page loads.
At first, some cronjobs can probably be replaced. For example if you have a counter for how many users have registered on your website, you can simply update this number when a user registers, so you don't have to use a cronjob or any scheduled task for this.
If you want to use scheduled tasks, I suggest you to use the method you are using right now, but with a little modification. If you're site has enough hits on a day, you can simply make the tasks run (or the tasks check function run) only for 1% or maybe 0.01% of the hits instead of all of them, the percentage you should use depends on the page hits you have and how many times you want to run the task. So, simply add a randomizer to achieve this feature.
You could simply use a function like this;
if(rand (1, 100) <= 1) { // 1, 100 is used to generate a number between 1 and 100. 1 is for one percent.
// Run the tasks system
}
Command line PHP + cron would be the way I would go. It's simple and should fit the bill. It is usually installed with PHP as a matter of course.
If you do not have the option to setup a cronjob you can call the script with cUrl (as alternative to wget - same functionality). Just do a scheduled task on your local machine that executes the cUrl function.
If you want something more abstract, you might consider using something like a PHP scheduler.
For example:
https://github.com/lavary/crunz
https://github.com/peppeocchi/php-cron-scheduler
And also, to parse the cron expression, you could use an existing library such as https://github.com/mtdowling/cron-expression. It provides a lot of useful methods to help you figure out information of a cron job.
Hope that helps.

Send mail to different customer on their birthday

I have found that there is related topic.However, there are some difference because the people is asking for ubuntu server but i am using the Apache (xampp****) the latest version, so are there any method to let the system automatically send mail by checking the date ?
Can it applies to some schedule sending as well? thanks
I have the script for sending it , the only thing i need is how to schedule send.
Edit:
Make it simpler , assuming only windows is the only platform. Can i do some php script and create the cron job in os , so that my client can schedule their mail in my system instead of doing it in the os?
3 rd party cron job is good suggestion but there is limitation (what if their service break down etc...) and i want everything is based on own system
If your xampp is on Windows, you can use windows-schedulers to call a particular script (PHP script in your case, preferably by an URL), which checks the birthdays of all the users and sends them mail accordingly.
If your xampp is on Linux/Unix/etc, you can use cron-jobs, and rest is same!
There is also a third way, a third-party scheduler or say online-cron, which will remotely call yours script (php-page).
Example:
If you are doing the cron-settings of OS yourself, you can still let the configure as to whom to email in your system, rather than OS.
After all, a cron so is to do something repeatedly, in your case, its just calling your scripts, by an URL.
Now regarding the time settings, you can make the CRON call your script every hour, and your php script will check the appropriate time, and fire the mails!
schedule a script in crontab or scheduler which will check the birthday of your customer
and send them an email
this script will run everyday at particular time of the day
use server cron job to run a script that will send out email. ur script should compare current's date and customer's birthday date.
You could use a cron-job. With PHP that get all the people that you want to send an email and gets template and send it just substitute the name and other stuff
You may use Wishing Application. Getting started provides good details on how to kick started. At minimal you need only two things the excel file having wish details (Birthday and on work anniversary), and a configuration file (application.properties) to provide the mapping details and other configurations. There are various options to run this application, locally (background, foreground, docker, windows-scheduler etc) on cloud
Disclaimer : I am the owner of application.

Is cron job/scheduled task service enabled?

I'm making a website that need's a cron job or a scheduled task to send emails weekly but the problem is that I don't know if the host supports it...
This website is for an institution and I can't put it in any other host...
The host is using Apache.
If it doesn't support, how can I send weekly emails automatically, without changing host?
EDIT
I forgot to say that I'm new in cron jobs.
Apache has nothing to do with cron jobs as the system running on the host must trigger the job (which then can invoke a script running under apache).
Do you have SSH access?
Add the job to the /etc/crontab file or the cron-file of your user.
Or do you have a Webinterface to some management software (e.g. Plesk)?
Search there for an option for Cron jobs or Scheduled tasks.
If not you can use some external services which will call an URL on you site to trigger the job like http://www.setcronjob.com/.
First, ask your host if cron jobs are supported (they should be), check your panel (if there is one), try to set up one and see if it works etc.
If not, one possible way (other than to find an external service that will do the call to the script for you) is to add a function to your code that will be called every time a visitor of your site enters a specific page (e.g. the index). There, you will check if the weekday is the day that you want to send the e-mail. If yes, then send the mail, having a flag (e.g. a record in the db) to check if it has already been sent.
Of course it's not the ideal solution, all the others (the actual crons or an external service) are better, but since it is a solution, it's worth mentioning.
Have you read some documentation about cron-daemon?
This code is check the the cron is running
ps -ef | grep cron
After then you need to create a file which can handle the email address pickups and send th mails.
And also need to add something like this to the cron (it just an example send out something at every sunday, 23 o'clock):
0 23 ? * 0 php /path/sendnewsletter.php

Resetting a MySQL Field value without user execution

I need to reset a MySQL Field value automatically at midnight. It is a specific column in a specific row in a table. I know how to do this in PHP but I do not know how to execute the PHP Script at midnight without someone having to do it themselves. Do you have any viable solutions?
Edit:--------------------
Preferably without using Cron Jobs.
If you are running on linux you would use a cronjob
On most distros there is a command called crontab that schedules tasks for you and a specific format you need:
0 0 * * * php /path/to/file.php
EDIT:
Wrote this before you edited yours :p I'm not sure there is any other way. Do you have a specific reason not to use a cronjob?
You may not even need to specify the php part if the php file is marked as executable i.e
0 0 * * * /path/to/file.php
Just do "chmod +x /path/to/file.php" form the linux command line
There are web based cron services too. Basically you set up an account and then they visit an URL of your choosing at a regular interval. On your server you set up a page that does what you need to get done. Preferably give it a unlikely-to-find-name like myjob789273634ahhh8s2nhw8sghusgf874wfu.php. You get the idea. (Remember that PHP-scripts timeout after like 30secs.)
Here's a google search:
**oops I'm new so I can't post URL apparently. Just search for "web based cron".
Good luck
/0
You could write a job scheduler into your program that runs jobs in a cron-like way. It would require a user to interact with the system to trigger, but it might be good enough depending on your needs. This is much more complicated than just running a cronjob, and does not ensure prefect timing (since it wont run until a user hits a page).
You'd probably need to add a table into you database that would list the job, the time you want them done, and a locking flag to avoid concurrent attempts to run the job. Each time your script runs, you'd check this table for overdue jobs and run them as needed.
Asking how to reliably set off a script at the same time every night without cron (or a scheduled task, on Windows) is like asking how to make a dynamic website without a server-side language.
If your app absolutely relies on a script running exactly at midnight, cron is a requirement. If your users have a hosting company that (stupidly) does not permit cron, they're going to be out of luck.

PHP: running scheduled jobs (cron jobs)

I have a site on my webhotel I would like to run some scheduled tasks on. What methods of achieving this would you recommend?
What I’ve thought out so far is having a script included in the top of every page and then let this script check whether it’s time to run this job or not.
This is just a quick example of what I was thinking about:
if ($alreadyDone == 0 && time() > $timeToRunMaintainance) {
runTask();
$timeToRunMaintainance = time() + $interval;
}
Anything else I should take into consideration or is there a better method than this?
That's what cronjobs are made for. man crontab assuming you are running a linux server. If you don't have shell access or no way to setup cronjobs, there are free services that setup cronjobs on external servers and ping one of your URLs.
I'm answering this now because no-one seems to have mentioned this exact solution.
On a site I'm currently working on, we've set up a cron job using cPanel, but instead of running the PHP Interpreter directly (because we're using CodeIgniter and our code is mapped to a controller function, this probably isn't a great idea) we're using wget.
wget -q -O cron_job.log http://somehost/controller/method
-q is so that wget won't generate any output (so you won't keep getting emails). -O cron_job.log will save the contents of whatever your controller generates to a log file (overwritten each time so it won't keep growing).
I've found this to be the easiest way of getting 'proper' cron working.
If you have a cPanel host, you can add cron jobs through the web interface.Go to Advanced -> Cron Jobs and use the non-advanced form to set up the cron frequency. You want a command like this:
/usr/bin/php /path/to/your/php/script.php
Have you ever looked ATrigger? The PHP library is also available to start creating scheduled tasks without any overhead.
Disclaimer: I'm among their team.
if you're wondering how to actually run your PHP script from cron, there are two options: Call the PHP interpreter directly (i.e., "php /foo/myscript.php"), or use lynx (lynx http://mywebsite.com/myscript.php). Which one you choose depends mostly on how your script needs its environment configured - the paths and file access permissions will be different depending on whether you call it through the shell or the web browser. I'd recommend using lynx.
One side effect is that you get an e-mail every time it runs. To get around this, I make my cron PHP scripts output nothing (and it has to be nothing, not even whitespace) if they complete successfully, and an error message if they fail. I then call them using a small PHP script from cron. This way, I only get an e-mail if it fails. This is basically the same as the lynx method, except my shell script makes the HTTP request and not lynx.
Call this script "docron" or something (remember to chmod +x), and then use the command in your crontab: "docron http://mydomain.com/myscript.php". It e-mails you the output of the page as an HTML e-mail, if the page returns something.
#!/usr/bin/php
<?php
$h = #file_get_contents($_SERVER['argv'][1]);
if ($h === false)
{
$h = "<b>Failed to open file</b>: " . $_SERVER['argv'][1];
}
if ($h != '')
{
#mail("cron#mydomain.com", $_SERVER['argv']['1'], $h, "From: cron#mydomain.com\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1");
}
?>
If you want to avoid setting up cron jobs and whatnot (though I'd suggest it's a better method), the solution you've provided is pretty good. On a number of projects, I've had the PHP script itself do the check to see whether it's time to run the update.
The down-side (okay, one of the down sides) is that if no one is using the app during a certain period then the script won't run.
The up-side is that if no one is using the app during a certain period then the script won't run. The tasks I've got it set up to do are things like "update a cache file", "do a daily backup" and whatnot. If someone isn't using the app, then you aren't going to need updated cache files, nor are there going to be any database changes to backup.
The only modification to your method which I'd suggest is that you only run those checks when someone successfully logs in. You don't need to check on every page load.
Cron is a general purpose solution for scheduling problems. But when you go big and schedules go high in frequency, there can be reliability/overlapping issues. If you see such problems, consider something like supervise or more sophisticated monit.
If you using cpanel u should add this like:
/usr/local/bin/php -q /home/yoursite/public_html/yourfile.php
I would outsource the cronjobs with www.guardiano.pm and call a url every X minute. When your url (i.e www.yoursite.com/dothis.php) is called than you execute some code. If you don't want to let the web request the page when you want you can allow only request in POST and send some parameter that only you know with guardiano.pm
Thats what I would do because I do that on my pet projects. Have fun!
The method you are using is fine, if you don't want to use cronjobs or anything external, but these can be heavy to check each time a page loads.
At first, some cronjobs can probably be replaced. For example if you have a counter for how many users have registered on your website, you can simply update this number when a user registers, so you don't have to use a cronjob or any scheduled task for this.
If you want to use scheduled tasks, I suggest you to use the method you are using right now, but with a little modification. If you're site has enough hits on a day, you can simply make the tasks run (or the tasks check function run) only for 1% or maybe 0.01% of the hits instead of all of them, the percentage you should use depends on the page hits you have and how many times you want to run the task. So, simply add a randomizer to achieve this feature.
You could simply use a function like this;
if(rand (1, 100) <= 1) { // 1, 100 is used to generate a number between 1 and 100. 1 is for one percent.
// Run the tasks system
}
Command line PHP + cron would be the way I would go. It's simple and should fit the bill. It is usually installed with PHP as a matter of course.
If you do not have the option to setup a cronjob you can call the script with cUrl (as alternative to wget - same functionality). Just do a scheduled task on your local machine that executes the cUrl function.
If you want something more abstract, you might consider using something like a PHP scheduler.
For example:
https://github.com/lavary/crunz
https://github.com/peppeocchi/php-cron-scheduler
And also, to parse the cron expression, you could use an existing library such as https://github.com/mtdowling/cron-expression. It provides a lot of useful methods to help you figure out information of a cron job.
Hope that helps.

Categories