เปิดหน้าเว็บให้เหมาะกับ OS ของผู้ใช้
เปิดหน้าเว็บให้เหมาะกับ OS ของผู้ใช้
เรื่องมีอยู่ว่า ผมจะต้องทำเว็บที่นำเสนอ Software ให้กับผู้เข้าเยี่ยมชมเว็บไซต์. Software ที่ว่านี้มีทั้งที่ทำงานใน MS Window และ Linux หน้าเว็บก็จะมีอยู่ 2 page
1. page สำหรับนำเสนอ Software ที่ใช้ใน MS Window
2. page สำหรับนำเสนอ Software ที่ใช้ใน Linux
เงื่อนไขก็คือ
ถ้าผู้ใช้เค้าใช้ MS Window เปิดเข้ามาหน้าเว็บสินค้าของเรา เราก็จะแสดง Page นำเสนอ Software ที่ใช้งานบน MS Window
ถ้าผู้ใช้เค้าใช้ Linux เปิดเข้ามาหน้าเว็บสินค้าของเรา เราก็จะแสดง Page นำเสนอ Software ที่ใช้งานบน Linux
แต่ทั้ง 2 page ก็จะมี link ไปหากันได้
http://www.geekpedia.com/code47_Detect-operating-system-from-user-agent-string.html
$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista ' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
// You are using Windows Vista
echo "You are using ".$CurrOS;
จากผลลัพธ์ที่ได้จาก Source Code ข้างบนนั้น เราจะเห็นได้ว่า Windows นั้นมีหลาย Version เหลือเกิน เราก็อาจจะเช็คแบบเหมารวมไปเลยก็ได้ โดยใช้ Function substr
if( substr($CurrOS) == 'Windows'){
// แสดงหน้าสินค้าสำหรับ MS Window
}elseif( $CurrOS == 'Linux'){
// แสดงหน้าสินค้าสำหรับ Linux
}else{
// กรณีที่ผู้ใช้ ไม่ได้ใช้ทั้ง Linux และ MS Windows เราก็มาเลือกเองว่าจะแสดงข้อมูลอะไรให้ลูกค้าดี
}
หลัการทำงานทั้งหมดก็เป็นแบบง่าย ๆ ใช้ตัวแปร Server : $_SERVER['HTTP_USER_AGENT'] เก็บข้อมูล แล้วนำข้อมูลที่ได้มาเปรียบเทียบกับข้อมูลที่เรามี เพื่อที่จะได้แสดงผลลัพธ์ที่เราต้องการได้


เว็บฝากไพล์ http://www.upload.tc