Examples of php
From nswccWiki
Differentiating between a desktop, laptop browser and an iPod or iPhone Safari browser
In my recent work with php scripting, I’ve had the need to be able to automatically redirect web browsers based on whether they are “any desktop, laptop” or “iPod, iPhone”. The automatic redirection is relatively simple; what happens after that is not so simple. The iPod/iPhone experience uses a different user paradigm. It is very different than the “traditional desktop” model.
The php code to accomplish the re-direction is relatively straight forward. It does work, I tested it! Emphatically, because some of what you will find on the Web clearly indicates the code “has not been tested”. This php “code snippet” can of course also be used to conditionally execute code in a file but that is not recommended.
// -----------------------------------------------------------------------------------------------------
if ((stristr($_SERVER['HTTP_USER_AGENT'],'iPod')) || (stristr($_SERVER['HTTP_USER_AGENT'],'iPhone')))
header('Location: iPod.htm');
This line MUST be the first line to emit HTML in the file. With that restriction it will rapidly, redirect an iPod or iPhone web client to another page, called ‘iPod.htm’ located in the same folder as the file containing this line. This happens so fast it is not noticeable. The idea is to have this embedded in the ‘index.php’ default page so that iPod/iPhone users get a more friendly startup screen, and while you’re at it take advantage of the incredible web technology built into Safari 3.1.
Building a custom screen for iPod/iPhone from Cascading Style Sheets(CSS 3.0) elements
Already I see that pick lists and coloured areas of the screen, dressed as and to behave as buttons using the new CSS model will become very important. The pick list behaves very differently on the iPod/iPhone. It behaves like a “rolodex” taking over the whole screen. The self contained code is purely HTML/CSS. The <blueButton.png>MUST be a .png image and be 29 px x 46 px in a sub directory called images. But hey, experiment. It will actually be sliced, diced and expanded in an unusual way by the -webkit-border-image: url(images/blueButton.png) 0 7 0 7; instruction. Basically, it will be divided into a 3 x 3 grid. The corners/ middle side pieces will be exploded, and the centre, expanded to fit. So almost any generic shape can be made to look like a button. Nice!
Just as a word of warning. This will only work in Safari 3.1.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<!-- user directed to here from index.php if using an iPod or iPhone -->
<title>My School Teacher Companion</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="viewport" content="width=device-width, user-scalable=no" />
<style type="text/css">
* { margin: 0px;
padding: 0px;
}
#header
{
font-family: Georgia;
font-size: 12pt;
font-weight: bold;
font-style: italic;
font-stretch: expanded;
color: #0000af;
text-align: center;
}
#footer
{
font-family: Helvetica, sans-serif;
font-size: 7pt;
font-style: italic;
text-align: center;
}
body
{
font-family: Helvetica, sans-serif;
background-color: #fff;
-webkit-text-size-adjust: auto;
}
.button
{
display: block;
line-height: 30px;
width: 100px;
font-family: Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
color: #fff;
text-decoration: none;
text-align: center;
}
.blue
{
margin: 3px auto;
text-shadow: #000 0px 1px 1px;
border-width: 0px 7px 0px 7px;
-webkit-border-image: url(images/blueButton.png) 0 7 0 7;
}
</style>
<!-- --------------------------------------------------------------------- -->
</head>
<body>
<table width = "100%">
<tr>
<td height="50" width="90%">
<div id=header>My High School <font color="#f00">i</font>SIS</div>
</td>
<td><a><img src="school_logo.gif" width="50" border="0"></a></td>
</tr>
</table>
<table width="100%" >
<td align="center">
<select name="action" onChange= "window.location=this.options[this.selectedIndex].value" >
<option value = "#" selected>Faculty </option>
<option value = "http://localhost/teachers/English/Faculty.html" > English</option>
<option value = "http://localhost/teachers/Maths/Faculty.html" > Maths</option>
<option value = "http://localhost/teachers/Science/Faculty.html" >Science</option>
<option value = "http://localhost/teachers/TAS/Faculty.html" > TAS</option>
<option value = "http://localhost/teachers/HSIE/Faculty.html" > HSIE</option>
<option value = "http://localhost/teachers/PE-H-PD/Faculty.html" > PE-H-PD</option>
<option value = "http://localhost/teachers/Vis-Arts/Faculty.html" > Visual Arts</option>
<option value = "http://localhost/teachers/Per-Arts/Faculty.html" > Perform Arts</option>
<option value = "http://localhost/teachers/Support/Faculty.html" > Support</option>
</select>
<BR><BR>
</td>
<td>
<a class="blue button" href="http://localhost/day.php" >Today</a>
</td>
<td>
<a class="blue button" href="http://localhost/month.php" >This month</a>
</td>
</tr>
<tr>
<td>
<a class="blue button" href="http://localhost/week.php" >This week</a>
</td>
<td>
<a class="blue button" href="http://localhost/month.php?cal=Assess" >Asess</a>
</td>
</tr>
</table>
<div id="footer" >
____________________<BR> iwp - Last Updated 2008-04-20
</div>
</body>
</html>
Did the Earth move for you?
Mashups will become very important for obtaining up to date information for students to work with. Here is an example of a PHP object module that obtains the most significant world (non personal!) seismological events over the last 7 days (in reverse chronological order- most recent first).
We download and reconfigure data from the US Geological Survey, extracting only significant information. The complete data file is downloaded and cached in the same folder as this file is located, so you can look at it. On a production site you might like to place cached files in one folder.
This example provides the data enclosed in an HTML table tag, allowing it to be easily used within a page. You can change the filter setting from 5.5 to something else but 5.5 provides a relatively short list, and leaves out minor tremors.
The data list is formatted for use in a side-bar column. The text format may need to be altered for a particular purpose.
For more on this topic check out | the Global Earthquake Explorer.
<?php
// Ian W. Parker, first worked on: 2008-04-26, last worked on: 2008-04-26
// accesses US geological survey(USGS) world earthquake data for last 7 days
// NOTE: peculiar field references due to splitting on comma. Comma is contained
// in date time field so we have to do some fixes! otherwise requires more parsing!
$shakeFile = 'weekShakes.txt'; // local cached filepath.name
$filter_level = 5.5; // if you would like more lower to say 4.0
$cache_ttl = 4 * 60 * 60; // cache is set at 4 hours
// -------------------------------------------------------------------------------
// check cached file to see if we need to update from internet
$file_mod_time = 0;
if ( file_exists($shakeFile)) $file_mod_time = filemtime($shakeFile);
// this will considerably speed up access for 99.9% of accesses
if ( mktime() > ($file_mod_time + $cache_ttl)) // cache has expired
{
// do not alter this reference
$ch = curl_init("http://earthquake.usgs.gov/eqcenter/catalogs/eqs7day-M1.txt");
$fp = fopen($shakeFile, "w");
curl_setopt($ch, CURLOPT_FILE, $fp); // associate file for storage
curl_setopt($ch, CURLOPT_HEADER, 0); // set TCP/IP header type
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // set an upper limit of 5 seconds
curl_exec($ch); // do it!
curl_close($ch); // close connection
fclose($fp); // close, flush buffers
}
// ------------------------------------------------------------------------------
if ($fp = fopen($shakeFile, "r") )
{
$bigShake .= '<table width="100%" border="1" cellspacing="1" cellpadding="1">';
$bigShake .= '<caption>Earth Shakes > '.$filter_level.' for last 7 days <BR>';
$bigShake .= '<a href="http://earthquake.usgs.gov/">US Geological Survey</a></caption>';
// this file is usually huge!, events > 5.5 is OK.
while ( !feof($fp) )
{
$line = fgets($fp); $target = explode(",",$line);
if ( $target[8] > $filter_level )
{
$target[11] = '<B>Locn:</B> '.trim($target[11],'\"').' ';
$target[3]= substr($target[3],2,3).', ';
// accumulate selected data
$bigShake .='<tr><td>';
$bigShake .= '<B>Date:</B> '.$target[3].$target[4].' ';
$bigShake .= '<B>Magn:</B> '.$target[8]. '<BR>';
$bigShake .= $target[11];
$bigShake .='</td></tr>';
}
}
// done with the file, close and return value
$bigShake .='</table>';
fclose($shakeFile);
}
else
{
$bigShake = "No Current Data";
}
// following HTML is for test harness purposes only!
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>index</title>
</head>
<body>
<?php echo $bigShake; ?>
</body>
</html>
