Wednesday, June 15, 2011

CHECK IF USER ONLINE OR AWAY SCRIPT


this script will tell if user is online doing something on your site or if he is away. if user remained inactive for 10 seconds he will labeled away create table in your data base called members with 4 fields 1d, email, password and activity. and enter some dummy data mysql.php <?php @mysql_connect("localhost","root","") or die ("could not connect to mysql"); @mysql_select_db("name of your database") or die ("no database"); ?> index.php (will contain the login script) <?php if ($_POST['email']) { //Connect to the database through our include include_once "mysql.php"; $email = $_POST['email']; $password = $_POST['password']; $sql = mysql_query("SELECT * FROM members WHERE email='$email' AND password='$password'"); $login_check = mysql_num_rows($sql); if($login_check > 0){ while($row = mysql_fetch_array($sql)){ $id = $row["id"]; session_register('id'); $_SESSION['id'] = $id; header ("Location: profile.php?id=$id"); exit(); } // close while } } ?> <style type="text/css"> <!-- body { } --> </style> <script type="text/javascript" src="js/jquery-1.4.2.js"></script> <script type="text/javascript"> </script> <form action="index.php" method="post" class="body">       <input name="email" type="text" class="textfield" id="textfield" value=""/>       <br />       <input name="password" type="password" class="textfield" id="textfield2" value=""/>       <br />       <input type="submit" name="button" id="button" value="Sign In" /> </form> //////////////////////// profile.php <?php session_start(); // Must start session first thing  if (!isset($_SESSION['id'])) { print 'you are not logged in'; exit(); } include_once "mysql.php"; if (isset($_SESSION['id'])) { $id = $_SESSION['id']; $mid = $_GET['id']; $time=time();//to get the timestamp  } mysql_query("UPDATE members SET lastactivity='$time' WHERE id='$id'");//every time this page refresh lastactivity will be updated $sql = mysql_query("SELECT * FROM members WHERE id='$mid'"); while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $email = $row["email"]; } ?>   <script type="text/javascript" src="js/jquery-1.4.2.js"></script>   <script type="text/javascript"> //load check.php in the <span id="online"></span> and refresh every 1 second $(document).ready(function(){    $("#online").load('check.php?id=<?php print"$id";?>');    setInterval(function(){   $("#online").load('check.php?id=<?php print"$id";?>');    },1000);// }); </script> this is <b><?php print"$email";?></b> and he is <b><span id="online"></b></span> //////////////////////////// check.php <?php session_start(); // Must start session first thing  if (!isset($_SESSION['id'])) { print 'you are not logged in'; exit(); } include_once "mysql.php"; if (isset($_SESSION['id'])) { $id = $_SESSION['id']; $mid = $_GET['id']; $time=time();  } $sql = mysql_query("SELECT * FROM members WHERE id='$mid'"); while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $email = $row["email"]; $lastactivity = $row["lastactivity"]; if(time() > $lastactivity+10/*10 is the number of seconds user stay online without doing activity on the page you can always change it */){$onlineStats = "away";}//if the last time you did something on your page + 10 seconds is less than the current time. your onlinestats will equal away or you can change to offline else if(time() < $lastactivity+10){$onlineStats = "online";}//if the last time you did something on your page + 10 seconds is greater than the current time. your onlinestats will equal online } ?> <?php print"$onlineStats";?>

No comments:

Post a Comment