Executing PHP Script Via CRON and CURL

CRON is a crucial component of a Web Server. It is where you run your scheduled tasks such as notifications, mass mailing, backup and stuff like that. I really thought it was just easy but when I have to setup a server from ground up, it was a bit tricky.
Creating a cron job is easy. Simply issue this command:
crontab -e
This will bring you to the cron job editor where you enter the time it should execute and the script / executable file to run.
For a time saver:
@hourly /home/user/script1.sh
@daily /home/user/script2.sh 
will execute the specified script hourly and daily respectively. A more advanced time/date is available on the cron manual pages. Since I only need hourly and daily jobs, so that’s it.
Inside the script, you need to specify the command line interpreter and the script itself. According to PHP Docs, executing a PHP script is as simple as:
php -f my_script.php
However, there are some settings that are not available on the command line version of PHP. One of the possible cause I think is the path resolution where the PHP script path is not resolved to the path of the PHP script, but to the path of the Bash script. With a very limited time, I immediately switches to CURL.
CURL can call URL as if human visits it. Based on that, we can setup curl to execute out script which is available via URL. So here is my bash script:
#!/bin/bash
curl http://test.com/test_ada32151asd32a1s54asd1_xxx.php
So that makes a great combo! CRON + CURL = FTW!

White-listing IP addresses for your Apache virtual hosts

I tried setting up some sites on public hosting company where I needed to set it up in a way where only specified IP addresses are allowed to access them. Below is what I did.

Basic config

Below is the basic configuration:
1
2
3
4
5
6
7
8
9
<Directory "/srv/httpd/htdocs/mydomain.com">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from 127.0.0.1
    Allow from xxx.xxx.xxx.xxx
    Allow from xxx.xxx.xxx.xxx
    Deny from all
</Directory>

Organized

Since I’m doing this IP blocking for many sites, it make sense to simplify the configuration so that I only need to edit a single list of IP that would apply to all sites. What I did is put the config on file and include it on each virtual host config. Below is the filename and the sample content.
File: /etc/httpd/block-world.conf
1
2
3
4
5
Order deny,allow
Allow from 127.0.0.1
Allow from xxx.xxx.xxx.xxx
Allow from xxx.xxx.xxx.xxx
Deny from all
Then for each virtual host, I include the file like this:
1
2
3
4
5
<Directory "/srv/httpd/htdocs/mydomain.com">
    Options Indexes FollowSymLinks
    AllowOverride All
    Include /etc/httpd/block-world.conf
</Directory>
That’s it. Share and enjoy.

Force Download CSV File

        $html =  $this->load->view('download_report', $result, TRUE);
        header("Content-Type: application/xls");
        header("Content-Disposition: attachment; filename=test.xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        echo $html;