Php simple counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
splintro
Forum Newbie
Posts: 2
Joined: Mon Sep 02, 2002 1:16 am

Php simple counter

Post by splintro »

Hi everybody!
I have a simple question: I have a HTML file (let's call it index.html). I want to put a php counter in this page (not shown on the page, only for my information). I don't understand very well the PHP philosophy... because I know perfectly to create a php file, containing a counter. But how can I launch this script automaticaly when the index.html page is accesed?
Thank you all and have a nice day.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

change "index.html" to "index.php". And add the following code

Code: Select all

<?php
$fh = fopen("counter.inc","a+");
$count = fread($fh,filesize($fh));
$count++;
fwrite($fh,$count);
fclose($fh);
?>
And create a new file called "counter.inc" in the same folder as "index.php". Change the permission to "777".
EvanClark
Forum Newbie
Posts: 9
Joined: Thu Aug 22, 2002 1:36 am
Contact:

Post by EvanClark »

Takuma ... I'm gonna sound like a pain in the ass here but...

You shouldn't call your counter file 'counter.inc' since it obviously isn't an include file.

It would be better PHPing to call it 'counter.dat'.

:P
splintro
Forum Newbie
Posts: 2
Joined: Mon Sep 02, 2002 1:16 am

Mention note ...

Post by splintro »

Thanx Takuma. I knew this solution (changing the index.html into index.php and so on...)
The problem is that I want to keep my index.html file and to call the 'counter.inc' from inside it. Is there any posibility? Like an automatically launched FORM (without the SUBMIT button) with the php script as the action. Is it a possibility? Can you include a FORM into a a.html file to launch automatically a php file, withou any SUBMIT button?
Thank you all for you time.
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

Hi,

See the code below, which does the automatic FORM submit to .php file:

Code: Select all

<HTML>
    <HEAD>
        <SCRIPT type="text/javascript">
            function submitFrm()
            &#123;
                document.frm.submit();
            &#125;
        </SCRIPT>
    </HEAD>

    <BODY onLoad="submitFrm();">
        <FORM name=frm action=counter.php>
        </FORM>
    </BODY>
</HTML>
The document.frm.submit() can be written in the onLoad itself also. But in case if you want to do any more processing, you can do it in the submitFrm(). The <FORM> can contain any hidden variables also.

Regards,
Post Reply