Jump to content

Recommended Posts

even if i'm new to php, so far i managed to write a who is online script.. but it's giving me a headache.. dunno whats wrong with it..

 

I have two tables.. one for guest(guest_online) and one for members(mem_online)

 

the code for the guest is working just right but the one to insert the member if he is logged in is not working as it should.. When i logged in, it insert the username in the mem_online table, but when i refresh the page, it keeps incrementing the members..

 

here's the code

<?php
session_start();
include('config.php');
include('functions.php');
$ip = $_SERVER['REMOTE_ADDR'];
$time = time();
$time_check=$time-60; //SET TIME 5 Minute

mysql_connect($server,$dbusername,$dbpassword);
mysql_select_db($db_name);
$sql = "SELECT * FROM guest_online WHERE ip = '$ip'";
$result = mysql_query($sql);

//check to see if there's a row
if(mysql_num_rows($result) == 1){
$sql1 = "UPDATE guest_online SET time = '$time' WHERE ip = '$ip'";
$result1 = mysql_query($sql1);
}else{
$sql2 = "INSERT INTO guest_online(ip,time)VALUES('$ip','$time')";
$result2 = mysql_query($sql2);
}
//end 
$sql3="SELECT * FROM guest_online";
$result3=mysql_query($sql3);

$count_user_online=mysql_num_rows($result3);

echo "Guest : $count_user_online "; 

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

// here is the code to insert the members if he is logged in
if(isset($_SESSION['user_name']))
{
$day=date('U');
$sql5 = "INSERT into mem_online VALUES('','$mem','$day','$ip')";
$result5 = mysql_query($sql5);
}
$sql6 = "SELECT * FROM mem_online";
$result6 = mysql_query($sql6);
$memonline = mysql_num_rows($result6);

echo "<br>Members : $memonline ";
?>

 

I tried to log in, and everytime is refresh the page, it keeps inserting my username and Members keeps increasing. Why is it so?

 

Can someone help?

if you dont use post data then i guess the prob is that u dont have a validation maybe you can try

$_SERVER['HTTP_REFERER'] to know if the user is form that page and or refresh the page

 

something like

if ($_SERVER['HTTP_REFERER'] == your page){//dont update}

I dont really get this part. How would i implement this with my code  ??? :'(

 

When i logged in with my username and password.. it shows one member, and when i refresh it shows two and keeps increasing.

 

I would like to try the $_SERVER['HTTP_REFERER'] but don't know how to use it  :-\

$_SERVER['HTTP_REFERER']  <-- determine what is the last page that user visited

 

sample your form index.php and you go to teng.php and you echo $_SERVER['HTTP_REFERER']

that should give you index.php

 

the logic i see is that once your form index.php and you refresh the page the value of $_SERVER['HTTP_REFERER'] is index.php

 

does :D it make sense

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.