php记录 用户当前页面停留时间

onunload +JS定时器 + ajax 实现

每秒写一次数据库肯定是不行的资源释放都来不及,使用js计时器,在离开页面时,调用ajax异步插入数据库,即不延迟跳转,也不会占用资源,插入完就释放了

HTML onunload 事件属性
定义和用法
onunload 属性会在页面下载时触发(或者浏览器窗口已关闭)。
onunload 在用户从页面导航离开时发生(通过点击链接、提交表单或者关闭浏览器窗口等等)。
注释:如果您重载页面,也会触发 unload 事件(以及 onload 事件)。



    
        
        
        
        记录当前页面存在时间
    

    
        

后台代码:

public function index(){
        dump(session('ele_timer'));
        $this->display();
    }
    public function index2(){
        $ele_timer = I('ele_timer');
        session('ele_timer',$ele_timer);
        return true;
    }
    public function index_post(){
        dump(I(''));
        dump(session('ele_timer'));
        die;
    }

你可能感兴趣的:(php记录 用户当前页面停留时间)