Counter is always set to 1 and not incrementing I want Anonymous users to redirect to login page if they have read three articles as trail. I wrote this code but the counter on any article is always set to one.I guess its initializing again and again. Can anybody please help with code.

&lt;?php
function track_node_view($node, $view_mode) {           
if (user_is_anonymous()) {
  if($node-&gt;type == 'article' &amp;&amp; $view_mode=='full'){
      static $counter = 0;
      if($counter &lt;= 3) {
        $counter += 1;
        dsm($counter);
        drupal_set_message($counter);
      }
      else {
        drupal_goto("user/login");
      }
   }
  }
The problem is that you failed to grasp how HTTP is basically stateless.

static $counter = 0;


This value will be kept during one run. But one run is one page load. Obviously, it will be fresh and new at all page loads - ie all article reads.

If you need to keep value in one user session, see Session API, that's what it is for.

If you don't need any extra functionalities, just use $_SESSION['mymodule_counter']. If you want to make sure session is started, use drupal_session_start()