Problem with script counter.php

Hi Gandalf nice to her from you,as its arch linux command line linux distro i have to put the ip address of the linux web server and that works it bring up my webpage.

I assume the webpage is just HTML? No PHP? If you don’t have a localhost address, then do you have another way to access the LAMP control panel (assuming there is one. I’m using XAMPP on Windows).

yes just html, i am just starting my web page at the moment its just index.html, i have not seen a LAMP control panel. what i did i had an old PC so i install arch linux with LAMP, the install worked fine and everything was working ok the PHP was working when i opened from my windows pc
http://192.168.3.3/info.php the php webpage used to come up, but now it just show the text from info.php

managed to get php working via this link:-
https://wiki.archlinux.org/index.php/Apache_HTTP_Server

but the counter is still not working any ideas please

If it’s still not working in the same way (i.e. it’s displaying zero), add some debug echos into your code to see which of the file handler routines it’s going into. When you first create the counter, it will be zero because that’s what you write into the counter file. Is it creating the file correctly, and writing the counter into it?

Note that in your code, the $chars array will start at $chars[0] as arrays are base zero by default, so you need to use [0], [1], [2]. [3] and [4] to build the counter image.

Also note that in testing, it’s only going to increment the counter once, because you test for (and later set) a session variable so the same user only registers once. You could take out the bit where it sets the session, to see if that helps.

i changed the $chars[0] to start at zero. i am new to php i dont understand debug echos not sure whats going wrong

All I mean by that is to add echo statements so you can see each bit of code as it’s being executed. For example, in the start of your code:

<?php
session_start();
$debug = true; // Change this to false when you want to stop the extra echos
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
  if ($debug) echo "Counter file did not exist";    // ** DEBUG 
  $f = fopen($counter_name, "w");
  fwrite($f,"0");
  fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
if ($debug) echo "Counter value is " . $counterVal; // ** DEBUG
// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
  $_SESSION['hasVisited']="yes";
  $counterVal++;
  $f = fopen($counter_name, "w");
  fwrite($f, $counterVal);
  fclose($f); 
  if ($debug) echo "Incremented counter"; // ** DEBUG
}
...

So I’ve added the lines that end in “** DEBUG” as a comment, which will echo whichever ones work. Once you’ve figured out the problem, you can stop them outputting by changing the line at the top to set $debug to false instead of true.

2 Likes

Hey @taz1 I’m not sure where you are at now. I would make the following observations/suggestions:

  1. The code on the website should work as is without any editing.

  2. Make sure you read the tutorial as well as copying the code. I realise you are new to PHP but all the more reason to try to understand what the code is doing. That’s how we learn.

  3. Try the text version first before going on to the graphical version - there’s less code and less to go wrong!

  4. If you run into difficulties, add the debug statements as @droopsnoot suggests.

Enjoy and good luck. Let us know how you get on…

1 Like

i have added the debug not sure how to test the script as i am using arch linux command line i will test it on some online php test page. thanks

thanks guys i will go back to the text version first and let you know

this is what my webpage displays

this is the code in my webpage:-
`

Number of visitors to this page so far:
Hit counter `

and this is the counter.php:-
`

<?php session_start(); $debug = true; // Change this to false when you want to stop the extra echos $counter_name = "counter.txt"; // Check if a text file exists. If not create one and initialize it to zero. if (!file_exists($counter_name)) { if ($debug) echo "Counter file did not exist"; // ** DEBUG $f = fopen($counter_name, "w"); fwrite($f,"0"); fclose($f); } // Read the current value of our counter file $f = fopen($counter_name,"r"); $counterVal = fread($f, filesize($counter_name)); fclose($f); if ($debug) echo "Counter value is " . $counterVal; // ** DEBUG // Has visitor been counted in this session? // If not, increase counter value by one if(!isset($_SESSION['hasVisited'])){ $_SESSION['hasVisited']="yes"; $counterVal++; $f = fopen($counter_name, "w"); fwrite($f, $counterVal); fclose($f); if ($debug) echo "Incremented counter"; // ** DEBUG } ?>

`

I have created counter.txt with 0 typed inside, but its still not working, any suggestions please

The code in your web page

<img alt="Hit counter" src="counter.php" />

is running the counter.php as though it was the graphical version, so that’s probably why you’re not seeing anything.

Can you simply run the counter.php instead of your web page?

when run http://ipaddress/counter.php

the webpage says:-

Counter value is 0

does not look like he counter.txt increase, but the file counter.txt modified date and time does change, its just still says zero

I think this is progress!

That line would appear to be output by the debug line

if ($debug) echo "Counter value is " . $counterVal; // ** DEBUG

I suspect it has not been increased because the session variable is still set from the last attempt to run the script. A session can be closed by closing and restarting the browser, but as you’re not running from a browser I’m not sure how you close a session on your setup.

Perhaps you can try the graphical version now. I suggest saving your current counter.php as something else - say textcounter.php so you don’t lose what you have done so far.

Remember to read the tutorial as it mentioned resizing the images.

i am using a browser from my windows PC would that work
i closed my browser and deleted the history using cclean ran it again and the webpage says:-
Counter value is 0 Incremented counter

then when i hit refresh it says:-
Counter value is 0

any ideas do you think its the script?

No. I very much doubt it is the script. I followed the script myself a couple of days ago and it works fine for me.

I am a bit puzzled by the message you get when you run your web page saying it’s using an old P4 LAMP. This could be the problem, or it could be that you don’t have write permissions for the folder you’re working in.

the old P4 is just something i wrote to remind me. i have read write permission on the folder and file as shown below:-
-rwxrwxrwx 1 root root 2 Feb 8 16:33 counter.txt

and as you can see the file modified time, but the counter.txt still says zero inside

Hmm. Certainly seems as though there are problems writing the file. What happens if you delete the counter.txt file - does the script create it?

1 Like

Maybe the “b” binary?

1 Like