PHP Rating Demo

The basic idea of this rating script is to count the number of votes and the ratings and store them in a text file (a database could also be used).

The first step was to create some images to represent the ratings. The easiest approach was to use ImageMagick to create colorful asterisks.

Next, the javascript was written. There was one minor browser compatibility issue with IE, the margin was not included in the offset. A simple fix was to use PHP to synchronize the left margin in the style tag and to add it in later in the javascript, if it was IE.

Finally, the PHP was written and tested. It is listed below.

rating.php5

$sRatingFile='rating.txt';
if (is_file($sRatingFile))
        $sRating=file_get_contents($sRatingFile);
else
        $sRating="0,0";

$aRating=explode(',',$sRating);

if (isset($_GET['r']))
{
        $r=(int)$_GET['r'];
        if (($r<0)||($r>5)) die('Bad rating');
        else
        {
                $aRating[0]++;
                $aRating[1]+=$r;
                file_put_contents($sRatingFile,implode(',',$aRating));
        }
}
$v=$aRating[0];
if ($aRating[0]!=0)
        $r=(int)($aRating[1]/$aRating[0]);
else
        $r=0;

echo<<<INPUT
<p>
<label>Rating</label>
<input name="iRating" id="iRating" type="image" src="images/$r.rating.png" alt="Rating" onclick="ratingClick(event)" />
$v votes
</p>
<div style="display:none">
<img src="images/$r.rating.png" id="imgRating" />
</div>
INPUT;

In a production environment, some CAPTCHA or other tool should be used to prevent automated submissions.

Web Application Help

Considerations for help systems for web applications.

  • User count If there aren’t many users, a simple text file should suffice. The next step up could be a word processing document or PDF. HTML is probably too cumbersome and costly for a small user base, unless it is expected to grow.
  • System complexity Simple systems, or those with excellent user interfaces need less documentation. Complex systems require more. Use the appropriate references to assist the user not only in using the application but working well in the system. For example, if they will be classifying information, provide links to materials that will help them enter the right data, not just valid data. This data should be separate, so it can be maintained by the appropriate people.
  • Target user type Internal users may be fine with a printed piece of paper. Other users may need a PDF, that they can search and print, and use familiar navigation. Potential clients should see the very finest help resources, because the content, navigation, presentation, and access to information may determine whether they purchase the product or not.
  • Features Should people be able to navigate between pages? Should they be able to search? Is a table of contents or index necessary? Type size should be considered, as well as whether images are important.
  • Images Screenshots should probably be avoided, since they may change and that requires documentation updates.
  • Author/Editor/Producer/Maintainer The person that produces the document should have the requisite skills. If it is an HTML document, the editor must know how to write decent HTML, including any necessary features.
  • Production The amount of effort and cost should be considered.

I think an excellent solution is a PDF, for the following reasons:

  • It is a popular format and the reader is a trusted piece of software
  • It has search and zoom
  • You can have tables of contents and indexes that link into the document
  • They print well
  • You can protect them from copying
  • You can link into them with anchors - into pages and sections
  • They are cost-effective to maintain and simple to track

PHP5 .tgz Download Counter

To add a download counter, you can use a RewriteRule to route requests through a script to deliver the file and update the counter. You may also be able to use a header(’location:file.tgz’) call as well.

.htaccess

RewriteEngine On
RewriteRule ^(.*\.tgz)$ download.php5?path=$1 [L]

download.php5

<?php
// downloading a file
$filename = $_GET['path'];
if (is_file($filename))
{
// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
// browser must download file from server instead of cache

// force download dialog
header("Content-Type: application/x-tgz");

// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");

/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

@readfile($filename);
$sCountFile=$filename.'.count.txt';
$iCount=file_get_contents($sCountFile);
file_put_contents($sCountFile,$iCount+1);
}
?>

Display Logic

<p><strong><?php include 'tri.tgz.count.txt'; ?></strong> downloads.</p>

This can be added to a site without changing the download URLs.

Many thanks to the PHP header link above, where the header code came from.

Simple Hit Counter Example

Hit counters are valuable because they allow site visitors to see how many people visited or used a server-based tool.

This is a very simple implementation of a counter. You can see it run at the link above.

The counter code is in the back-end/server-side logic.

PHP5 version

$iCount=file_get_contents('count.txt');
file_put_contents('count.txt',$iCount+1);
echo '<p><span style="font-weight:bolder;border:1px solid #000;padding:2px;">'.$iCount.' gradients created.</span></p>';

PHP4 version

$iCount=file_get_contents('tri.count.txt');
$fp=fopen('tri.count.txt','w+');
fwrite($fp,$iCount+1);
fclose($fp);
echo '<p>'.$iCount.' downloads</p>';

Multi-Lingual Site Configuration with Apache

Most browsers send the prefered language to servers when they access sites. This can be used to select the appropriate content, using Apache’s mod_negotiation.

Sample .htaccess file

Options +MultiViews
LanguagePriority fr en
AddLanguage en .en
AddLanguage fr .fr

Files

$ more req*
::::::::::::::
request.html.en
::::::::::::::
Hello
::::::::::::::
request.html.fr
::::::::::::::
Bon Jour

If no preference is given, the page will display Bon Jour, otherwise, English readers will get Hello, and French will receive Bon Jour.