The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

2 Pages V  1 2 >  
Reply to this topicStart new topic
> Help for simple PHP script
denmarks
post 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 );
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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;
file_get_contents($count_file, $count);
Should be changed to this 1 line:
CODE
$count = file_get_contents($count_file);
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post Aug 27 2018, 09:51 AM
Post #3


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



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 );
?>


This post has been edited by denmarks: Aug 27 2018, 09:57 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post Aug 27 2018, 01:30 PM
Post #5


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



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 );
?>


This post has been edited by denmarks: Aug 27 2018, 01:34 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Aug 27 2018, 01:37 PM
Post #6


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



Have you made the log file writable with CHMOD (usually done with the FTP program)?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post Aug 27 2018, 02:17 PM
Post #7


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post Aug 27 2018, 02:50 PM
Post #9


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



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 );
?>


This post has been edited by denmarks: Aug 27 2018, 02:53 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Aug 27 2018, 03:52 PM
Post #10


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



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).





User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post Aug 27 2018, 04:07 PM
Post #11


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



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?

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Aug 27 2018, 05:55 PM
Post #12


.
********

Group: WDG Moderators
Posts: 9,630
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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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



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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post Aug 28 2018, 08:58 AM
Post #15


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



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 );
?>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Aug 28 2018, 09:07 AM
Post #16


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



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).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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";
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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Aug 31 2018, 03:51 AM
Post #18


.
********

Group: WDG Moderators
Posts: 9,630
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).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post Aug 31 2018, 08:46 AM
Post #19


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post 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



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th March 2024 - 07:43 AM