Monday, November 11, 2013

Your own php online counter for website

This is the script for online counter .It can be used in your website to display online visitors in your site.

(use with your own css to have a better look)

*How to make ?


Step 1 ==>In your php site make a database.
Note the username,password and the database name of the created database.

Step 2 ==> Now create two files sql.sql and user_online.php in your site using ftp.

Step 3 ==>Copy and paste below content to user_online.php

<?
/*

JOIN US https://m.facebook.com/groups/427625107334527
Dedicated to Our New Member Arun Raj and Okechukwu Friday
*/

session_start();
$session=session_id();
$time=time();
$time_check=$time-600;
//SET TIME 10 Minute

$host="paste mysql server name here"; //My sql Host name obtained from the host
$username="paste username of database"; // Mysql username
$password="paste password of database"; // Mysql password
$db_name="paste name of database"; // Database name
$tbl_name="user_online"; // Table name

// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE session='$session'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count=="0"){
$sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
$result1=mysql_query($sql1);
}
else {
"$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
$result2=mysql_query($sql2);
}

$sql3="SELECT * FROM $tbl_name";
$result3=mysql_query($sql3);

$count_user_online=mysql_num_rows($result3);

echo "Online ($count_user_online) ";

// if over 10 minute, delete session
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);

mysql_close();

// Open multiple browser page for result
?>

Step4==> Copy and paste below code to sql.sql file

CREATE TABLE `user_online` (
`session` char(100) NOT NULL default '',
`time` int(11) NOT NULL default '0'
) TYPE=MyISAM;
Step5==> Now save and again open the sql.sql file.
There will be an option in the ftp site like "create sql setup wizard". Click it
A form will appear . Fill it as given below

Name:install.php
Server:paste mysql server name
User: paste mysql username
Password:paste mysql password
Database:paste mysql database name
DROP:Yes
CREATE:Yes
INSERT:Yes
and click submit.

Now type the location of  install.php  (like http://ypoursite.com/onlin_count/install.php) in your web browser and go to the url.


== Now it is ready to use ====
if you do not see "create sql setup wizard" in your ftp client just enter the phpmyadmin of your mysql database and paste the contents of sql.sql file there and run that script.

*How to use ?


In your php site use "include" function
to display this counter.

Eg: echo'<div class="smart_coders">';include 'user_online.php';
echo'</div>';

Enjoy this counter.

Demo Here:   http://appz.x20x.ml/project/online_counter/user_online.php

                                                                                                                                           Thankz.