On my website I use cron job on cpanel.
I have this code below in the construct area of controller but it stops the cpanel cron job from working.
if (!$this->input->is_cli_request()) {
show_error('Direct access is not allowed');
}
Question Do I need the code above. if I use my cpanel cron job? I just want to make it more secure.
<?php
class Cron extends CI_Controller {
public function __construct() {
parent::__construct();
if (!$this->input->is_cli_request()) {
show_error('Direct access is not allowed');
}
$this->load->library('email');
$this->load->model('members_model');
}
public function message()
{
$admin_email = $this->config->item('email_host');
$admin_email_pass = $this->config->item('email_password');
$companyname = 'Riwaka';
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://mail.yourdomain.co.nz',
'smtp_port' => 465,
'smtp_user' => $admin_email,
'smtp_pass' => $admin_email_pass,
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->email->initialize($config);
$members = $this->members_model->get_approved_members_for_cron_job();
if ($members) {
foreach ($members as $member) {
if ($member['approved'] == '1' && $member['approved_email_sent'] == '0')
{
$this->email->set_newline("rn");
$this->email->clear();
$this->email->from($admin_email, 'Admin');
$this->email->to($member['email']);
$this->email->subject($companyname .' Account Approved');
$this->email->message('test');
$update = array(
'approved_email_sent' => '1',
);
$this->members_model->update_approve_email_send($member['email'], $update);
$this->email->send();
}
}
}
}
}
2
Answers
To prevent direct access from a webpage:
you need to add this line
with CI 3.0 you can
is_cli()
returns TRUE if the application is run through the command line and FALSE if not.as by my comment, the cpanel cron job pattern is:
,
see docs here
related post
Solved now. CodeIgniter Cron Job through Cpanel
It seem to be a issue from path I had used. On cpanel before I had
That was not working proper if I wanted to use this code to prevent access
So now I changed to
All works fine now