2010
08.09

It happened to me quite a few times now, that I imported a mysql dump file and afterwards noticed that the character encoding was broken.
To help you guys preventing this from happening use following import command.

1
mysql -u root -p --default_character_set utf8 NAME_OF_YOUR_DATABASE < mysql_dump_file.sql

The important argument is “–default_character_set utf8” to preserve the encoding.

Enjoy

2010
06.11
1
ErrorException [ Warning ]: htmlspecialchars() expects at most 3 parameters, 4 given

If Kohana 3 throws this exception, that means that your currently installed PHP version is lower than 5.2.3 .
This can be easily be fixed by updating PHP to the latest stable version (recommended) or by changing one of Kohana (not recommended).

To fix it code wise open following file with your preferred editor

1
/system/classes/kohana/html.php

Search for…

1
return htmlspecialchars((string) $value, ENT_QUOTES, Kohana::$charset, $double_encode);

… and change it into

1
return htmlspecialchars((string) $value, ENT_QUOTES, Kohana::$charset);

Done :)

2010
05.24

A common task in web development is to track or show a users online time.

Theory

We record and save the time (in seconds) of the users first interaction with our website. On every following click we subtract this information with the current time.

Working example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* tell php to start a new or resume an existing session */
session_start();
 
/* store time of first user interaction */
if(!isset($_SESSION['session.started'])) {
 $_SESSION['session.started'] = time();
}
 
/* calculate time elapsed since first click */
$onlineTime = time() - $_SESSION['session.started'];
 
/* display online time in various formats */
print 'Seconds: '.$onlineTime;
print "<br />\n".'Minutes:'.floor(($onlineTime / 60));
print "<br />\n".'Hours:'.floor(($onlineTime / 60 / 60));
print "<br />\n".'Days:'.floor(($onlineTime / 60 / 60 / 24));

  PHP - Track user online time (651 bytes, 48 hits)

Enjoy!

2010
05.17
1
2
print URL::site(Request::instance()->uri(), TRUE); // http://www.example.com/kohana/info/index
print URL::site(Request::instance()->uri()); // /kohana/info/index

2010
04.28

“Allow mock locations” – means to be able to fake/simulate the location you’re at for development purposes

2010
04.12

Backups are crucial!
Since that is fact I wrote a little script in Python to easily backup mysql databases on a linux server.

  Python Mysqldump Backup 1.1 (858 bytes, 111 hits)


How do i configure it?
After you downloaded the script to your Linux server you will need a few Linux packages. Execute the following commands as root:

apt-get update
apt-get install python mysqldump bzip2

What does it do?
This script dumps a certain database (or all databases) into a single mysql file, which is zipped (bzip2) afterwards.

Now edit the script with your preferred editor.

vim mysqldump-backup.py

Customize the following lines:

# 2. Config
mysqlUser = “root”
mysqlPwd = “YOUR_PASSWORD”
mysqlDatabase = “–all-databases”
dumpDir = “/tmp/”
dumpFile = “mysqldump-” + strftime(“%Y-%m-%d-%H-%M-%S”, gmtime()) + “.sql”

The last step is to make the script executable:

chmod u +x mysqldump-backup-1.1.py

Thats it!

You can now run the script:

/path/to/the/script/mysqldump-backup-1.1.py

To run it daily (crontab):

crontab -e

Add at the bottom:

0 3 * * * /path/to/the/script/mysqldump-backup.py

Now your databases will be backupped daily at 03:00 am

Download

  Python Mysqldump Backup 1.1 (858 bytes, 111 hits)

Changelog
1.1

  • Removed trailing semicolons
  • Renamed variables

1.0

  • Initial release

Have fun!
Andreas Glaser aka JaZz

2010
03.26

I just came across a problem with “Drupal” related to my server configuration.
I tried to set the user permissions and Drupal would tell me “Validation error, please try again. If this error persists, please contact the site administrator.”.

This error was caused be the php extension “suhosin” which limits the maximal length of post requests.

To fix that open /etc/php5/apache2/conf.d/suhosin.ini and increase the value of suhosin.request.max_value_length to your needs.

2010
03.24

This function retrieves vocabulary names by their id.

getVocabularyNameByVID($vid)

1
2
3
4
5
6
7
8
9
10
11
function getVocabularyNameByVID($vid) {
  /* make sure vid is numeric */
  if(!is_numeric($vid)) return false;
 
  /* fetch name */
  $result = db_query("SELECT name FROM {vocabulary} WHERE vid = %d", $vid);
  $name = db_fetch_object($result)-&gt;name;
 
  /* return name or false if not found */
  return $name ? $name : false;
}

Usage

1
print getVocabularyNameByVID(1);

2010
03.22

PHP as well as most (all?) other programming languages provide different ways to comment source code.
Most common comment indicators are # , // and /* */ .

1
2
3
4
5
6
# this is a comment
echo 'print something';
// another comment
phpinfo();
/* and one more */
var_dump($_ENV);

Which one should I use?

# vs //

I definitely tend to # since it’s only one character instead of two slashes.

# and // vs. /* */

If you plan to pack your code I would highly recommend to always use /* */ due to it’s source code packer compatibility.

/* */ also provides multi line commenting.

1
2
3
4
5
/**
* this
* is a
* valid comment
**/

So long,
JaZz

2009
12.14

Well, “perfect” might be a little exaggerated although I consider this a good configuration at the moment.
You can put this globally into your httpd.conf or separately in every vhost configuration file you need it.

1
2
3
4
5
6
7
8
9
10
 
        ExpiresActive On
        ExpiresByType text/css "access plus 1 month"
        ExpiresByType text/javascript "access plus 1 month"
        ExpiresByType application/x-javascript "access plus 1 month"
        ExpiresByType application/javascript "access plus 1 month"
        ExpiresByType image/png "access plus 1 year"
        ExpiresByType image/jpeg "access plus 1 year"
        ExpiresByType image/gif "access plus 1 year"
        ExpiresByType image/x-icon "access plus 1 month"

Make sure you versionize your JavaScript/CSS to be able to force an update, in case you change them.
Example:

1
2
3
4
5
6
 
    My Example
 
Some Content
 
    <script src="javascript.js?version=1" type="text/javascript"><!--mce:0--></script>

You should also give newly updated pictures new names!

Over and out,
JaZz