Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ Help for simple PHP script

Posted by: denmarks Aug 26 2018, 03:50 PM

I know hardly anything about PHP and am trying to create a very simple script. It is the beginning of a web counter.
There is a file named count.log that has a single line in it with a zero (0).
I want to read the file and create a gif displaying the number.
One is then added to the count and it is written back to the log file.

The gif displays but I do not believe the the log is read or written. The value of 0 appears which is what I initialize the log file to.

CODE

<?php
$count_file = 'count.log';
$my_img = imagecreatefromgif ("count.gif");
$textcolor = imagecolorallocate($my_img, 0, 0, 0);
$count = 0;
file_get_contents($count_file, $count);
imagestring($my_img, 4, 2, 3, "Counter $count", $textcolor);
$count = $count + 1;
file_put_contents($count_file, $count);
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
?>

Posted by: CharlesEF Aug 27 2018, 01:30 AM

You need to assign the results of the file read to a variable. These 2 lines:

CODE
$count = 0;
file_get_contents($count_file, $count);
Should be changed to this 1 line:
CODE
$count = file_get_contents($count_file);

Posted by: denmarks Aug 27 2018, 09:51 AM

QUOTE(CharlesEF @ Aug 26 2018, 11:30 PM) *

You need to assign the results of the file read to a variable. These 2 lines:
CODE
$count = 0;
file_get_contents($count_file, $count);
Should be changed to this 1 line:
CODE
$count = file_get_contents($count_file);



I made the change. The 100 that I initialized the file with now displays but the counter does not increment.

CODE

<?php
$count_file = 'count.log';
$my_img = imagecreatefromgif ("count.gif");
$textcolor = imagecolorallocate($my_img, 0, 0, 0);
$count = file_get_contents($count_file);
imagestring($my_img, 4, 2, 3, "Counter $count", $textcolor);
$count = $count + 1;
file_put_contents($count_file, $count);
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
?>

Posted by: CharlesEF Aug 27 2018, 01:18 PM

After the file read the variable $count will contain a string. You may have to cast the $count variable as an (INT). I just did a small test, reading and writing a file. I didn't have to cast the variable in my test. I used this line:

CODE
$count += 1;
Replace
CODE
$count = $count + 1;
with the 1 line I just posted and try it out.

Posted by: denmarks Aug 27 2018, 01:30 PM

QUOTE(CharlesEF @ Aug 27 2018, 11:18 AM) *

After the file read the variable $count will contain a string. You may have to cast the $count variable as an (INT). I just did a small test, reading and writing a file. I didn't have to cast the variable in my test. I used this line:
CODE
$count += 1;
Replace
CODE
$count = $count + 1;
with the 1 line I just posted and try it out.


I don't think that the file_put is working. I initialized the file at 100. 100 displays. The file still contains 100 after it runs. Do I have to somehow release it after the file_get.

CODE

<?php
$count_file = 'count.log';
$my_img = imagecreatefromgif ("count.gif");
$textcolor = imagecolorallocate($my_img, 0, 0, 0);
$count = file_get_contents($count_file);
imagestring($my_img, 4, 2, 3, "Counter $count", $textcolor);
$count += 1;
file_put_contents($count_file, $count);
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
?>

Posted by: Christian J Aug 27 2018, 01:37 PM

Have you made the log file writable with CHMOD (usually done with the FTP program)?

Posted by: denmarks Aug 27 2018, 02:17 PM

QUOTE(Christian J @ Aug 27 2018, 11:37 AM) *

Have you made the log file writable with CHMOD (usually done with the FTP program)?


I looked at the file permissions and it is read, write, and execute. That was the default. Does the type of "log" have any special meaning? I can change it.
You can run my file at http://www.dmmarks.com/count.php
Maybe you can see errors I do not see.

Posted by: CharlesEF Aug 27 2018, 02:25 PM

We can't see PHP code from a web page. But, if it is the same as what you have posted then we have it, if there are other changes made then post the entire script again. Put these 2 lines of code at the beginning of the script, to see any error messages.

