简体   繁体   English

使用“全局”变量的CodeIgniter计数器

[英]CodeIgniter counter using “global” variable

I am a newbie CodeIgniter developer and I must say my frustration rached to new heights when I realized I wasn't able to make work a very simple click counter. 我是CodeIgniter的新手开发人员,当我意识到无法将工作变成一个非常简单的点击计数器时,我不得不感到沮丧。 The idea is you click on a button and get the number of clicks counted. 想法是您单击一个按钮并获得计算的点击次数。 This is the view simplecounter_view: 这是视图simplecounter_view:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<html>
    <body>  
     <?php
        echo '<form action="'. base_url().'index.php/simplecounter" method="POST">';        
        echo '<font color="blue">Click counter:' . $counter . '</font><br/><br/>';              
        echo '<input type="submit" name="myform" value="Count">';
        echo '</form>';
     ?>
    </div>
  </body>
</html>

If you click the form button it will call the SimpleCounter Controller: 如果单击表单按钮,它将调用SimpleCounter控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class SimpleCounter extends CI_Controller { 
    public function __construct() {             
        parent::__construct();
      $this->load->helper('url');
    }
    public function index() {
        $myform=$this->input->post('myform');
        if ($myform=='Count') {
            Globals::setCounter(Globals::getCounter()+1);
        }   
        $data['counter'] = Globals::getCounter();
        $this->load->view('simplecounter_view', $data);         
    }
}

Finally, I am using a Globals class with the property $counter in order to keep its value "global" and accessible from the Controller: 最后,我将Globals类与$ counter属性一起使用,以使其值保持“全局”并可从Controller访问:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Globals {
     private static $initialized=false;
     private static $counter;   

    private function __construct() {}
    private static function initialize() {
        if (self::$initialized)
            return;

        self::$counter = 0;
        self::$initialized = true;    
     }    
    public static function setCounter($n) {
        self::initialize();
        self::$counter = $n;
    }
    public static function getCounter() {
        self::initialize();
        return self::$counter;
    }
}

The code above doesn't work as I expected. 上面的代码无法正常工作。 The click counter doesn't get updated on every user click. 点击计数器不会在每次用户点击时更新。 It seems to me the Globals class gets recreated every time one of its methods is called and so $counter is reset to zero and $initialized to false. 在我看来,每次调用其方法之一时都会重新创建Globals类,因此$ counter重置为零,而$ initialized为false。

I understand there might be different approaches (like defining $counter in config.php) though I think using a separated class for keeping $counter is more elegant. 我知道可能会有不同的方法(例如在config.php中定义$ counter),尽管我认为使用单独的类来保持$ counter更为优雅。 What am I missing here? 我在这里想念什么? Where did I get it all wrong? 我在哪里弄错了? Your help is much appreciated. 非常感谢您的帮助。

Since HTTP is stateless protocol, once the http response is sent to the requesting browser all your variables get lost. 由于HTTP是无状态协议,因此一旦将HTTP响应发送到发出请求的浏览器,您的所有变量都会丢失。

In computing, a stateless protocol is a communications protocol that treats each request as an independent transaction that is unrelated to any previous request so that the communication consists of independent pairs of request and response. 在计算中,无状态协议是一种通信协议,将每个请求视为与任何先前请求均不相关的独立事务,因此通信由独立的请求和响应对组成。

You need to save the counter in a database. 您需要将计数器保存在数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM