Monday, 24 February 2014

How to Create a Contact Form for Your Website in an Easy Way : A Set of Step by Step Tutorials Using HTML5, CSS3 and PHP (10)

(For the latest PDF files Merger Software please have a look at the top of the left margin, inside the red box.)
Continues From Part 9
In the previous post I got  the IP of the visitor and displayed it on the bottom of the contact page as a courtesy. I also put it inside the messages they might send for me. To keep the IP of any visitor who might send a message or just click on my page, I keep a record of IP's of all the visitors. Allow me to advance further and add more elaborations to my contact form.
Remember that I am not addressing any security issue such as preventing SQL Injection or making PHP codes more accomplished by using advanced functions. Sometimes they are controversial even among the PHP experts. I have done many computer codes from Assembly and machine codes, Fortran, dos to Eifel. Now I only do assembly, C, and Java and sometimes Windows C++. I do not like to become engaged in PHP. See, it is advised to change $Name = $_POST['Name'] to $Name = filter_input(INPUT_POST, 'Name'). This works. but a similar trick, filter_input(INPUT_SERVER, "REMOTE_ADDR") in place of $_SERVER["REMOTE_ADDR"] does not work, uniformly (might work intermittently; mostly dispatches Server Error). It is due to the fact that as you might notice, parameters and references are not uniform. Hence, the interested readers might like to study manuals deeper.

How to make a simple basic database of visitors' IP's?

How to collect (put) visitors' IP records in a text file?

It is usual that web servers use MySQL to gather information in a systematic manner. I make a simple text to keep records of visitors IP. Please remember line (217) of contact form in previous post. This is where the visitors get their IP's upon landing on the page and the best time to catch a record of the visit. Therefore, add the following snippet after this line and before line (218). Hence Line (218) will become Line (228)

 $ip_file = 'save_ip.txt';
 $date_ti = date('Y-m-d H:i:s');
       $file_content = file_get_contents($ip_file);
       file_put_contents($ip_file, $file_content . "\r\n" . $date_ti . "\t||\t" . $visitor_ip);  
 else{//create text file and put header for the table
      file_put_contents($ip_file, "Date and Time" . "\t||\t" . "Visitors IPs"
               . "\r\n" . $date_ti . "\t||\t" . $visitor_ip);

(Download PHP code in Zip format here or open the code in text format here). On each landing, the code snippet checks if the text file  'save_ip.txt' is already created and exists. If it is the first ever reference to this file it creates the file and puts a simple header for its columns. Then goes to the next line and puts the date, time and IP of the first visitor in the second row. However, if the text file already exists code snippet gets contents of the text, rewrites it. Then goes to the next line and adds one new record to previous records. I tried to add some formatting and vertical bars to separate columns, nevertheless, readers might like to impose their own ways of doing that. Remember that, at the end, one needs to use MySQL to accumulate these information or to invoke third party tools and subscribed services.
Also, it is worth mentioning that one needs to become familiar with PHP tabs, new line, carriage-return, end-of-line and so on. I used the familiar "\t", "\r", "\n" for purposes of this article, but frequently, they do not behave in such a simple way as they should.
You can learn about PHP date and time formats, too, from their wikis.
After two visits you can open the text file in your browser to watch records of visitors IP's or you can download and read them with a text editor, or other ways that your web server allows.

Please check permissions (three digits numbers such as 644) for the created file not to be accessible to unwanted visitors. (In the last snap-shot IP's painted with wrong numbers for privacy of visitors.)
Please click here to open the contact form and then open the saved text to see that your own IP has been added (many times a day I renew the text to keep privacy of previous visitors).

Download this tutorial as PDF format here
Download part 1 to part 10 as PDF format here

1 comment:

  1. Hello! I сould have sworn I've beеn to this site befоre but after
    reading througҺ somе of the post I realized it's new to me.
    Nonethеlеss, I'm definitely delightеԀ I fօund it and I'll be Ьookmarking and checking back

    Here is my weblog Alghero live