Jump to content

counting users


duncan_cowan

Recommended Posts

I would like to use php to count how many users there are logged in. I have a script that creates a session file with the user name and password in. How do I count how many session files there are?

Link to comment
Share on other sites

There are a lot of examples of this on the forum if you want to search. You can't reliably do it by counting sessions, because you can't tell when people close their browser or go to a different page. It's probably better to keep track of when people access pages in a database, and check the database for recent accesses. Search the forum for examples.

Link to comment
Share on other sites

HIIs it possible when a user logs in to store a "1" in a "logged in" field in a users table and after 2 minutes it will automatically change the 1 to a 0, but on the pages that are only accessible after log in, every 2 minutes the page will refresh the 1 so that the 1 will last another 2 minutes. This therefor will mean that if the user closes the page then after 2 minutes he will be classed as logged out.

Link to comment
Share on other sites

just search the form, I remember seeing this same topic like a week or 2 ago...took me like 10 secs... http://w3schools.invisionzone.com/index.ph...hl=users+onlinebasicly:check to see if there is a session $been_here and see if the time is more than 15 mins agoif more than 15 mins ago or never beforeset session $been_here with current time as the valuethen open a file and save the current time as a new line.then for the counter, open that file and count how many are from no more than 15 mins ago...

Link to comment
Share on other sites

It's probably easier to use a database then a file. You can have a timestamp in the users table, and on each page load update that field with the current timestamp. To display how many users are online, just count how many timestamps are within 1 or 2 or 5 or 10 minutes or whatever.

Link to comment
Share on other sites

Then something is not being set right. I have a cookie from your site, so the session is being used. You need to do some debugging and make sure that the data in the session is what you expect and that the database is being updated correctly. It would probably be good if you could use something like PHPMyAdmin to look in the table and make sure that the updates are happening. If all of that is working correctly, then the problem is probably the statement that gets the list of users from the database.

Link to comment
Share on other sites

the code for the site is:

