【问题标题】:How to store user activities to database using sessions : Codeigniter如何使用会话将用户活动存储到数据库:Codeigniter
【发布时间】:2015-12-15 10:22:01
【问题描述】:

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

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

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

       $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');

【问题讨论】:

  • 你想要存储用户活动的时间??和他们去过的地方单独在一起??
  • 用户从他们的帐户中搜索网站评论。所以每次用户搜索都会存储到数据库中对应的用户数据中。
  • search 表示任何搜索字段输入??
  • 是的,有一个表格检查这个链接"mxcounters.com/traffictack/woorank你在Skype上吗?
  • 我有发送请求。

标签: php database codeigniter session


【解决方案1】:

试试这个

在控制器中

$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);
}

【讨论】:

  • 感谢您的帮助。我很接近它。
【解决方案2】:

如果你想插入每个搜索,你可以使用:

首先创建一个存储记录的表。

CREATE TABLE `usersearchs` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR(50) NOT NULL DEFAULT '0',
`search` VARCHAR(50) NOT NULL DEFAULT '0',
`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX `id` (`id`)
)
ENGINE=InnoDB
;

在此表中,时间戳会自动添加到条目中。

然后在每次搜索时使用插入查询:

INSERT INTO usersearch (user_id, search) VALUES ($user_id, $search_string);

如果你只想更新 last_activity 列:

更改您的表,在每次表更新后更新 last_activity 并使用第二列,例如命名为 searchString 以包含最后搜索的项目。

ALTER TABLE `users`
CHANGE COLUMN `last_activity` `last_activity` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
ADD COLUMN `searchString` VARCHAR(50) NOT NULL AFTER;

然后在每次搜索时使用查询:

UPDATE users SET searchString = `$searchstring` WHERE id = $userID

使用数据库时请使用右转义(PDO 预处理语句是一个不错的选择)

【讨论】:

    猜你喜欢
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多