PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

[back]WB01624_.gif (281 bytes) 12/23/99 "Using Cookies & File Appending" WB01626_.gif (272 bytes)[next]

Hosts That Support PHP

I am going to be starting a list of known PHP enabled hosts as a reference so people can see if their host supports PHP 3, so look for that soon as I have started assembling a list.

The way the cookies crumble...

Yesterday we looked at forms, now in continuing with the rate.php3 script we are working on, we need a way to stop the same people from voting repeatedly. One way to do this is to use cookies. Cookies in PHP are stored in the environmental variable HTTP_COOKIE_VARS array. The names of the set cookies will be the actual variable names, not the HTTP_COOKIE_VARS itself, since that is an array. The cookies are stored in name-value pairs. From the php manual this defines the function setcookie:

int setcookie(string name, string value, int expire, string path, string domain, int secure);

Something to remember about setting cookies. You can't have an <HTML> tag before setting any cookies, so set your cookies before escaping PHP to set the <HTML> tags and printing anything to the browser. That is a limitation from browsers and an easy mistake to make. See the code below to see how simple it would be to check for a cookie stored as the unique $filename (see 12229):

<?
$check = "test";
$check .= $filename;
if ($test == $check)
{
print("<HTML><BODY>You have already voted. Thank you.</BODY></HTML>");
}
else
{
$rated = "test";
$rated .= $filename;
setcookie(test, $rated, time()+86400);
print("<HTML><BODY><br>You haven't voted before so I recorded your vote</BODY></HTML>");
}
?>

Example #9: Checking for and setting a cookie

I set a limit to one valid vote per 24 hours (time + 86,400 seconds), so it prevents people from voting any single diary entry repeatedly. This is kind of an interesting situation because remember that each page is a different file, so I have to be careful not to use one unique variable for all pages when setting the cookie or else someone who legitimately tried to rate more than one page would have their vote ignored.

File Writing Permissions

In order to allow the script to read and write to files inside the directory I need to set the DIRECTORY permissions to 777. I have done that and now I have the complete code below for my first truly useful PHP script:

<?
$security_domain = "www.php-scripts.com";
$security_domain2= "php-scripts.com";
$url = explode("/", $HTTP_REFERER);
if(ereg("$security_domain|$security_domain2", $url[2]))
{
  $filesplit = explode(".", $url[4]);
  $filename = $filesplit[0];
    if(ereg("[0-9]{6}", $filename))
    {
      $check = "rated";
      $check .= $filename;
        if ($rated == $check)
         {
          print("<HTML><BODY>You have already voted. Thank you.</BODY></HTML>");
         }
         else
         {
         $rated = "rated";
         $rated .= $filename;
         setcookie(rated, $rated, time()+86400);
         $filename .= ".dat";
         // time to open the unique file for appending
         $newrate = fopen($filename, "a");
           if ($newrate)
           {
             fputs($newrate, "$rating\n");
             fclose($newrate);
             print("<HTML><BODY><p>Filename chosen for diary page is $filename");
             print("<p>Thanks for rating my page: $HTTP_REFERER ");
             print("<br>with a rating of: $rating</BODY></HTML>");
           }
         }
     }
     else
    {
    print("<HTML><BODY><p><strong>Sorry this is not a valid diary");

    print("page</strong></BODY></HTML>");
    }
  }
  else
  {
  print("<HTML><BODY><p><strong>Sorry, you cannot run this");
  print(" script from this location</strong></BODY></HTML>");
}
?>

Some notes: as mentioned yesterday I added the $security_domain2 to make sure that whether the "www" is used or not, the rating will still be allowed. The logical OR symbol is the pipe symbol | inside the ereg function. The fopen function is used to open a file for writing "w" reading "r" or appending "a". If the file does not already exist, it will be created.  The filehandle is used by the fputs function to actually add the new $rating and a new line \n to the end of the file. Finally we use fclose to close the open file. To use this file I just need to create a form (which I have done below). Now you can rate how useful this diary entry (and all future diary entries) are.

How useful did you find this diary entry? Avg Surfer Rating: 4.87 (1216)

[back]WB01624_.gif (281 bytes) 12/23/99 "Using Cookies & File Appending" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 02/9/04 11:29