NetAdminTools.com
 
SignalQ Sites:
NetAdminTools - Coprolite - NoNIC - SpotBridge - NAW
RoboCoop - AreWeDown - SolarPower - SysAdminTools
Xfig - Gold Loaf - GeekPapa - FixGMC - MCJ - FixRambler
Categories:
GNU/Linux | Homebrew designs | Perl | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS | Hail Support


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·REALbasic
·All Categories


Programming With PHP - Part 2
Topic: Web   Posted:2003-02-11
Printer Friendly: Print

spacer
In Part 1 of this series, we set up a basic PHP server with Apache on a Red Hat system, and wrote a simple "Hello World" script. In this article, we will vary content based on browser type.

We will use the HTTP_SERVER_VARS server variable. The specific variable we want is HTTP_SERVER_VARS['HTTP_USER_AGENT']. To use this, let's create a small HTML/PHP doc that echoes back the variable:

[root@srv-33 website]# cat index.php
<html>
<head>
<title>HTTP_SERVER_VARS_USER_AGENT</title>
</head>
<body>
<?php
echo $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
?>
</body>
</html>
[root@srv-33 website]#

Notice how you can put normal HTML in a .php document. Just put the <?php and ?> tags around the PHP part.

Let's vary our content based on the platform the browser is running on. If the platform is Linux, then we will put a nice penguin graphic on the page. If not, we will say "You could have had a penguin!!.

Now, we are Perl fans, so we prefer the perl-style regular expressions. The preg_match does just that. For more info, look here. The i means to ignore case. Now, we have never seen a Linux user agent string that didn't have a capital L, but it is probably wise to ignore case anyway. We don't want to offend possible Linux users that want to see the penguin, and can't, because their user agent string has a lower case l. :) Of course, the string the browser passes can't really be relied on, but in most cases this should work from our experience glancing at http logs. The final html looks like this:

[root@srv-33 website]# cat index.php
<html>
<head>
<title>HTTP_SERVER_VARS_USER_AGENT</title>
</head>
<body>
<?php
$hv=$HTTP_SERVER_VARS['HTTP_USER_AGENT'];
if(preg_match ("/linux/i",$hv)){
echo "<img src=\"tux.png\">";
}
else {
echo "You could have had a penguin!!!";
}
?>
</body>
</html>
[root@srv-33 website]#





Please read our Terms of Use
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2008 NetAdminTools.com

Created by:
MCJ
MCJ CMS