<?php  require 'protected/db_connect.php';	// database connect script.if ($logged_in == 1) {header('Location: http://www.wasper-rocker.co.uk/protected/world_war_six_-.php');}//Deletes Old Database Entries  $maxtime = time() -600; $sql = mysql_query("DELETE FROM ppl_online WHERE UNIX_TIMESTAMP(activity) < '$maxtime'"); $rows = mysql_affected_rows(); if(!session_is_registered('online')){ 	@mysql_query("INSERT INTO ppl_online (session_id, activity, ip_address, refurl, user_agent) <br>		VALUES ('".session_id()."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}')"); 	session_register('online'); } else { 	if ($logged_in == 1){ 		@mysql_query("UPDATE ppl_online SET activity=now(), member='y' WHERE session_id='".session_id()."'"); 	} } if(session_is_registered('online')){		 	@mysql_query("UPDATE ppl_online SET activity=now() WHERE session_id='".session_id()."'"); } ?><html><head><meta http-equiv="Content-Language" content="en-gb"><title>World War Six - Home</title><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"><!-- ImageReady Slices (images/WW6.psd) --><center><font face="Haettenschweiler"><table id="Table_01" width="800" height="631" border="0" cellpadding="0" cellspacing="0">	<tr>		<td colspan="12">			<img src="protected/images/WW6_01.gif" width="800" height="69" alt=""></td>	</tr>	<tr>		<td>			<img src="protected/images/WW6_02.gif" width="28" height="21" alt=""></td>		<td>			<img src="protected/images/WW6_03.gif" width="93" height="21" alt=""></td>		<td colspan="3">			<img src="protected/images/WW6_04.gif" width="93" height="21" alt=""></td>		<td>			<img src="protected/images/WW6_05.gif" width="93" height="21" alt=""></td>		<td>			<img src="protected/images/WW6_06.gif" width="93" height="21" alt=""></td>		<td>			<img src="protected/images/WW6_07.gif" width="93" height="21" alt=""></td>		<td>			<img src="protected/images/WW6_08.gif" width="93" height="21" alt=""></td>		<td>			<img src="protected/images/WW6_09.gif" width="93" height="21" alt=""></td>		<td>			<img src="protected/images/WW6_10.gif" width="93" height="21" alt=""></td>		<td>			<img src="protected/images/WW6_11.gif" width="28" height="21" alt=""></td>	</tr>	<tr>		<td colspan="12">			<img src="protected/images/WW6_12.gif" width="800" height="23" alt=""></td>	</tr>	<tr>		<td colspan="3">			<img src="protected/images/WW6_13.gif" width="153" height="21" alt=""></td>		<td rowspan="9">			<img src="protected/images/WW6_14.gif" width="6" height="517" alt=""></td>		<td colspan="8">			<img src="protected/images/WW6_15.gif" width="641" height="21" alt=""></td>	</tr>	<tr>		<td colspan="3" height="85" background="protected/images/WW6_16.gif"><div align="center"><?php include("protected/login.php") ?>		<img src="protected/images/WW6_07.gif">		</div></td>		<td colspan="8" rowspan="7" valign="top" background="protected/images/WW6_17.gif"><div align="center"><br><br><br>				</tr>	<tr>		<td colspan="3">			<img src="protected/images/WW6_18.gif" width="153" height="45" alt=""></td>	</tr>	<tr>		<td colspan="3" height="85" background="protected/images/WW6_19.gif"><div align="center"><?php include("protected/memberstats.php") ?></div></td>	</tr>	<tr>		<td colspan="3">			<img src="protected/images/WW6_20.gif" width="153" height="45" alt=""></td>	</tr>	<tr>		<td colspan="3" height="85" background="protected/images/WW6_21.gif"><div align="center">Site			stats here.</div></td>	</tr>	<tr>		<td colspan="3">			<img src="protected/images/WW6_22.gif" width="153" height="45" alt=""></td>	</tr>	<tr>		<td colspan="3" height="85" background="protected/images/WW6_23.gif">		<div align="center">Site stats here.</div></td>	</tr>	<tr>		<td colspan="3">			<img src="protected/images/WW6_24.gif" width="153" height="21" alt=""></td>		<td colspan="8">			<img src="protected/images/WW6_25.gif" width="641" height="21" alt=""></td>	</tr>	<tr>		<td>			<img src="spacer.gif" width="28" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="93" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="32" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="6" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="55" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="93" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="93" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="93" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="93" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="93" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="93" height="1" alt=""></td>		<td>			<img src="spacer.gif" width="28" height="1" alt=""></td>	</tr></table><font size="1" face="Arial">©Duncan Cowan 2006.</font></center><!-- End ImageReady Slices --></body></html>

The code for "protected/memberstats.php" is:

<? // This file is included into your website // Preferably a MySQL connection has been established already $limit_time = time() - 300; // 5 Minute time out. 60 * 5 = 300 $sql = mysql_query("SELECT * FROM ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='n' GROUP BY ip_address") or die (mysql_error()); $sql_member = mysql_query("SELECT * FROM ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address") or die (mysql_error()); $visits = mysql_num_rows($sql); $members = mysql_num_rows($sql_member); echo "People Online:<br />"; echo "Guests Online: $visits<br />"; echo "Members Online: $members<br />"; ?>

Can you find any errors Please!? :)

Link to comment
Share on other sites

Well, instead of using session_is_registered() try setting the 'online' cookie an other ones to that to like yes or no or whatever, then use and if to check it, if($_SESSION['online'] == "yes"){do this}else{do this}etc.....

Link to comment
Share on other sites

I have done that and i have made a page to tell me if the session online was set and i went on the home page and then the check page but it told me that it wasnt set.the check page is http://www.wasper-rocker.co.uk/check.phpand the code on it is:

<?phpif($_SESSION['online'] == "yes"){echo "Set";}else{echo "Not Set";}?>

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...