Dealing with UTF-8 text using the PHP mail()-function

On one of my PHP-based websites, users can send mails via a simple text form. I had a problem with special characters in the subject or message body. The webpage has a <meta charset=”utf-8″ />-tag, which causes problems when people enter special characters, like é or € or “smart quotes”. They get sent via the mail()-function and show up like Ã³ or Ã© or â€™.

Deep down in the user comments in the documentation on the mail()-function, I found a small function that solved this easily:

function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') {
 $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
 $result = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header);
 return $result;
}

Just use mail_utf8() instead of mail() on a UTF-8 page and a proper UTF-8 mail will be sent out.

Windows 7 + IIS 7.5 + php 5.3.1 + MySQL 5.1: no good combination

I recently installed Windows 7 on my laptop. I also configured IIS 7.5 on it, installed php 5.3.1 and MySQL 5.1.

Everything seemed to be installed correctly: I could see the IIS 7 Welcome screen, I could look at the php configuration with phpinfo() and I could see the MySQL tables using the MySQL Administrator GUI.

But when I tried to use phpMyAdmin, I got stuck: after trying to load the homepage of phpMyAdmin for 20-30 seconds, I got an HTTP Error 500. I searched the internet for hours, trying to find somebody with a similar experience, but I did not find any valuable resources.

Even a very simple test-script like

<?php
echo "#1. Try to connect to mysql server...";
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>

generated a HTTP 500-error, after a 20-30 seconds wait.

Eventually, I decided to look at what I had installed when I was still using Vista. The difference seemed to be php: on my previous system, php 5.2.10 was installed.

I finally uninstalled MySQL and php 5.3.1, and then reinstalled php 5.2.11 and MySQL 5.1.41. That combination seemed to work without issues.

Conclusion: IIS 7.5 + php 5.3.1 + MySQL 5.1 does not seem to be a workable solution (for now). Stick to the 5.2-branch of php if you want to use it on Windows 7/IIS 7.5.

Update on How to implement Facebook Connect on a PHP website

A few weeks ago, I blogged about my first tests with Facebook Connect. Since then, Facebook has made it a bit easier by introducing the Facebook Query Language, or FQL.

FQL allows you to use a SQL-style interface to more easily query the Facebook social data. As an example, you can use statements like

SELECT name, pic FROM user WHERE uid=211031 OR uid=4801660

to get the name and picture of specific users.

Details about FQL can be found on the Facebook Developers website. To implement it in PHP, you use the following code:

$fbid = $facebook->user;
echo $fbid;
if ( $fbid )
  {
  $query = sprintf("SELECT name, pic_square_with_logo FROM user WHERE uid = %s",$fbid);
  $user_details = $facebook->api_client->fql_query($query);
  }

...

<? if ( $user_details[0]["pic_square_with_logo"] != ''
  { ?>
  <img src="<?=$user_details[0]["pic_square_with_logo"]?>" />
  <? } ?>

You can test FQL queries with the Facebook API test tool.