php - 如何使用 session 将用户事件存储到数据库: Codeigniter

标签 php database codeigniter session

我有这个网站http://mxcounters.com/traffictack/

用户来到网站注册并登录。登录后,他们在自己的域中搜索一些搜索引擎优化评论。现在我想将每个用户的搜索单独存储到数据库中。然后我会将每个用户搜索的域显示到他们的帐户页面。

我尝试了一些最后的事件方法来存储在数据库中,但它没有那么有用,请帮助我:

       $time_since = now() - $this->session->userdata('last_activity');
       $interval = 300;
       // Do nothing if last activity is recent
       if ($time_since < $interval) return;
       // Update database
       $updated = $this->db
       ->set('last_activity', now())
       ->where('id', $user_id)
       ->update('users');

最佳答案

试试这个

在 Controller 中

$userId = $this->session->userdata('user_id');
if(empty($userId)){
    redirect('login'); # this for user not logged
}
else{
    $search  = $this->input->post('domain');

    $resut = $this->method_name->serach_domain($userId, $search);
    if(!empty($resut)){

        #have data. if user come here set another methods for Insert data
        $this->model_name->insert_search_result(($userId, $search);

    }
    else{

        # nodata
        echo "No result found"
    }

}

在模型中

function insert_search_result(($userId, $search){

    $data = array(
       'user_id' => $userID ,
       'search' => $search,
       'timestamp' => date('Y-m-d H:i:s')
    );

    $this->db->insert('mytable', $data);
}

关于php - 如何使用 session 将用户事件存储到数据库: Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286589/

相关文章:

php - 将信息从数据库传递到 Bootstrap Modal

javascript - 如何使实时源列表仅在事件选项卡上更新或在选项卡再次变为事件状态时更新?

php - codeigniter 数据传递 controller->library->view

javascript - 如何在 codeigniter 中使用 jquery-confirm?

php - 在预 Controller codeigniter Hook 中访问 CI session

javascript - AngularJS 身份验证安全性

php - 如何在 PHP 中从 MySQL 创建自定义 JSON 布局

android - 如何在前台和后台进程之间同步访问sqlite db?

database - 存储文件的最佳数据库

sql - 修改 Teradata 中表的主索引