ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Anonymous User

November 18, 2011 12:26pm

Subscribe [2]
  • #1 / Nov 18, 2011 12:26pm

    anonymous162744

    58 posts

    I am using an unreleased version of Brian Litzingers Anonymous User plugin for ExpressineEngine 2. The reason is because I want users to be able to maintain favorites (using Solspace favorites add-on) during a session without needing to login.

    https://github.com/litzinger/Anonymous-Member

    It seems to be creating the user and saving the favorite properly, the problem is that it’s not recognizing the session, and creating a new user on each refresh, when it should retain that newly create user. I’m not an EE developer so I was hoping someone could take a quick look at the code on github and let me know if they see what needs to be changed to fix the issue. This was a plugin for EE1, so I’m wondering if it’s just a coding convention from EE1 that needs to be changed?

    Any help would be greatly appreciated.

  • #2 / Nov 20, 2011 6:01am

    miketwalker

    1 posts

    We ran into this same issue, the latest EE update seems to have made undocumented changes to the way EE handles members and has broken that.

    Our temporary solution was to take from that module the base code, seed it with our own password, and then login the user via AJAX. Not perfect, but it works for now.

    <?php
     $salt = '';
     $screen_name = 'Anonymous Member';
     $group_id = '10';
     $temp_username = 'anonymous_'.$this->EE->functions->random('encrypt');
     $temp_pass = $this->EE->functions->hash("anon_password");
      
     // Create Anonymous User 
     $this->EE =& get_instance();
     $this->ip = $this->EE->input->ip_address(); 
     
     $data['username']    = $temp_username; 
     $data['password']    = $temp_pass;
     $data['ip_address']  = $this->ip;
     $data['unique_id']   = $this->EE->functions->random('encrypt');
     $data['join_date']   = $this->EE->localize->now;
     $data['email']       = '[email protected]';
     $data['screen_name'] = $screen_name;
     $data['group_id']    = $group_id;
     $data['accept_messages'] = 'n';
     $data['accept_admin_email'] = 'n';  
     $data['accept_user_email'] = 'n';
     $data['notify_by_default'] = 'n';
     $data['notify_of_pm'] = 'n';
     $data['display_avatars'] = 'n';
     $data['display_signatures']  = 'n';
     $data['smart_notifications'] = 'n';
     
     $this->EE->db->insert('members', $data);
     
     $member_id = $this->EE->db->insert_id();
      
     $json = array();
     
     $json['username'] = $temp_username;
     $json['password'] = 'anon_password';
     
     // encode array $json to JSON string
     $encoded = json_encode($json);
      
     // send response back to index.html
     // and end script execution
     die($encoded);
    ?>

    This code makes a new user, and returns a JSON object with the username and password to login with. In this case, the password is static but you can change anon_password to be dynamic if you wish.

    We call this code via AJAX at the top of the page we’re using it (basically put this in place of where you have the anonymous member module called now) to call the PHP from above.

    {if !logged_in}
    [removed]
    
    $(document).ready(function() {
     $.getJSON("PUT YOUR BASE URL HERE/create_anonymoususer", function(json) {
      var creds = "username=" + json.username + "&password;=" + json.password+ "&ACT=9&RET=-2&site_id=1&auto_login=1";
      $.ajax({
         url: "/index.php?ACT=10",
       success: function(){
        logmein(creds);
       }
      });
     }); 
    });
    
    function logmein(variables){
     $.ajax({
       url: '/index.php',
       type: "POST",
       data: variables,
       success: function() {
        location.reload();
       }, 
     });
    }
    [removed]
    <div id="response"></div>
    </html>
    {if:else}
    // Remainder of page here
  • #3 / Nov 20, 2011 12:15pm

    anonymous162744

    58 posts

    Does anyone know what changed with the sessions and how to fix the plugin? Supposedly people had it working before, I’m sure it’s not that difficult a fix. It seems like ExpressionEngine isn’t adding the session to the database if that helps.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases