Hit counter

A hit counter will let us know how many times a page is accessed. In case one visitors loads the page several times, the hit counter will increase several times (but this is likely to happen only a few times).

The code for the hit counter bellow will save the number of hits in a file named counter.txt (the name of this file may be changed). Each time the page is loaded, the file will be read, the number will be increased by one and the new number will be saved to the same file.

counter.php

<?php

//The file where number of hits will be saved; name may be changed; p.e. "/counter_files/counter1.txt"
$counterfile = "counter.txt";

// Opening the file; number of hit is stored in variable $hits
$fp = fopen($counterfile,"r");
$hits = fgets($fp,100);
fclose($fp);

//increading number of hits
$hits++;

//saving number of hits
$fp = fopen($counterfile,"w");
fputs($fp, $hits);
fclose($fp);

//printing  hits; you may remove next line (and keep the counter only for your records)
print $hits;

?>


To use this code, copy it to your page in the exact position where you want to show number of hits. If your page is a ".html" page, change the extension to ".php". Them, visit your page. In the first visit, you will get a couple of errors (the file where number of hits are recorded does not exists, so some errors will be shown in the page). Reload the page and you will see no errors. The hit counter will be working.

In case you want to use different hit counters for different pages, chage name of $counterfile for each page (p.e.: counter1.txt, counter2.txt, etc).  
Share:

Generate Php Form for mailing

 In this tutorial explain about 'How make mail form in Php?'. First of all you need a form, as for example the one in the table:

Form.html 

<FORM ACTION="formtomail.php" METHOD=post>

<!-- Your fields here -->

<INPUT TYPE=submit value="Submit">
</FORM> 

The Form Action must be directed to the PHP script bellow. you need the php script (copy the information in the table to a text file and save the file as "formtomail.php" in your server).

Formtomail.php 
<?
if ($_POST){
      // posted information is added to variable message
      $message="";
      foreach ($_POST as $formfieldname => $formfieldvalue){
            $message.="$formfieldname \n $formfieldvalue\n\n";     //
      }
     // send email with information from the form to
      mail("webmaster@mysite.com","Form to Mail", $message,"From: <webmaster@mysite.com>\nContent-Type: text/plain");
      print "The information has been send to webmaster";
}else{
      print "No information has been posted";
}
 
?> 

You need to customize the script: substitute red text in the script by your email. This scripts assumes you have no restrictions to use mail commnad.
Share: