I render a php layout file with some variables, then I mail it to some address. There is no problem when doing this task through the web. I just call some controller->action and everything is ok.
Additionally, I want to send a mail whenever its time is set to sent. Cronjob will read a timestamp of email column from db and sends when its time is come.
But the thing is Yii::$app->mailer->compose('some layout file path here') uses output_buffering internally. Its ok for php that runs on web, but it does not work for php that runs on CLI. output_buffering can't be turned on php cli. Cronjob uses php cli not php web.
I got stuck, what can I do? Any suggestions? Is it possible to create controller and run action of it? I have one solution: run curl with cronjob and access action of controller which runs on web through cli, but its not the best way I think as it overloads the web part.
Please give me the best, fast and not overloading way to do this
Related
I'm creating a website that requires a file to be generated and stored on the server periodically (an XML feed for iTunes). The page is generated using ExpressionEngine. I discovered that the website's current server has a very restricted cPanel and doesn't have access to cron.
So I'm considering two options; find an alternative way to access the cronjobs (if they are available), or find an alternative way to created regularly scheduled tasks.
Regarding the first option, how would I go about determining if a server has cron available? I'm not sure how useful this would be anyway since I don't think the server allows shell access (it's a very basic setup for people who aren't tech savvy).
Regarding the second option, a friend mentioned to me that the functionality of cronjobs can just be done in PHP. How would I go about this?
Or, am I perhaps thinking too much with this? The page in ExpressionEngine that outputs the XML file is domain.com/itunes/itunes_feed. This just has some EE tags that outputs the relevant XML and the resultant page is in .xml format. Is it enough to just submit the above url to iTunes, or does it have to be a url to the actual pre-existing file on the server?
Option 1
Simply contact your hosts and ask them do they support cron jobs, and if so, how to set up.
Option 2
I only just set up my own set of cron jobs yesterday..
Create a php file that runs the code you want,
Set up and account on https://www.easycron.com/
Upload your php file to easycron
Set the times in which you would like your php code to run
Simple as that! Does that make sense?
this isn't the best method for doing the task, but how would you run a cronjob of a zend view.
The view is used to generate a file using an output buffer and then save the file on the server, it runs once a day.
Would it just be a matter of calling the url of action of the controller with curl:
23 50 * * curl http://pclite.com/statistics/generate
The application required authentication though.
If you are the admin of the server, I will not do this way,
I will code a PHP page using curl to download and save the file, since you coding a php file,you are able to simulate the login procedure , you can write the username and password in the php file, and make sure the file is saved by where you want
then I using LYNX in the corn, a text browser , it will call this php file once a day, so you don't have to record any username password in the cronjob and this php do what ever you wan to grab
Since you said, that this is not the best method for doing such a task, i won't tell it again :D
If the cronjob runs on the same server your webserver is on, you could check the client-ip and skip authentication if they are the same. Because if the "attacker" can send requests from your own server to the application you really have a serious security issue.
So, yes. If you skip authentication when the ip is the same you just need to call the url.
As any other class Zend_View can be instantiated from anywhere and in particular Zend_View can render to a variable. This means that you do not need to call the whole web application if all you want to do is render something.
As stated your other option is to have an entry point to the application and call it to get the return. But if you're just saving some file to the server it could be perceived as a better approach to have the cronjob be a script that does any thing. This way you will also save some load of your web application. The last thing may not be so relevant but what if in the future you want to call this endpoint several times per day for a lot of users or something?
So, you can create a CLI script that includes Zend_View and renders within itself. As always with Zend Framework the implementation choice i left entirely to you.
Not sure if I understood the use/purpose of PHP entirely, but what seems to me that a .php file only executes when it is being called/executed by something before it, could be a html or a php file.
A thought, is it possible that a php file written, and it would just be activated by its own, example over a duration span of time, every week or so it would do something?
Thanks for looking...
You are looking for a cron job. This allows you to save a line of code on your remote server that will execute based on the criteria you set. You can make it execute a variety of files but PHP files are definitely one of the files you can execute in this manner.
As mentioned by nathan, you will be looking for a cron job. This is a server side setting in the server that will call a url at a set interval.
You seem to not really understand how PHP works. PHP scripts are called server-side before sending data to the client. They are run once when the client is accessing the script.
Sorry if this is a duplicate question...I've searched around and found similar advice but nothing that helps my exact problem. And please excuse the noob questions, CRON is a new thing for me.
I have a codeigniter script that scrapes the html DOM of another site and stores some of that in a database. I'd like to run this script at a regular interval. This has lead me to looking into cron jobs.
The page I have is at myserver.com/index.php/update
I realize I can run a cron job with curl and run this page. If I want to be a bit more secure I can put a string at the end like:
myserver.com/index.php/update/asdfh2784fufds
And check for that in my CI controller.
This seems like it would be mostly secure, but doesn't seem like the "right" way to do things.
I've looked into running CI from the command line, and can execute basic pages like:
php index.php mycontroller
But when I try to do:
php index.php update
It doesn't work. I suspect this is because it needs to use HTTP to scrape the DOM of the outside page.
So, my question:
How do I securely run a codeigniter script with a cron job that needs HTTP access?
You have a couple options. The easiest would be to have your script ensure that the $_SERVER['REMOTE_ADDR'] is coming from the same machine before executing.
Another would be to use https and have wget or curl use HTTP authentication.
What exactly went wrong?
What error did it throw?
I have used CI from the command line before without any problems.
Don't forget that in case you are not on the folder the script is located you need to specify the full path to it.
something like
php /path/to/ci_folder/index.php update
Also on your controller you can add.
if ($this->input->is_cli_request())
// run the script
else
// echo some message saying not allowed.
This will run what is needed only if the php script is running on the command line.
Hope it Helped.
Hey folks, the way i understand it is that cron can be used to execute php code by launching the php interpreter and passing it the path to the script to be executed.
The code I would like to schedule is in a codeigniter controller/model. So basically the controller contains 3 functions that perform some db stats. Each function will have its own schedule.
How can I secure that controller so that the code doesn't get executed maliciously? do I pass some creds to the controller as part of the cron job? or do i take that code an set it up as a separate ci app?
Any thoughts on the matter would be appreciated.
thanks
You shouldn't create a controller for doing a script. You should just create a normal PHP script, and launch it via command line/cron.
The script shouldn't be in your public web directory, it should be elsewhere (in a script folder for example), not accessible by the public (a script shouldn't be a web page).
Because if you have a script as a controller, that means you lanch the script via the HTTP server, which isn't secure, and in your cron task you'd have to use something like wget "localhost/mycontroller/myaction" (less clean).
You could always move the file outside the web directory, so you can only access it from the server side. Another way is to change the permissions on the file, so your server cant read the file, and execute the cron under root (not recommended).
As for credis, you can make the script only run if you pass the correct get variable. For example, the script only runs when you call:
http://localhost/script.php?chjfbbhjscu4iu793673vhjdvhjdbjvbdh=bugy34gruhw3d78gyfhjbryufgbcgherbciube
I don't think the querystring idea is that bad actually, especially if this URL is being passed along your own network behind a firewall then there's no real cause for concern.
Another security feature you could implement is making sure the "client's" request IP address is equal to the server's IP address, hence the script can only proceed if it is being called from the server that executes the controller action.