Help for simple PHP script |
Help for simple PHP script |
denmarks |
Aug 26 2018, 03:50 PM
Post
#1
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
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 ); ?> |
CharlesEF |
Aug 27 2018, 01:30 AM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
You need to assign the results of the file read to a variable. These 2 lines:
CODE $count = 0; Should be changed to this 1 line: file_get_contents($count_file, $count); CODE $count = file_get_contents($count_file); |
denmarks |
Aug 27 2018, 09:51 AM
Post
#3
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
You need to assign the results of the file read to a variable. These 2 lines: CODE $count = 0; Should be changed to this 1 line: file_get_contents($count_file, $count); 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 ); ?> This post has been edited by denmarks: Aug 27 2018, 09:57 AM |
CharlesEF |
Aug 27 2018, 01:18 PM
Post
#4
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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. |
denmarks |
Aug 27 2018, 01:30 PM
Post
#5
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
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 ); ?> This post has been edited by denmarks: Aug 27 2018, 01:34 PM |
Christian J |
Aug 27 2018, 01:37 PM
Post
#6
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
Have you made the log file writable with CHMOD (usually done with the FTP program)?
|
denmarks |
Aug 27 2018, 02:17 PM
Post
#7
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
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. |
CharlesEF |
Aug 27 2018, 02:25 PM
Post
#8
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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. This post has been edited by CharlesEF: Aug 27 2018, 02:29 PM |
denmarks |
Aug 27 2018, 02:50 PM
Post
#9
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
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 ); ?> This post has been edited by denmarks: Aug 27 2018, 02:53 PM |
Christian J |
Aug 27 2018, 03:52 PM
Post
#10
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
|
denmarks |
Aug 27 2018, 04:07 PM
Post
#11
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
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? |
Christian J |
Aug 27 2018, 05:55 PM
Post
#12
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
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. |
denmarks |
Aug 27 2018, 06:25 PM
Post
#13
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
Thanks for your help. Since this was just an experiment I think I will go no further.
|
CharlesEF |
Aug 27 2018, 08:12 PM
Post
#14
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
|
denmarks |
Aug 28 2018, 08:58 AM
Post
#15
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
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 ); ?> |
Christian J |
Aug 28 2018, 09:07 AM
Post
#16
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
|
CharlesEF |
Aug 31 2018, 02:55 AM
Post
#17
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
This script works for me, with a test .gif I had.
CODE $count_file = "count.log"; Be sure to change the .gif image name.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); } } |
Christian J |
Aug 31 2018, 03:51 AM
Post
#18
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
Also, the Content-Type header should be image/gif if you create a GIF image (though browsers may display it anyway).
|
denmarks |
Aug 31 2018, 08:46 AM
Post
#19
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
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. |
CharlesEF |
Aug 31 2018, 12:25 PM
Post
#20
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
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. |
Lo-Fi Version | Time is now: 24th April 2024 - 06:22 AM |