Archive

Tag Archives: php

It’s quite often for a developer to make use of the user agent to determine what browser does a user used, especially for those who develop web services and websites. Recently I found a website which has a huge database of mobile device information, including the user agent string and even the functions supported in the device browser.

ScientiaMobile WURFL Explorer
http://www.tera-wurfl.com/explore/search.php?action=browse

Tera-WURFL Explorer – Samsung GT-9100 (Galaxy SII)
http://www.tera-wurfl.com/explore/?action=wurfl_id&id=samsung_gt_i9100_ver1

To begin with, we need to understand that in the HTTP protocol, browser send its identity called user agent to the server to request the wanted webpage. Every browser has its only unique user agent value, and therefore we can check that value to identify the user browser. So, first we have to take a look at some examples of user agents of mobile devices.

iPhone user agent

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3

iPod Touch user agent

Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A101a Safari/419.3

iPad user agent

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

Android user agent

Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2
Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

BlackBerry user agent

BlackBerry9000/4.6.0.266 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/120

After all, in programming, we gather these data to do the checking. First in JavaScript:

if (/(iPhone|iPod|iPad)/.test(navigator.userAgent)) {
    /* This is iOS */
}
if (/Android/.test(navigator.userAgent)) {
    /* This is Android */
}
if (/BlackBerry)/.test(navigator.userAgent)) {
    /* This is BlackBerry */
}
if (/(iPhone|iPod|iPad|BlackBerry|Android)/.test(navigator.userAgent)) {
    /* This is one of the mentioned mobile device browsers */
}

And this is how it works in PHP:

if (preg_match('/iPhone|iPod|iPad/', $_SERVER['HTTP_USER_AGENT'])) {
    /* This is iOS */
}
if (preg_match('/Android/', $_SERVER['HTTP_USER_AGENT'])) {
    /* This is Android */
}
if (preg_match('/BlackBerry/', $_SERVER['HTTP_USER_AGENT'])) {
    /* This is BlackBerry */
}
if (preg_match('/iPhone|iPod|iPad|BlackBerry|Android/', $_SERVER['HTTP_USER_AGENT'])) {
    /* This is one of the mentioned mobile device browsers */
}

Fix Mbox Mail for Mac with Apple Mail 5, please see this post:
Fix Mbox Mail for Mac on Lion, works Apple Mail 5

After the new Mac OSX Lion released, there has been a number of people finding out the Mbox Mail for Mac doesn’t work at all. But the fact is, it actually works. The reason is that the whole new updated Apple Mail 5 has added some security protocols which don’t support the Mbox Mail for Mac. I have been doing some programs hoping to fix this problem. However it takes some time. Therefore herein I would like to introduce an alternative way with Thurderbird to make the whole thing works again.

Step 1:
If you don’t have Mbox Mail for Mac, download and install Mbox Mail for Mac 1.0.2 below and ensure it is not expired and started:
http://fluentfactory.com/mboxmail-for-mac/download.php

Step 2:
Download and install the beta version of Thurderbird here:
http://www.mozillamessaging.com/en-US/thunderbird/early_releases/downloads/

Step 3:
Go to “Tools”->”Account Settings”, then add new mail account:

Step 4:
Enter name and email address, but leave password empty. Then continue.

Step 5:
Right after continue, you will see “manual config”. Click it.

Step 6:
Input password and fill the boxes as shown in the screenshot. Then click “re-test” and then “create account”.

Step 7:
You might see a warning. Tick the understand box and then create account.

Finally:
After all you should be able to check your hotmail. Enjoy and please “like” or “+1” this passage if you feel it helpful. =]

$url = "http://j-jis.com/";
$html = file_get_contents($url);
$html = preg_replace("/rn|n|t/", "", $html); //remove unwanted characters
$html = iconv("Shift_JIS","UTF-8",$html); //convert encoding
echo $html;

Usually while we extract information from other websites, character encoding might not correct. For example, j-jis.com is encoded with Shift-JIS. Therefore if your MySQL charset is UTF-8, it causes incorrect information. To handle the changing of character encoding, we can use iconv in PHP.

$url = "http://j-jis.com/";
$html = file_get_contents($url);
$html = preg_replace("/rn|n|t/", "", $html); //remove unwanted characters
$html = iconv("Shift_JIS","UTF-8",$html); //convert encoding
echo $html;
$string = '你好嗎';
echo json_encode($string); //Output "u4f60u597du55ce"
echo preg_replace("#u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '1'))", json_encode($string)); // Output "你好嗎"

Json has been an efficient way to handle information and message exchanges in web programming. For example, I usually use PHP to connect MySQL and retrieve information, then display as Json. So a webpage can “AJAX” the displayed Json to create a dynamic view on itself. However, while the information involves unicode characters, PHP turn them into unreadable codes. After google, I found the following is the best way to solve the problem.

$string = '你好嗎';
echo json_encode($string); //Output "u4f60u597du55ce"
echo preg_replace("#u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '1'))", json_encode($string)); // Output "你好嗎"
%d bloggers like this: