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.

Can I access EE session variables when outside of EE?

October 09, 2007 9:12am

Subscribe [4]
  • #1 / Oct 09, 2007 9:12am

    Jeff Ricks

    99 posts

    I’ve been trying to wrap my head around session variables and understand how to use the $SESS global variable to access the session variables that are registered when logging into EE front door or back door. My question is, since $SESS is defined by EE code, I can’t use that variable when running outside of EE. Is there another way I can access the session variables, that are registered by EE, when I’m no longer running under EE?

    I’d like to be able to log into EE, then go to a non-EE website and do things according to the EE group assigment of the logged in person. Is that possible? If I know the username of the logged in person I know I can look up their account in the EE database to get their group assignment ID but I’d rather get that information from the session variable if possible. Is that possible?

  • #2 / Oct 09, 2007 2:09pm

    Jeff Ricks

    99 posts

    Can anyone give me a yes or no on my question? Maybe it’s a no brainer, i.e. one of the following:

    - Of course you can do that. Duh! :slaps forehead:
    - Of course you can’t do that. Duh! :slaps forehead:

    The problem is, I don’t know which is correct. I don’t necessarily need someone to show me how to do anything. At this point I just need to know it it’s possible or not, and I can take it from there. Yes, or no? Thanks much.  😊

  • #3 / Oct 09, 2007 6:29pm

    Gonzalingui

    13 posts

    Hey

    I’m new in this too. And I’m starting to work with session variables right now and searching I found your post. So I don’t think I have an answer for that but…

    You only need the group-id of the logged user? Does it matter if that ID is shown in the URL of all the links pointing to the non-EE site? example: http://example.com/another_website/GROUP_ID

    If you can do that, then you don’t need to pass session id’s or specific user data.

  • #4 / Oct 09, 2007 6:37pm

    Jeff Ricks

    99 posts

    You only need the group-id of the logged user? Does it matter if that ID is shown in the URL of all the links pointing to the non-EE site? example: http://example.com/another_website/GROUP_ID

    If you can do that, then you don’t need to pass session id’s or specific user data.

    Hey, that’s not a bad idea. That could work for me for my current need. I still would like to find out if I can access all the sessions variables outside of EE. I’ve been reading, and coding and trying all kinds of things but still haven’t found a way to do it. I get the feeling I’m overlooking something simple. If anyone can pont me in the right direction, I would sure appreciate it.

  • #5 / Oct 09, 2007 7:00pm

    Gonzalingui

    13 posts

    Hi again 😊

    The group_id idea is not bad, but can be a problem if you don’t want anyone to change that parameter in the URL to see the other “versions” of the website.

    A better solution would be if You pass the EE’s session_id via URL. Then validate the user by comparing the session_id, user IP, and user-agent with the information stored in the EE database’s exp_sessions table. Then you can get the member_id, and then get everything you need.

    Which session variables do you need to access?

  • #6 / Oct 09, 2007 11:28pm

    Jeff Ricks

    99 posts

    Well, I’m making progress on this. Now I know that it is possible to set session variables within EE and use them outside of non-EE programs. I placed the code below in an EE template and ran the code. It does the following:

    1. Test if session variable ‘count’ has been set. If not then assign it the value, 0.
    2. If it has already been set then increment it’s value.
    3. Echo its value.
    4. Exit.

    <?php
    
    session_start();
    
    if ( !isset($_SESSION['count']) )
    $_SESSION['count'] = 0;
    $_SESSION['count']++;
    echo 'count = '. $_SESSION['count'];
    
    ?>

    Then I ran the below code outside of EE, in a php file. It simply displays the value of ‘count’, if it’s already been set. Whatever the last was for count, when the EE template code was run, will be the value displayed by the php file.

    <?php
    
    session_start();
    
    if ( !isset($_SESSION['count']) )
    $_SESSION['count'] = 0;
    echo 'count = '.$_SESSION['count'];
    
    ?>

    So, this means that I should be able to read all of the EE session variables. I just need to know how to reference them properly when outside of EE. So, apparently the answser is, “Of course you can do that! Duh! :slaps forehead:

  • #7 / Oct 10, 2007 3:14am

    Gonzalingui

    13 posts

    Hi Jeff,

    I keep looking this topic because I’ve resolved something similar in EE, but because I was out of time I done it in a ugly way. That particular case doesn’t matter now because in a near future I’ll have to rework all that, and then I’ll try to do it well.

    Regarding to your solution, I never come up with an idea like that because I was misunderstanding the term “EE session variables”, for me that is something like “PHP Session Variables inside EE”.

    The progress you make is very interesting, I didn’t know that would work because I believed EE was like a bubble where all the PHP code you run will have a kind of “special scope”. Is good to know that those lines work.

    If you want to store information from EE, I suggest you to save it all inside a PHP array, and then save that array into $_SESSION, I think it’s much cleaner.

  • #8 / Oct 18, 2007 2:43pm

    Lisa Wess

    20502 posts

    Hi, Jeff,

    I’ve gone ahead and removed your post in the Technical Support forum.  This is really the appropriate forum for this thread; and it is definitely outside of the normal scope of the technical support forum (helping with technical issues inside of ExpressionEngine).

    Are you, in some way, calling ExpressionEngine into your static file?  There’s no reason those classes would be available to a static file - you’ll need some way to bring EE into the scope.

    I’d honestly consider a different approach, like bringing those static pages into ExpressionEngine.

  • #9 / Oct 18, 2007 2:44pm

    Derek Jones

    7561 posts

    I would just like to point out that cross posting “to give [a post] more visibility” may indeed draw attention to you, but not likely positive attention.  This is a PHP question, and to close the issue, I’ll go ahead and answer it, but please in the future refrain from cross posting just because you aren’t getting the response that you like.

    No, an external script cannot access variables or objects in another script (such as ExpressionEngine), unless it is running within that script.  This is what plugins, modules, extensions, and PHP within templates accomplish.  Standalone PHP scripts do not reside in memory, and cannot interact with one another.

  • #10 / Oct 18, 2007 3:36pm

    Jeff Ricks

    99 posts

    Sorry for the etiquette lapse. I won’t try that again. :cheese:

    I thought a session variable would be available to any script, whether an EE or a static script, as long as the browser isn’t closed (or the session cookie deleted).

    The reason I can’t run my static code in EE is because the static code is a main menu system that I want to work with both my EE pages and pages of other applications. My intent is to include the menu script php file into my EE templates and include it in the other application pages. The user will login via EE then will have menu items available based on the user’s group_id session variable setting (I guess technically it would be a semi-static menu script). I suppose once logged in I could write the group_id value to a cookie that my other application can read, but it seems to me there’s a cleaner way of doing this.

  • #11 / Oct 18, 2007 3:43pm

    Derek Jones

    7561 posts

    ExpressionEngine doesn’t use PHP sessions, which I think is where the confusion may be stemming, Jeff.  And I wouldn’t recommend using a cookie as you pose it; cookies are easily falsified and able to be manipulated, so using it alone as the basis for granting access would be unwise.

  • #12 / Oct 18, 2007 3:55pm

    Jeff Ricks

    99 posts

    ExpressionEngine doesn’t use PHP sessions, which I think is where the confusion may be stemming, Jeff.

    Yep, that’s where the confusion lies. I didn’t know that. Thanks for straightening me out.

    And I wouldn’t recommend using a cookie as you pose it; cookies are easily falsified and able to be manipulated, so using it alone as the basis for granting access would be unwise.

    It looks like I’ll have to use the cookie approach. The cookie group_id setting will make certain menu items available. Other more secure means are used to control actual page access. The menu might link them to a page, but if they aren’t supposed to have access, they’re not gonna get it.

    Thanks for the help Derek! I just needed someone to point me in the right direction. Looks like I was marching south when I needed to be going north.  :roll:

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

ExpressionEngine News!

#eecms, #events, #releases