How to easily run a PHP script in the background via terminal

I’m writing a PHP script that is going to serve as a daemon on my own server. As I’m writing it I want to test it a lot to make the daemon as stable as possible. Yesterday I wanted to let the script run overnight to monitor memory usage and make sure it didn’t crash.

If you don’t want to bother setting up a cron you can just run:

nohup php [file.php] &

All the output (stuff you echo) will be stuffed in nohup.out.

Posted at August 02, 2012, under PHP.

Sharing JSON data between jQuery and PHP using Ajax

When building interactive websites I often find the need to send or receive data after a page load. Ajax is the perfect solution for this problem, as it permits data sharing between the front end (javascript) and the backend (PHP) in the background.

Why would I need Ajax?
When you you start using Ajax it opens endless possibilities for your website, think about autoupdating a page (timing), showing specific content on a click or adding new content on a scroll (for example when scrolled to the bottom), or logging details (think Google Analytics).

When you use jQuery you can use a couple of very powerful build in functions to make Ajax a breeze.

more

Posted at February 05, 2012, under jQueryPHP.

Automatically backup your SQL database every day

As I build this site I decided to create my own site analytics instead of using Google Analytics. I need to create a solid backup system to save those analytics in case something goes wrong with my database.

This solution has two parts:

  • A mysqldump that saves an export of my whole databse into a file
  • A PHP script that mailes the export as an attachment to a Gmail mail account.

Both parts are automated using cron.

The mysqldump was pretty easy:

/usr/bin/mysqldump -u x --password=y z | gzip > /backups/export.sql.gz 2> /home/vgemtdpx/backups/export.sql.err

Replace the x with your database username, the y with your password and the z with the name of the database you want to backup.

That command tries to dump an export in /backups/export.sql.gz, if it can’t (if there is any output) store that output (the error) instead in export.sql.err.

You can cron that right away.

For the next part we use PHPMailer so we can attach the export. This is the backup.php:

$address = 'x@gmail.com';
$password = 'y'
$name = 'Mike van Rossum';

$path = dirname(FILE);

require $path . '/path/to/phpmailer.php';

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPDebug = 2;

gmail settings

$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username   = $addres;
$mail->Password   = $password;

the senders

$mail->SetFrom($addres, $name);
$mail->AddReplyTo($addres, $name);

$mail->Subject = "daily backup of DB: mikevanrossum.nl";
$mail->AltBody = "See attachment";
$mail->MsgHTML("See attachment");

$mail->AddAddress($addres, $name);

add attachments

$mail->AddAttachment('/backups/export.sql.gz');
$mail->AddAttachment('/backups/export.sql.err');

Once you’ve replaced it with your data you can cron this script to using something like this:

/usr/local/bin/php -q -f /path/to/backup.php

Make sure the mysqldump runs first and the PHP runs an interval later (mine is set for 15 minutes), my experience is that both take a couple of seconds to complete but it’s better to be safe than sorry.

Beveiliging van mijn eigen CMS

You’ve found a dinosaur, this post is only here for archiving purposes. The content is outdated and is not applicable anymore.

In de post ga ik in op de beveiliging die ik heb toegepast op deze website. Ik verantwoord keuzes uit mijn ontwerp en leg uit hoe ik problemen heb opgelost. Graag rekening houden dat dit mijn eerste eigen PHP website is en ik alleen zaken heb beveiligd waarvan ik wist dat ze een mogelijk gat waren.

more

Mijn eigen CMS voor ServerSide Scripting

You’ve found a dinosaur, this post is only here for archiving purposes. The content is outdated and is not applicable anymore.

Ter afsluiting van het vak ServerSide Scripting heb ik mijn eigen CMS geschreven in PHP (en een hoop JS). In deze post licht ik alle onderdelen toe omdat ik een aantal niet-standaard elementen heb verwerkt.

Op dit moment is het admin gedeelte nog voor iedereen te bekijken, ik verwijs er in deze post een aantal keer naartoe. Zodra dit vak is afgerond timmer ik alles dicht. Nu kan niemand op de online versie wijzigingen aanpassen (met uitzondering van het vullen van mijn analytics tabellen).

Alle code (op een config bestand na) is te vinden op mijn github.

more