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";
}
Related Posts:
  • Including and Requiring PHP Files To make your code more readable, you can place your functions in a separate file. Many PHP add-ons that you download off the Internet contain functions already placed into files that you simply include in your PHP program… Read More
  • Creating Arrays PHP provides the array( ) language construct that creates arrays. The following examples show how arrays of integers and strings can be constructed and assigned to variables for later use: $numbers = array(5, 4, 3, 2, 1);… Read More
  • Php Mysql Image upload <?php // 1. Gem modtagne formulardata i variabler: $navn = $_POST['navn']; $alder = $_POST['alder']; $postnr = $_POST['postnr']; $mail = $_POST['mail']; $billede = $_FILES['profilbillede']; $password = $_PO… Read More
  • PHP Data Types PHP provides four primitive data types: integers, floating point numbers, strings, and booleans. In addition, there are two compound data types: arrays and objects.  Integers Integers are whole numbers. The range… Read More
  • PHP MySQL Functions mysql_field_len — Returns the length of the specified field mysql_field_name — Get the name of the specified field in a result mysql_field_seek — Set result pointer to a specified field offset mysql_field_table — Get … Read More
  • Showing the Browser and IP Address Here is a simple page that prints out the browser string and the IP address of the HTTP request. Create a file with the following content in your web directory, name it something like example.php3, and load it in your bro… Read More
  • PHP - Echo <?php $myiString = "Hi!"; echo $myiString; echo "<h5>I love PHP!</h5>"; ?>   Display: Hi! I love  PHP!  A simple form example     1 <html> 2 <head> 3 <title&g… Read More
  • File Manipulation 11.3. File Manipulation There may be times when you don't want to store information in a database and may want to work directly with a file instead. An example is a logfile that tracks when your application can't co… Read More
  • Php Directory Functions chdir — Change directory chroot — Change the root directory closedir — Close directory handle dir — Return an instance of the Directory class getcwd — Gets the current working directory opendir — Open directory handle read… Read More
  • PHP Zip File Functions zip_close — Close a ZIP file archive zip_entry_close — Close a directory entry zip_entry_compressedsize — Retrieve the compressed size of a directory entry zip_entry_compressionmethod — Retrieve the compression meth… Read More
  • PHP Functions A function is a named sequence of code statements that can optionally accept parameters and return a value. A function call is an expression that has a value; its value is the returned value from the function. PHP provid… Read More
  • PHP Web-Related Variables PHP automatically creates variables for all the data it receives in an HTTP request. This can include GET data, POST data, cookie data, and environment variables. The variables are either in PHP's global symbol table or … Read More
  • Defining Functions There are already many functions built into PHP. However, you can define your own and organize your code into functions. To define your own functions, start out with the function statement: function some_function([argumen… Read More
  • Length of a String The length property of a string is determined with the strlen( ) function, which returns the number of eight-bit characters in the subject string: integer strlen(string subject) We used strlen( ) earlier in the chapter t… Read More
  • PHP Configuration Directives Although the focus of this book is application security, there are a few configuration directives with which any security-conscious developer should be familiar. The configuration of PHP can affect the behavior of the cod… Read More