CODE
ini_set('display_errors', 'On');
error_reporting(E_ALL);


I have an appointment so I will be offline for several hours.

Posted by: denmarks Aug 27 2018, 02:50 PM

QUOTE(CharlesEF @ Aug 27 2018, 12:25 PM) *

We can't see PHP code from a web page. But, if it is the same as what you have posted then we have it, if there are other changes made then post the entire script again. Put these 2 lines of code at the beginning of the script, to see any error messages.
CODE
ini_set('display_errors', 'On');
error_reporting(E_ALL);


I have an appointment so I will be offline for several hours.


If I add those 2 lines I get an error that nothing can be displayed. Without the lines the png is displayed with "Counter 100". I renamed the log file to data.
I changed the write to put out a string rather than the variable and then deleted the data file. It did not create the file. The code is back to the following and the data file contains 100. You can easily copy the data and gif if you want to test it. Note that the numbers in imagestring are from another program. I will adjust them later.

CODE

<?php
$count_file = "count.data";
$my_img = imagecreatefromgif ("count.gif");
$textcolor = imagecolorallocate($my_img, 0, 0, 0);
$count = file_get_contents($count_file);
imagestring($my_img, 4, 2, 3, "Counter $count", $textcolor);
$count += 1;
file_put_contents($count_file, $count);
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
?>

Posted by: Christian J Aug 27 2018, 03:52 PM

QUOTE(denmarks @ Aug 27 2018, 09:17 PM) *

I looked at the file permissions and it is read, write, and execute. That was the default.

For Owner? Maybe you also need write permission for Group, depending on the server configuration (IIRC).






Posted by: denmarks Aug 27 2018, 04:07 PM

QUOTE(Christian J @ Aug 27 2018, 01:52 PM) *

QUOTE(denmarks @ Aug 27 2018, 09:17 PM) *

I looked at the file permissions and it is read, write, and execute. That was the default.

For Owner? Maybe you also need write permission for Group, depending on the server configuration (IIRC).


Owner, Group, and Public are all the same.

Since I am new to PHP I have no idea where to start. Can someone copy all the files and test it on their site?


Posted by: Christian J Aug 27 2018, 05:55 PM

I tested it in XAMPP, and it seemed to work as expected.

I assume you're testing on an online server? Maybe the web host support can tell if the server is using an unusual configuration.

Posted by: denmarks Aug 27 2018, 06:25 PM

Thanks for your help. Since this was just an experiment I think I will go no further.

Posted by: CharlesEF Aug 27 2018, 08:12 PM

QUOTE(denmarks @ Aug 27 2018, 06:25 PM) *

Thanks for your help. Since this was just an experiment I think I will go no further.

Those 2 lines shouldn't affect anything except showing errors. You did put them after '<?php' and before any of your PHP code right?

Posted by: denmarks Aug 28 2018, 08:58 AM

QUOTE(CharlesEF @ Aug 27 2018, 06:12 PM) *

QUOTE(denmarks @ Aug 27 2018, 06:25 PM) *

Thanks for your help. Since this was just an experiment I think I will go no further.

Those 2 lines shouldn't affect anything except showing errors. You did put them after '<?php' and before any of your PHP code right?


I get:
The image "http://www.dmmarks.com/count.php" cannot be displayed because it contains errors.

CODE

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$count_file = "count.data";
$my_img = imagecreatefromgif ("count.gif");
$textcolor = imagecolorallocate($my_img, 0, 0, 0);
$count = file_get_contents($count_file);
imagestring($my_img, 4, 2, 3, "Counter $count", $textcolor);
$count += 1;
file_put_contents($count_file, $count);
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
?>


Posted by: Christian J Aug 28 2018, 09:07 AM

QUOTE(Christian J @ Aug 28 2018, 12:55 AM) *

I tested it in XAMPP, and it seemed to work as expected.

I should clarify that I only tested writing to the count.data file. The image creation part didn't work for me (I got a broken image).

Posted by: CharlesEF Aug 31 2018, 02:55 AM

