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.

EE variables and PHP input/output

December 02, 2011 11:41pm

Subscribe [3]
  • #1 / Dec 02, 2011 11:41pm

    struiling

    10 posts

    I’m trying to implement single sign-on. Don’t need to go into implementation details on that, but they want a BASE64 encoded JSON string of relevant user data. Like so:

    <?php
    $data = array(
           "id" => "{logged_in_member_id}",
            "username" => "{logged_in_username}",
            "email" => "{logged_in_email}"
    );
    
    $message = base64_encode(json_encode($data));
    ?>

    Except. Even though EE is setting the info in the $data array correctly (i.e. [id] => “1”), when it’s base64’d, it’s encoding [id] => “{logged_in_member_id}” as a string literal.

    Here’s a stripped down example code that I’ve found will work in any PHP-enabled template. I’ve tried on input and output (seems output should be the right one), but it doesn’t seem to make a difference.

    <?php
    $data = array(
           "id" => "{logged_in_member_id}",
            "username" => "{logged_in_username}",
            "email" => "{logged_in_email}"
    );
    
    $message = base64_encode(json_encode($data));
    
    print(json_encode($data) . "
    
    ");
    
    print($message);
    
    ?>
    
    
    
    
    If the base64 string matches this: 
    
    eyJpZCI6Intsb2dnZWRfaW5fbWVtYmVyX2lkfSIsInVzZXJuYW1lIjoie2xvZ2dlZF9pbl91c2VybmFtZX0iLCJlbWFpbCI6Intsb2dnZWRfaW5fZW1haWx9In0=
    
    
    
    it decodes to this (not what I want) : 
    
    
    <!-- spaced out to prevent EE from interpreting -->
    {"id":"{
    logged_in_member_id
    }","username":"{
    logged_in_username
    }","email":"{
    logged_in_email
    }"}

    This should be able to work, right? I’m on version 2.1.3. As a note, if you use the PHP base64_decode function and it decodes to {logged_in_username}, EE will interpret that and print the username. So I’ve been using a decoder website instead of the function.

    Any help is much much appreciated. Thanks!

  • #2 / Dec 03, 2011 2:14am

    @nnette

    109 posts

    My issue and yours are related.  How are you getting an EE tag to ever work within php?  I have my template setting to “Output” and it parses everything literal.

  • #3 / Dec 03, 2011 3:29am

    struiling

    10 posts

    Ah, I’m pretty sure it’s a parse order thing, where it turns out that there is a fair amount of EE tags that are still not processed until after PHP Output: http://expressionengine.com/wiki/Parse_Order/ . Including standard variables, which is what I really want in this case. Not sure how to get around this.

  • #4 / Dec 03, 2011 3:37am

    struiling

    10 posts

    I think one just has to get the info that’s from the standard variables directly with PHP. http://ellislab.com/forums/viewthread/82379/ . Funny how we both had the same problem right now!

  • #5 / Dec 03, 2011 6:07am

    @nnette

    109 posts

    Ok, I figured out my issue.  These two resources should help you:

    http://ellislab.com/expressionengine/user-guide/development/usage/session.html
    http://ellislab.com/expressionengine/user-guide/development/usage/database.html


    Try This:

    <?php
    $data = array(
            "id" => '".$this->EE->session->userdata('member_id')."',
            "username" => '".$this->EE->session->userdata('username')."',
            "email" => '".$this->EE->session->userdata('email')."'
    );
    
    $message = base64_encode(json_encode($data));
    ?>

     

    This is how I was using it:

    UPDATE `my_table` 
    SET `my_field` = `my_field`+".$_POST["data"]." AND `my_field_2` = `my_field_2`-".$_POST["data2"]." 
    WHERE `my_field_4` = '".$this->EE->session->userdata('member_id')."' 
    LIMIT 1;

    Hopefully this information helps you as well.  😊

  • #6 / Dec 03, 2011 4:22pm

    struiling

    10 posts

    Hallelujah! I did

    $data = array(
           "id" => $this->EE->session->userdata('member_id'),
            "username" => $this->EE->session->userdata('username'),
            "email" => $this->EE->session->userdata('email')
    );

    and all is well.

  • #7 / Dec 04, 2011 1:00pm

    Sean C. Smith

    3818 posts

    Hi struiling,

    Glad you got that worked out. Is there anything else that you need help with?

    @nnette, Thanks for the assist.

    Sean

  • #8 / Dec 05, 2011 3:32pm

    struiling

    10 posts

    All set, thanks.

  • #9 / Dec 05, 2011 6:42pm

    Sean C. Smith

    3818 posts

    Great, going to close this thread. Feel free to post again when needed.

    Sean

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

ExpressionEngine News!

#eecms, #events, #releases