Jump to content

Recommended Posts

Hi to everyone, i'm new here and i'm looking for a good tutorial about paging and a good tutorial on showing how many guests and how many logged members there are actually.. Just like this forum

 

Can someone tell me where to find it? [especially the tutorial on showing how many guests and how many logged members there are]

 

Any help would be appreciated. Thanks a ton

Link to comment
https://forums.phpfreaks.com/topic/75047-solved-can-someone-guide-me/
Share on other sites

paging? showing certain data? that is irrelavent. 

Pagination is what I am assuming you are talking about and it is not dependent on the fields you are taking out of sql, only the criteria of the search.

 

Huhh yeah pagination.. i got this one working.. but now im only searching tips to make this who is online script to show the number of visitors and members.. can someone tell me how to begin with this?

I tried man  ;D but something is bothering me..

 

Most of the tutorial i've seen add a visitor though his ip, which is correct i presume? now my question is, when this visitor logs in, he should be removed from the visitors list, right? but wouldn't the script read his ip and insert him once again in the visitors list??

Sorry if i'm asking those question. I just started with php and mysql and well, there are lots of genius in php in this forum, i might get some ideas..  ;D

Ok, i have two tables

 

#active_guests#

ip | timestamp

 

and table

 

#active_users#

username | timestamp

 

Here is the code i have to show the number of active users

<?php
session_start();
$conn = mysql_connect(".....","....","....");
mysql_select_db("tech");

$time = time();
$time_out = 300;
$guest_time_out = time()-300;
$member = $_SESSION['member'];
$ip = $_SERVER['REMOTE_ADDR'];

$sql = "SELECT * FROM active_guests WHERE ip = '$ip'";
$result = @mysql_query($sql,$conn);
$num = @mysql_num_rows($result);

if($num == 0){
$q = "INSERT INTO active_guests(ip,timestamp)VALUES('$ip','$time')";
$res = @mysql_query($q);
}else{
$q = "UPDATE active_guests SET timestamp = '$time' WHERE ip = '$ip'";
$res = @mysql_query($q);
}

$cnx = "SELECT * FROM active_guests";
$resultat = @mysql_query($cnx);
$visitors = @mysql_num_rows($resultat);


// if over 5 minute, delete session
$sql4="DELETE FROM active_guests WHERE time<$guest_time_out";
$result4=@mysql_query($sql4);

echo "Visitors online : $visitors ";
?>

 

as you can see,

 

##$member = $_SESSION['member']; is when someone logs in..

 

Now if someone logs in, it should add them to the active_users table and remove them from the visitors list.. the problem is that i don't know how to get it to work..

 

Hope i'm not bugging you with this.

well copy that table of guest users online, for logged in users

then say

<?php
if(!empty($message)){
//delete from the guest table where ip = the users ip
//Insert into or update member online table
}
//Check guest and users online for old timestamps and delete where needed.
?>

That is the just idea, use what you got and it should make sense after you make the new table/ add in this tidbit.

ok thats what im using now, but it doesn't seems to work right

 

<?php
session_start();
include('configuration.php');
include('sessions.php');
$conn = mysql_connect($server,$dbname,$password);
mysql_select_db($dbname);

$time = time();
$time_out = 300;
$guest_time_out = time()-300;
$member = $_SESSION['member'];
$ip = $_SERVER['REMOTE_ADDR'];

$sql = "SELECT * FROM active_guests WHERE ip = '$ip'";
$result = @mysql_query($sql,$conn);
$num = @mysql_num_rows($result);

if($num == 0){
$q = "INSERT INTO active_guests(ip,timestamp)VALUES('$ip','$time')";
$res = @mysql_query($q);
}else{
$q = "UPDATE active_guests SET timestamp = '$time' WHERE ip = '$ip'";
$res = @mysql_query($q);
}
// grab result for visitors
$cnx = "SELECT * FROM active_guests";
$resultat = @mysql_query($cnx);
$visitors = @mysql_num_rows($resultat);


// if over 5 minute, delete session
$sql4="DELETE FROM active_guests WHERE time<$guest_time_out";
$result4=@mysql_query($sql4);

    echo "Visitors : $visitors ";

if(!empty($member)){ 
//delete the ip of the member in the active guest table
$del = "DELETE FROM active_guests WHERE ip = '$ip'";
$delresult = @mysql_query($del);
//Insert into or update member online table
$sql = "SELECT * FROM active_users WHERE username = '$member'";
$result = @mysql_query($sql);
$num = @mysql_num_rows($result);
if($num == 0)
{
$sql = "INSERT INTO active_users(username,timestamp)VALUES('$member','$time')";
$result = @mysql_query($sql);
}else{
$sql = "UPDATE active_users SET timestamp = '$time' WHERE username = '$member'";
$result = @mysql_query($sql);

}
}
$grab = "SELECT * FROM active_users";
$result = @mysql_query($grab);
$count = mysql_num_rows($result);
echo "<br>Members online : $count ";
?>

 