This script works for me, with a test .gif I had.

CODE
$count_file = "count.log";
if($my_img = imagecreatefromgif("giphy.gif"))
{
$textcolor = imagecolorallocate($my_img, 0, 0, 0);
$count = file_get_contents($count_file);
$count += 1;
file_put_contents($count_file, $count);
if(imagestring($my_img, 4, 2, 3, "Counter $count", $textcolor))
{
  header("Content-type: image/png");
  imagepng($my_img);
  imagedestroy($my_img);
}
}
Be sure to change the .gif image name.

Posted by: Christian J Aug 31 2018, 03:51 AM

Also, the Content-Type header should be image/gif if you create a GIF image (though browsers may display it anyway).

Posted by: denmarks Aug 31 2018, 08:46 AM

QUOTE(Christian J @ Aug 31 2018, 01:51 AM) *

Also, the Content-Type header should be image/gif if you create a GIF image (though browsers may display it anyway).


Thanks for your help but it still doesn't increment. It is probably my host. I will check with them some day. I have also posted the question at the 1and1 forum and am still waiting for a response.

Posted by: CharlesEF Aug 31 2018, 12:25 PM

QUOTE(denmarks @ Aug 31 2018, 08:46 AM) *

Thanks for your help but it still doesn't increment. It is probably my host. I will check with them some day. I have also posted the question at the 1and1 forum and am still waiting for a response.

It increments and displays the .png with no error. Also, when I inserted my 2 lines for error checking I got actual error messages, don't remember them now.

Posted by: denmarks Aug 31 2018, 12:31 PM

QUOTE(CharlesEF @ Aug 31 2018, 10:25 AM) *

QUOTE(denmarks @ Aug 31 2018, 08:46 AM) *

Thanks for your help but it still doesn't increment. It is probably my host. I will check with them some day. I have also posted the question at the 1and1 forum and am still waiting for a response.

It increments and displays the .png with no error. Also, when I inserted my 2 lines for error checking I got actual error messages, don't remember them now.


I must be missing something on the server. I get errors with

ini_set('display_errors', 'On');
error_reporting(E_ALL);

but if I leave out ini_set... there is no error.

I read about a php.ini file. Do I need that? Also I can not find a php log file to look for errors.

Posted by: CharlesEF Aug 31 2018, 03:11 PM

QUOTE(denmarks @ Aug 31 2018, 12:31 PM) *

I must be missing something on the server. I get errors with

ini_set('display_errors', 'On');
error_reporting(E_ALL);

but if I leave out ini_set... there is no error.

I read about a php.ini file. Do I need that? Also I can not find a php log file to look for errors.

The php.ini file should be in the same directory that PHP was installed in, I have never heard of anyone needing to create the file themselves. The 'php-errors.log' file should be in your temp directory, the php.ini file points to the directory.

Are you running the script from a local web server OR from your online web server?

Posted by: denmarks Aug 31 2018, 03:23 PM

QUOTE(CharlesEF @ Aug 31 2018, 01:11 PM) *

QUOTE(denmarks @ Aug 31 2018, 12:31 PM) *

I must be missing something on the server. I get errors with

ini_set('display_errors', 'On');
error_reporting(E_ALL);

but if I leave out ini_set... there is no error.

I read about a php.ini file. Do I need that? Also I can not find a php log file to look for errors.

The php.ini file should be in the same directory that PHP was installed in, I have never heard of anyone needing to create the file themselves. The 'php-errors.log' file should be in your temp directory, the php.ini file points to the directory.

Are you running the script from a local web server OR from your online web server?


I have my own webspace. I do not install PHP. I do not have a temp directory. It is being run from an online web server (1and1)

Posted by: CharlesEF Aug 31 2018, 07:59 PM

I guess you should ask your hosting company where the INI and ERROR files are located. I can login to my account and have access to that information.

If you really want to learn PHP you should install a web server on your local computer. That way you KNOW when a script works correctly and can talk to your hosting company about it. I know nothing about 1and1.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)