0

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.

<?php
function track_node_view($node, $view_mode) {           
if (user_is_anonymous()) {
  if($node->type == 'article' && $view_mode=='full'){
      static $counter = 0;
      if($counter <= 3) {
        $counter += 1;
        dsm($counter);
        drupal_set_message($counter);
      }
      else {
        drupal_goto("user/login");
      }
   }
  }
0

In my mind your code does the following:

An anonymous user shows an article. Then (why static, makes no sense) a counter is declared with 0. Then the counter increments to 1.

Then any other or the same user (because there's no saving in user session) clicks anywhere and looks an article. The counter is again set to 0, then to 1 etc...

I assume you want the following scenario:

An anonymous user comes to your site, clicks 3 articles, and when clicking to the 4th he shall be redirected to the login site.

So what do you need:

You need a session handler for anonymous user. But Drupal haven't got sessions for them. So here are the drupal functions for sessions: drupal user session

To start a session for anonymous user:

if(session_id() == '') {
  session_start();
}

Then in your module your counter comes to play a role, so start with your hook_node_view:

function track_node_view($node, $view_mode) {           
if (user_is_anonymous()) {
//Look if the counter is already set, if not create it
  if(!isset($_SESSION['track'])){
    $_SESSION['track'] = array();
  }
//Then look if the counter is in the session array(defaults to NULL)
  $tracker = track_session_get('track');
// if $tracker isn't available initialise your $counter
  if($tracker == NULL){
    $counter = 0;
  }
 // if session track is set and tracker != NULL you have to get the variable for counter
  if($tracker != NULL){
    $counter = $_SESSION['track']['counter'];
  }
 //now you should have a number between 1-3
  if($counter <= 3){
    $counter = ++$counter;
    //store this new value in session
    track_session_set('track',$counter);
  }
  else{
   //set the $counter to 0 and redirect
   $counter = 0;
   track_session_set('track',$counter);
   drupal_goto("user/login");
  }
 }
}

function track_session_get($session_name, $default = NULL) {
  if (isset($_SESSION[$session_name])) {
    return $_SESSION[$session_name];
  }
  return $default;
}

function track_session_set($session_name, $session_value) {
  $_SESSION[$session_name] = $session_value;
}

I did not tested this code, so this is only an advise, not a howto;)

  • Wow, thanks alot maen. Great effort. really grateful. working on it. Thanks again Sir. – Rakesh Verma Jun 19 '15 at 10:01
2

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()

  • I don't know how to work with session api, i tried but not getting anything. can you suggest me how should i start. – Rakesh Verma Jun 19 '15 at 9:10
  • Just write to $_SESSION['mymodule_counter'] ? – Mołot Jun 19 '15 at 9:14
  • should i need to write the code to create a session or just start with $_SESSION? – Rakesh Verma Jun 19 '15 at 9:24
  • See my updated answer. got to go now, bye. – Mołot Jun 19 '15 at 9:26
0

It is not working because that hook is normally invoked once for each page request, and at each page request, Drupal is getting bootstrapped; that means that once Drupal served the page request, every static variable is reset to its default value (in your case, 0). In your case, you should use variable_set() to set a Drupal persistent variable, and variable_get() to get its value.

Notice also that hook_node_view() should not redirect users like your code does; it is going to give you troubles with cron tasks. In fact, that hook is invoked even when a module is calling node_view() without any user watching a node page.

  • So how should i redirect users without hook_node_view? – Rakesh Verma Jun 19 '15 at 8:50
  • variable_set() is global, and he wants per-user data, right? May you elaborate how would you distinguish anonymous users, and why not to use sessions for this? – Mołot Jun 19 '15 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.