此会话演示了PHP如何根据浏览器类型,随机生成的数字或用户输入提供动态内容.它还演示了如何重定向客户端浏览器.
PHP创建了一些有用的环境变量,可以在用于设置PHP环境的phpinfo.php页面中看到.
PHP设置的一个环境变量是 HTTP_USER_AGENT ,用于标识用户的浏览器和操作系统.
PHP提供了一个函数getenv()to访问所有环境变量的值. HTTP_USER_AGENT环境变量中包含的信息可用于创建适合浏览器的动态内容.
以下示例演示了如何识别客户端浏览器和操作系统.
注意 : 函数preg_match()在 PHP正则表达式会话中讨论.
<html> <body> <?php function getBrowser() { $u_agent = $_SERVER['HTTP_USER_AGENT']; $bname = 'Unknown'; $platform = 'Unknown'; $version = ""; //First get the platform? if (preg_match('/linux/i', $u_agent)) { $platform = 'linux'; }elseif (preg_match('/macintosh|mac os x/i', $u_agent)) { $platform = 'mac'; }elseif (preg_match('/windows|win32/i', $u_agent)) { $platform = 'windows'; } // Next get the name of the useragent yes seperately and for good reason if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) { $bname = 'Internet Explorer'; $ub = "MSIE"; } elseif(preg_match('/Firefox/i',$u_agent)) { $bname = 'Mozilla Firefox'; $ub = "Firefox"; } elseif(preg_match('/Chrome/i',$u_agent)) { $bname = 'Google Chrome'; $ub = "Chrome"; }elseif(preg_match('/Safari/i',$u_agent)) { $bname = 'Apple Safari'; $ub = "Safari"; }elseif(preg_match('/Opera/i',$u_agent)) { $bname = 'Opera'; $ub = "Opera"; }elseif(preg_match('/Netscape/i',$u_agent)) { $bname = 'Netscape'; $ub = "Netscape"; } // finally get the correct version number $known = array('Version', $ub, 'other'); $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; if (!preg_match_all($pattern, $u_agent, $matches)) { // we have no matching number just continue } // see how many we have $i = count($matches['browser']); if ($i != 1) { //we will have two since we are not using 'other' argument yet //see if version is before or after the name if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){ $version= $matches['version'][0]; }else { $version= $matches['version'][1]; } }else { $version= $matches['version'][0]; } // check if we have a number if ($version == null || $version == "") {$version = "?";} return array( 'userAgent' => $u_agent, 'name' => $bname, 'version' => $version, 'platform' => $platform, 'pattern' => $pattern ); } // now try it $ua = getBrowser(); $yourbrowser = "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent']; print_r($yourbrowser); ?> </body> </html>
这会在我的机器上产生以下结果.根据您使用的内容,此结果可能与您的计算机不同.
它将产生以下结果 :
Your browser: Google Chrome 54.0.2840.99 on windows reports: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
PHP rand()函数是用于生成随机数.i此函数可以生成给定范围内的数字.应该对随机数生成器进行种子处理,以防止生成规则的数字模式.这是使用 srand()函数实现的,该函数指定种子编号作为参数.
以下示例演示了如何在每次四个中显示不同的图像图像 :
<html> <body> <?php srand( microtime() * 1000000 ); $num = rand( 1, 4 ); switch( $num ) { case 1: $image_file = "/php/images/logo.png"; break; case 2: $image_file = "/php/images/php.jpg"; break; case 3: $image_file = "/php/images/logo.png"; break; case 4: $image_file = "/php/images/php.jpg"; break; } echo "Random Image : <img src=$image_file />"; ?> </body> </html>
它将产生以下结果 :
处理HTML表单和PHP时最重要的事情是任何表单元素PHP页面将自动提供HTML页面.
将源代码放在test.php脚本中,尝试以下示例.
<?php if( $_POST["name"] || $_POST["age"] ) { if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) { die ("invalid name and name should be alpha"); } echo "Welcome ". $_POST['name']. "<br />"; echo "You are ". $_POST['age']. " years old."; exit(); } ?> <html> <body> <form action = "<?php $_PHP_SELF ?>" method = "POST"> Name: <input type = "text" name = "name" /> Age: <input type = "text" name = "age" /> <input type = "submit" /> </form> </body> </html>
它会产生以下结果 :
PHP默认变量 $ _ PHP_SELF 用于PHP脚本名称当你点击"提交"按钮时,将调用相同的PHP脚本,并产生以下结果 :
方法="POST"用于发布用户数据到服务器脚本.将数据发布到服务器脚本有两种方法,这些方法在 PHP GET& POST 章节.
PHP 标题( )功能将原始HTTP标头提供给浏览器,并可用于将其重定向到另一个位置.重定向脚本应位于页面的最顶部,以防止加载页面的任何其他部分.
目标由位置:标题指定作为 header()函数的参数.调用此函数后, exit()函数可用于暂停其余代码的解析.
以下示例演示了如何将浏览器请求重定向到另一个网页.通过将源代码放在test.php脚本中来试用这个例子.
<?php if( $_POST["location"] ) { $location = $_POST["location"]; header( "Location:$location" ); exit(); } ?> <html> <body> <p>Choose a site to visit :</p> <form action = "<?php $_SERVER['PHP_SELF'] ?>" method ="POST"> <select name = "location">. <option value = "http://www.IT屋.com"> Tutorialspoint.com </option> <option value = "http://www.google.com"> Google Search Page </option> </select> <input type = "submit" /> </form> </body> </html>
它将产生以下结果 :