How to make a Hit Counter using PHP

Hello all! in this post i m going to show you how to make a web hit counter using PHP.It is a very easy and useful hit counter which can display the total number of visits made to your website.In this post you will also learn the file handling capabilities of PHP.Following is the complete code of counter.The file counter.dat is already created on the server, than this file is read and incremented everytime your webpage is requested by a browser.


Simple PHP Gridview

If someone is migrating from ASP.Net to PHP than he must be searching for Gridview kind of funtionality in PHP.Basically this kind of job is accomplished in PHP by populating an HTML table at runtime i.e extracting each row from the database one by one and than build a table dynamically.Following code is doing the same...


Creating a Database from within PHP

Creating a database from the PHP code on runtime is very simple by using a combination of Structured Query Language and the PHP mysql_query() function.


Steps to communicate with MySql database using PHP

If your PHP web application is dealing with a database most commonly MySql,The knowledge about the following functions and their order as described is better.

  1. mysql_connect(hostname,databaseUserName,databasePassword):This is the first function which will establish a connection with the database.It returns false on connection failure.
  2. mysql_select_db("databaseName","connectionName"):This function selects the database.It takes database name and connection name as parameter, Note that connection name is optional.This function can also return false in case of any error.


MySql Connect : The mysql_connect() function.

MySql is the database mostly paired with PHP to provide robust functionality of Interactive webpages.To establish any communication with database in MySql, it is necessary to connect with the database.


PHP inlude() Function

If some lines of code are to be included in every page of the website, The PHP include() funciotn works perfect.If you have been working in ASP.Net or in Dreamweaver than you must be familiar with the role of masterpages and frames in and Dreamweaver respectively.Suppose if the look and feel of the website has to be kept similar than we can include the navigation and header files on every page using PHP include() function.


How to get the name of PHP script

To extract the name of the currently executed script from the URL, The PHP superglobal array $_SERVER can be used.The example below describes the procedure.


How to get IP address of visitor in PHP

Getting the IP address of the visitor is the simplest task in PHP.Look at the following example:

echo $userIP;


PHP Cookies

A cookie is used to identify a user.Some information about the website's user are stored at the user's computer so whenever the same user sends the request through a browser software, the stored cookie can be utilized to identify that user.
Like every good web programming language, PHP also provides a very useful and easy mechanism to accomplish the concept of cookies.The setcookie() function will create a cookie in client computer, it is must that the setcookie() function should be called before the HTML beginning code.


PHP For Loop

In every programming languages loops are applied where the repeated execution of a particular code is required until condition to be tested remains true. PHP For loop is identical to the for loop in C.


PHP Switch

The PHP switch is another conditional statement which carries out multiple condition check and execute the appropriate task based on the condition testing.
The PHP switch statement is a decent replacement of PHP if-elseif-else structure.In switch case structure only exact values can be compared i.e greater than or less than comparison is not present.


PHP if - else

Conditional statement is used where we have to take different decisions on different conditions.The PHP if-else conditional statement is very similar to the C.The PHP if-else conditional statement can be utilized in three ways.

  • Using Only if using only if statement performs a single task only if the condition is true and does nothing if the condition goes false.
  • Using if-else using if-else, if the condition is true the specified task is performed and if it is false the code in the else block will be executed.
  • Using if-elseif-elseThis is the complete form of PHP if-else conditional statement where different conditions are tested and the action is performed accordingly.


PHP Sessions

Session is a way of keeping the user information on the server to recognize a particular user.The session information is a temporary arrangement of keeping the user data on the webserver which only lasts until the browser is not closed.
Sessions are implemented in a way that whenever a session is started a unique id (UID) is assigned to the user,this ID is than stored in a cookie or embedded in the URL.After that the $_SESSION array is used to hold different information based on that UID which was assigned to the user.



The PHP $_Post function is another way of grabbing the user inputted information.When a HTML form is used with its method="post" than the data of different elements of this form can be retreived by the PHP $_post function in the PHP page which is called on submission of the form.
The $_Post function gets the data with reference to the name attribute of the form elements e.g Textbox,Checkbox and Radio buttons.Unlike the $_GET function the PHP $_POST function makes the information transfer secure.The data is not visible to the user and it is not embedded in the URL and the amount of data to be shifted is almost unlimited too.
For more clear illustration let's go through to the following example...add the following code in a html file...



The PHP $_GET function is used to transfer information from page to page.Basically the PHP $_GET function retrieves variables embedded in the url after the ? sign.If more than two,these variables are separated with & sign.
If you are familiar with ASP.Net you will find PHP $_GET function as a replacement of Querystring.To get data with PHP $_GET is not a secure way of sending data from one page to another because all the information is visible in the url, but for simple use it is very handy.


PHP Variables

Like in any other programming language, in PHP a variable is also used to dedicate some memory space to store some data.

Things to Remember...
  • The variable name must be a alpha-nemeric or may include underscores.
  • After $ sign the variable name should have letter or underscore i.e a variable name must not start with a numeric value.
  • Spaces are not allowed.


Adding PHP in HTML

In this little post,i will explain different methods of inserting PHP code in HTML, so the PHP interpreter can identify the PHP scripts embedded in HTML.


The PHP Development Environment


You write the PHP code in any editor and save it as .php file.Than this page is requested through a webserver software
such as apache.This webserver software than calls the php interpreter to process this .php file for us and dislay it on
browser unlike the static html pages which just requires a browser to show its data.
Now the problem is that setting up this environment is not so easy you have to go through the following processes.
1.Download and intall Apache.
2.Download and intall PHP and also configure it with Apache.
3.Download and install MySQL database management system.
Alternatively and recommendedly just go to this url and install wamp server which is a simple arrangement for all
these tools and its installation is fairly simple.