Reputation: 19
First of all, I'm new to php, so plz don't take it for saying' RTM or something. Atm i'm trying to make(edit) some script(s), that at the end should show me how much users are online at the moment.
But as i looked so far, there's or ip identification or cookies. This two ways aren't really accurate about counting online users by several reasons, specially the cookies one. So due to my noob status and obviously lack of knowledge at php, i want you to help me with proper user identification algorithm or even code.
I'm using a php5.5, if that may help, and i'm not using nor want to use flash/java etc at my site except pure html, css and php.
I'm trying to think how may i avoid proxies, for instance, because i know that if u use flash or java, u may go further to take a GUID (Globally Unique Identifier) or even identify users by mac address, but as i've told u, i dont wanna use anything except html,css and php. but i think i've got pretty nice idea about how it still may work very precisely:
if I'll add some unique num to the Ip(even proxies!), it would be pretty unique then. So, even if u open a new tab, the script may go for some loop and check if the ip(doesn't matter if it's proxy or not) has that addition num, and if it is just ignore it(or something like that, the point is not to add another one, like with regular ip).
so u actually get pretty sure about real statistic at the right moment of time about users online. U treat to proxies then like real ip's, and it's much more correct that way, i guess. However, I've got no idea how to actually realize that one. and if u'll help to add that future, i'll be very glad. here's the code:
$user = "blabla";
$pass = "blabla";
$host = "blabla";
$base = "blabla";
@mysql_connect($host,$user,$pass) or die(mysql_error());
@mysql_select_db($base) or die(mysql_error());
function onLine() {
$distance = 300;
$ip = $_SERVER['REMOTE_ADDR'];
$query = "DELETE FROM `online` WHERE 'stamp'+$distance < ".time()." OR `ip` = '$ip'";
@mysql_query($query) or die(mysql_error());
$query = "INSERT INTO `online`(`stamp`,`ip`) VALUES('".time()."','".$ip."')";
@mysql_query($query) or die(mysql_error());
$result = mysql_query("SELECT COUNT(*) FROM `online`");
$row = mysql_fetch_array($result,MYSQL_BOTH);
return ($row[0]);
}
echo onLine();
and of course the database's name is online, with ID int(8) A_I primary, stamp varchar(20), ip varchar(15)
too much text, i know -_-
Upvotes: 0
Views: 84
Reputation: 4833
As many people said it in the comments, ip address is not the good way to do this.
Reinventing the wheel is never a good idea as long as you're a beginner in php. I suggest you to use the sessions, which already gives a unique identifier for each user. That unique identifier is stored in a cookie, but the corresponding data is stored server-side. This is a reliable solution, and the most commonly used.
PHP stores the sessions data in internal files, but this behaviour can be changed. For example, you can set it to be stored in your database. That will allow you to know how many people is currently using your website with a simple SQL query.
Have a look at the session_set_save_handler function for the details about the implementation
Upvotes: 1