Now let's write some more practical scripts. We will check the type of browser used by visitors who browse the page. To achieve this goal, we need to check the user's agent string, which is part of the HTTP request sent by the browser.
Now let's write some more practical scripts. We will check the type of browser used by visitors who browse the page. To achieve this goal, we need to check the user's agent string, which is part of the HTTP request sent by the browser. This information is stored in a variable. In PHP, variables always start with a dollar sign. The variable we are interested in now is $ _ SERVER ['http _ USER_AGENT '].
Note: $ _ SERVER is a special PHP reserved variable that contains all the information provided by the web server, known as active global variables (or "Super global variables "). For more information, see "active global variables" in this manual. These special variables are introduced in PHP 4.1.0. Before that, we applied the $ HTTP _ * _ VARS array, for example, $ HTTP_SERVER_VARS. Although they are no longer used, they still exist in the new version.
To display this variable, we only need to perform the following operations:
Example: print a variable (array element)
The output of this script may be:
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
PHP has many different types of variables. In the preceding example, an array element is printed. Arrays are a type of very useful variables.
$ _ SERVER is only one of the variables that PHP uses for proactive regionalization. You can view the list of these variables in the "keep variables" section, or create the following file to get a complete list:
Example. use phpinfo () to display all predefined variables
If you open this file in your browser, you will see the complete information about PHP and a list of all the variables that you can apply.
You can add multiple PHP statements in a php id, or create a code block to do more things than simple echo. For example, if we need to check Internet Explorer, we can perform the following control:
Example: structure control and function application
If (strstr ($ _ SERVER ['http _ USER_AGENT '], 'msi ')){
Echo You are using Internet Explorer
';
}
?>
The output of this script may be:
You are using Internet Explorer
Here we need to introduce some new principles. We use an if statement. If you are familiar with the basic syntax of the C language, you should be familiar with it. Otherwise, you may need to pick up any book that is compatible with PHP and browse the previous two or three chapters.
We need to first introduce the second principle, which is to call the strpos () function. Strpos () is a built-in function of PHP. its function is to search another string in one string. For example, we need to find 'msi' in the $ _ SERVER ['http _ USER_AGENT '] (the so-called haystack) variable '. If this string (the so-called needle) is found in this haystack, the function returns the position of needle in the relative beginning of the haystack; if not, FALSE is returned. If this function does not return FALSE, if judges the condition as TRUE and runs the code in braces {}. Otherwise, the code is not run. You can create similar scripts by using if, else, and other functions, such as strtoupper () and strlen. In this manual, the relevant page also contains a model.