<?php echo $_SERVER["HTTP_USER_AGENT"]; ?>
该脚本的输出可能是:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
PHP 有很多种不同类型的变量,在以上例子中我们打印了一个数组的元素,数组是一类非常有用的变量.
$_SERVER 只是 PHP 自动全局化的变量之一,您可以查阅“保留变量”一节来查看这些变量的列表,或者也可以建立如下的文件来得到一个完整的列表.
php 根据HTTP_USER_AGENT 判断用户浏览器类型,代码如下:
- function browsers(){
- global $HTTP_USER_AGENT ;
- if (isset($HTTP_USER_AGENT)){
- $sAgent = $HTTP_USER_AGENT;
- }else{
- $sAgent = $_SERVER['HTTP_USER_AGENT'];
- }
- if (strpos($sAgent,'MSIE') !== false && strpos($sAgent,'mac') === false && strpos($sAgent,'Opera') === false){
- $iVersion = (float)substr($sAgent,strpos($sAgent,'MSIE') + 5,3);
- return ($iVersion >= 5.5) ;
- }else if (strpos($sAgent,'Gecko/') !== false){
- $iVersion = (int)substr($sAgent,strpos($sAgent,'Gecko/') + 6,8);
- return ($iVersion >= 20030210) ;
- }else{
- return false;
- }
- }
可以得到链接/提交当前页的父页面URL,代码如下:
- <?php
- if (isset($_SERVER['HTTP_REFERER'])) {
- print "The page you were on previously was {$_SERVER['HTTP_REFERER']}<br />";
- } else {
- print "You didn't click any links to get here<br />";
- }
- ?>
- <a href="refer.php">Click me!</a>
|