Page 1 of 1

Php simple counter

Posted: Mon Sep 02, 2002 1:16 am
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.

Posted: Mon Sep 02, 2002 1:30 am
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".

Posted: Mon Sep 02, 2002 4:48 am
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

Mention note ...

Posted: Mon Sep 02, 2002 5:40 am
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.

Posted: Mon Sep 02, 2002 6:50 am
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,