When im logged in, it doesn't seem to delete my IP from the active_guest table, but Members online get to 1, and when i logged out, members is still on 1

 

Why is it so? Did i implement the code well?

well first off don't suppress errors with that @ sign, its never good to do so ever even on a live site. (unless its like corporate level stuff).  Secondly stuff using that * operator its going to lag up your pages, just say select count(*) and then say $result = mysql_query($sql) or die(Mysql_error());  if(mysql_result($result,0)>0){

 

it will save you a lot if you replace all of your queries.

 

Now for the actual issues.

 

You don't have a delete query set (unless its part of your logout script which you aren't displaying to us).  so this is why member count stays up.  as for the guest count you can't delete something you are creating on your pages since you are making that query on the same page use some logic.

 

I cleaned up your stuff cause It was well ugly to me, its still not perfect, but it should be more coherent.  Also Its untested so it will probably error

 

<?php
session_start();
include('configuration.php');
include('sessions.php');
$conn = mysql_connect($server,$dbname,$password);
mysql_select_db($dbname);

$time = time();
$time_out = 300;
$guest_time_out = time()-300;
$member = $_SESSION['member'];
$ip = $_SERVER['REMOTE_ADDR'];
if(!empty($member)){ 
//delete the ip of the member in the active guest table
$del = "DELETE FROM active_guests WHERE ip = '$ip'";
$delresult = mysql_query($del) or die(mysql_error());
//Insert into or update member online table
$sql = "SELECT count(*) FROM active_users WHERE username = '$member'";
$result = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($result);
if($num == 0){
	$sql = "INSERT INTO active_users(username,timestamp)VALUES('$member','$time')";
	$result = mysql_query($sql) or die(mysql_error());
}
else{
	$sql = "UPDATE active_users SET timestamp = '$time' WHERE username = '$member'";
	$result = mysql_query($sql) or die(mysql_error());
}
}
else{
$sql = "SELECT count(*) FROM active_guests WHERE ip = '$ip'";
$result = mysql_query($sql,$conn)or die(mysql_error());
if(mysql_results($result,0) == 0){
	$q = "INSERT INTO active_guests(ip,timestamp)VALUES('$ip','$time')";
	$res = mysql_query($q) or die(mysql_error());
}
else{
	$q = "UPDATE active_guests SET timestamp = '$time' WHERE ip = '$ip'";
	$res = mysql_query($q) or die(mysql_error());
}
}	
// if over 5 minute, delete session
$sql4="DELETE FROM active_guests WHERE time<$guest_time_out";
$result4 = mysql_query($sql4);
// if over 5 minute, delete session
$sql5 = "DELETE FROM active_users WHERE time<$guest_time_out";
$result5 = mysql_query($sql5) or die(mysql_error());


// grab result for visitors
$cnx = "SELECT count(*) FROM active_guests";
$resultat = mysql_query($cnx) or die(mysql_error());
$visitiors = mysql_result($resultat,0);
$grab = "SELECT count(*) FROM active_users";
$result = mysql_query($grab) or die(mysql_error());
$count = mysql_num_rows($result);
echo "Visitors : $visitors ";
echo "<br />Members online : $count ";
?>

 

 

to your logout script add

<?php
$q = "Delete from `active_users` Where Username = '".$_SESSION['member']."'";
$r = mysql_query($q) or die(mysql_error());
?>

and that should help

 

 

EDIT: I noticed a couple of minor errors so do a recopy if u are reusing it

well first off don't suppress errors with that @ sign, its never good to do so ever even on a live site. (unless its like corporate level stuff).  Secondly stuff using that * operator its going to lag up your pages, just say select count(*) and then say $result = mysql_query($sql) or die(Mysql_error());  if(mysql_result($result,0)>0){

 

it will save you a lot if you replace all of your queries.

 

Thats what i needed to know  ;D Thanks for the tip tho. I appreciate ur time.. your script and running just fine..just modify it a bit to suits my need.. it did what i wanted to .. Thanks for your help. I should save this example somewhere just to learn from it..

 

I cleaned up your stuff cause It was well ugly to me,

 

Don't forget that i'm new to php :P..

 

Cheers and thanks again

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.