p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Help with Counter File


Message #1 by Gene Ingraham <n2bim@w...> on Thu, 27 Feb 2003 08:10:28 -0500
Hello to the group,

First, I'm new to this group and to PHP, and I could use a little help, 
if it's available.  I am an accountant by trade, and not an IT 
professional, nor am I a programmer, so please bear with me if my 
mistake is obvious.  Thanks!

I've been trying to add a new counter file to the home page of a site 
that I administer.

1 - I added a blank file called counter.dat to the directory.

2 - I added the following code to the index.html file and renamed the
     file to index.php:

            <p>
	   <?PHP include ("count.php?show=true");
	   ?>
            </P>

3 - I added a file called count.php with the following code:

      <?PHP
      $file = "counter.dat"; // counter file
      if(!($fp = fopen($file, "r"))) die ("Cannot open $file.");
      $count = (int) fread($fp, 30);
      fclose($fp);

      $count++;

      if ($show)
      {
      echo "You're visitor Number <b>$count</b>.";
      }

      $fp = fopen($file, "w");
      fwrite($fp, $count);
      fclose($fp);
      ?>

4 - I set the permissions on these files using CHMOD to Read/Write
     (but not Execute).  I suspect there may be a problem with that,
      but I'm not sure.

When I open the page, I get the following:

Warning: Failed opening 'count.php?show=true' for inclusion 
(include_path='.:/usr/local/lib/php') in 
/usr/local/www/virtual/scarcnj/www.scarcnj.org/index.php on line 162


The server that this site is running is using UNIX, with the latest PHP 
files.

Can anybody tell me where the problem is?

Thanks,

Gene Ingraham


Message #2 by "Pedro Graca" <bzzzt@f...> on Thu, 27 Feb 2003 13:18:10 +0000
On Thu, 27 Feb 2003 08:10:28 -0500, Gene Ingraham said:
[...]
>             <p>
> 	   <?PHP include ("count.php?show=true");
> 	   ?>
>             </P>
[...]
> When I open the page, I get the following:
> 
> Warning: Failed opening 'count.php?show=true' for inclusion 
> (include_path='.:/usr/local/lib/php') in 
> /usr/local/www/virtual/scarcnj/www.scarcnj.org/index.php on line 162

hmmmm ... I'm not sure, but try this instead

<?php
  $show = true;
  include "count.php";
?>
-- 
                                            ()    ribbon campaign     ()
                                            /\   against HTML mail    /\
Message #3 by "Nikolai Devereaux" <yomama@u...> on Thu, 27 Feb 2003 10:49:40 -0800
> > <?PHP include ("count.php?show=true");
>
> hmmmm ... I'm not sure, but try this instead
>
> <?php
>   $show = true;
>   include "count.php";
> ?>


Pedro's correct.  include() thinks that "count.php?show=true" is the
filename that you're trying to open.  It's an invalid filename, and the
filesystem would complain that it couldn't open the file.

Pedro's solution works because include() basically inserts the included
file's contents in-place in the current document.  Therefore, any variables
that are in scope of the includING file are also in scope for the includED
file.


Take care,

Nik


  Return to Index