douqiao2471 2014-04-25 10:12
浏览 58

按唯一编号定义用户

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 -_-

  • 写回答

1条回答 默认 最新

  • dro62273 2014-04-25 11:03
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大