This site uses cookies from Google to deliver its services, to personalize ads and to analyze traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. Learn More

[PHP] Displaying Browser Specific-php

However, having seen some of the possible values of HTTP_USER_AGENT in the last chapter,
you can imagine that there are hundreds of slightly different values. So it's time to learn
some basic pattern matching.

You'll use the preg_match() function to perform this task. This function needs two arguments:
what you're looking for, and where you're looking:

preg_match("/[what you're looking for]/", "[where you're looking]");

This function will return a value of true or false, which you can use in an if…else block
to do whatever you want. The goal of the first script is to determine if a Web browser is
Microsoft Internet Explorer, Netscape, or something else. This can be a little tricky,
but not because of PHP.

Within the value of HTTP_USER_AGENT, Netscape always uses the string Mozilla to identify
itself. Unfortunately, the value of HTTP_USER_AGENT for Microsoft Internet Explorer also
uses Mozilla to show that it's compatible. Luckily, it also uses the string MSIE, so you
can search for that. If the value of HTTP_USER_AGENT doesn't contain either Mozilla or MSIE,
chances are very good that it's not one of those Web browsers.

Open a new file in your text editor and start a PHP block, then use getenv() to place the
value of HTTP_USER_AGENT in a variable called $agent:

<?
$agent = getenv("HTTP_USER_AGENT");

Start an if…else statement to find which of the preg_match() functions is true, starting
with the search for MSIE:

if (preg_match("/MSIE/i", "$agent")) {
$result = "You are using Microsoft Internet Explorer.";
}


Continue the statement, testing for Mozilla:

else if (preg_match("/Mozilla/i", "$agent")) {
$result = "You are using Netscape.";
}

Finish the statement by defining a default:

else {
$result = "You are using $agent";
}

source : http://seo-tips-tech.blogspot.com, http://detik.com, http://tribunnews.com

0 Response to "[PHP] Displaying Browser Specific-php "

Post a Comment

Contact

Name

Email *

Message *