Automatic check if mod_rewrite rules are enabled

Sometimes there is a need to check if mod_rewrite rules are enabled or not from a server script and in this post, I will show a method of automatic detection that we use.

To pass all user requests to front-page controller in Modera products we use the following rules:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}  !-d #exclude requests to real directories
RewriteCond %{REQUEST_FILENAME}  !-f #exclude request to real files
RewriteRule .+ index.php [L]

For checking if these rules are active we are making HTTP request to predefined URI, which in case of enabled rules will be handled by front-page controller script index.php which will reply with predefined response, if rules are not enabled then this server will return back with a 404 HTTP response.

(more…)

Gunnar Optics glasses – if you need something yellow in front of your face

One more review of Gunnar Optics glasses. I am considering to buy one of those already for a while, but hesitating because of unit price and because I do not have any particular problems with my eyes. I would probably buy them just to try for 30$-40$.

But do they actually work? Most probably yes… at least to some extent making everything around you yellowish :)

mod_rewrite server-variables full list

Official mod_rewrite documentation is not very detailed about server-variables values which is usually used in RewriteCond rules. There is an easy way to get a list of server-variables values by exporting server variables into environment variables and than printing them by your web script, PHP works well :)

(more…)

Double motd and last login

After upgrading openssh package to openssh-5.1_p1-r1 on our Gentoo system we started receive duplicated motd and last login messages on every ssh login. The reason for this is that new pam scripts for ssh comes with mod_lastlog and mod_motd enabled. First set of lastlogin and motd messages is displayed by pam system and second is displayed by ssh daemon itself because by default PrintMotd and PrintLastLog are enabled. To avoid duplicated lines on every ssh login you have to add following lines to /etc/sshd_config:

PrintMotd no
PrintLastLog no

or disable pam_lastlog and pam_motd in /etc/pam.d/system-login.

I would recomend the first method because pam scripts are more configurable.

0.09 + 0.01 = 0.0:

Wow! What a weird PHP bug! Result of 0.09 + 0.01 is… guess what, it is 0.0: (yes, zero dot zero colon).

s6urik@web1:~$ echo '
 
Other float values looks to be processed correctly:
<pre lang="bash">s6urik@web1:~$ echo '
 
System is running on Amazon EC2 small instance. Here is more info:
<pre lang="bash">s6urik@web1:~$ php -v
PHP 5.2.4-2ubuntu5.3 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 23 2008 06:44:49)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
s6urik@web1:~$ uname -a
Linux web1 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 GNU/Linux

This bug was successfully reproduced on custom compile of PHP 5.2.6. (latest available version on the moment).
Under Valgrind everything is fine and result of expression equals 0.1 as it should be, it means that this is rather CPU issue than PHP as Derick said.

Reminder: The only valid measurement of code quality

This is just a reminder post dedicated to anyone who is trying to measure code quality.

(more…)

JavaScript error handling and General Best Practices

A very interesting presentation from Nicholas C. Zakas named “Enterprise JavaScript Error Handling (Ajax Experience 2008)”, worth to read.

I am collecting application development best practices for quite a long time, and after studying slides from this brilliant presentation my “Error Handling” part increased. Some day I will publish a full list, but now just “Error Handling” part as a very short summary of presentation.

  • Assume your code will fail.
  • Log errors.
  • Provide a debug mode.
  • Don’t tell the user application is not working unless absolutely necessary.
  • Recovery from error if it is possible if not than message the user immediately.

A better mysqldump for your projects

I would like to share with you a simple bash script for dumping MySQL table data and structure separately.

Basically it’s a mysqldump wrapper. It allows you to specify tables which data will be ignored in a “data” mode and removes AUTO_INCREMENT automatically added by mysqldump to CREATE TABLE clauses in a “structure” mode.

I usually have this script in tools/ folder of my project and using it this way:

$ tools/sqldump.sh structure > sqldumps/structure.sql
$ tools/sqldump.sh data > sqldumps/data.sql

(more…)

Warning: comparing dates in MySQL might cause you problems

In this post I just want to warn developers about using MySQL date functions for comparison reasons. Problem is obvious, it is a possible clock skew between your application server and MySQL server time.

MySQL has timezone support and it’s timezone might be different from application server’s one. MySQL might be located on other machine than application server and there might be clock skew between these machines, etc.

Let’s assume that you have a database table with field named “publish_date” of a “datetime” type. Value for this fields is set automatically by application server. In this case dangerous query for selecting records with “publish_date” in past will be:

SELECT * FROM `mytable` WHERE `publish_date` < NOW();

If there is a clock skew between MySQL and application servers than this query might return incorrect results. To avoid this problem instead of using NOW() function in this case we should use constant value generated by your application.

Be very careful comparing dates coming from your application with MySQL dates.

Data generation software

Did you ever have a need to populate your application with data for testing? Probably the best way is using data generation software in these cases. “Data Generator” is a good one.

It’s a free, open source script written in JavaScript, PHP and MySQL that lets you quickly generate large volumes of custom data in a variety of formats for use in testing software, populating databases, and scoring with girls.

You can try this demo script directly on generatedata.com server.