【问题标题】:Where to put my hit counter code in my php class of codeigniter?将我的命中计数器代码放在我的 php 类 codeigniter 中的什么位置?
【发布时间】:2014-05-05 21:47:26
【问题描述】:

我已经创建了一个计数器,它工作正常,但问题是,我已经将代码放在构造函数中,如下所示:

// working code
<?php
class Welcome extends CI_Controller{
      function __construct()
      {
          hit_counter(); // works perfectly fine...
      }
      function view_blog()
      {
          // perfectly working code
      }
      function other_function()
      {
          // working fine
      }
}

现在的问题是,每当用户第一次访问该网站时,它都会运行代码,但是当他访问view_blog时,它也运行,当在other_function时,它再次运行,我只想这样做,我的计数器只计算他一次,之后他应该只在他下次访问该网站时计算,而不是在他访问各种功能时计算。

【问题讨论】:

    标签: php codeigniter hitcounter


    【解决方案1】:

    为什么不实现 PHP 原生会话?你也可以在你的 CI 的会话中进行这个实现。

    <?php
    session_start(); //<--- Add here
    class Welcome extends CI_Controller{
        function __construct()
        {
            if(!isset($_SESSION['visited'])) 
            {
            hit_counter(); // works perfectly fine...
            $_SESSION['visited'] = true; //<--- Sets here the first time.
            }
        }
    

    【讨论】:

    • 但是如何销毁会话,一旦他离开,如果他想在 30 秒后访问,会话可能会保留
    • 他使用了本机会话,所以当浏览器窗口关闭时会话被破坏。
    • 由于一些奇怪的原因它不起作用,我也关闭了浏览器和标签,但似乎在下次访问时不起作用:(
    • @avinashizhere,关闭浏览器时无法检索会话。所以下次他来的时候,它会触发hitCounter()。你能解释一下not working吗?
    • 让我检查实时服务器,现在我在 localhost 上使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    相关资源
    最近更新 更多