0

I have tried something along the lines of

$email = ee()->....

and with a hope to include/require the page at the top of an external php page and then echo the value of $email.

This doesn't appear to work.

I need to gain access to the ee session class outside of the cms but still based on user logged in status.

How could i achieve this?

  • You'd have to include and instantiate the entire application - which raises the question, why does this have to exist in an external PHP file? A better route would be to create a module with an action, which can then be called via a direct link. – Derek Hogue May 8 '15 at 16:13
2

Well the short answer is you can'tshouldn't easily, it raises all sorts of questions regarding the security model!

However there may be alternatives. You could for instance write an extension for EE that exposes an ACTion (as Derek said), or more simply just write a template, and then get your information through AJAX/curl from your other external page.

for instance suppose you had a template living at API/getUserEmail, on that template you could do :

<?php
    //is the AJAX request from the known source?
    //here you can add code to ensure its just YOUR external site getting to this api
    if ( $legitRequest ) { 
        //is the user logged in
        $memberId = ee()->session->userdata('member_id');
        if ( $memberId != 0 ) {
            return json_encode(array('success' => ee()->session->userdata('email');));
        } else {
            return json_encode(array('error' => 'no user logged in'));
        }
    } else {
        return json_encode(array('error' => 'permission denied'));
    }
?>

Also, to get the ajax return with curl there's an example in the answer to this question : https://stackoverflow.com/questions/3928228/php-and-curl-get-ajax-data

Another alternative is to query the database directly, though you can't check if the user is logged in easily via a striaght database call.

If you wanted to go down the "instansiate the entire application" route check out the code in index.php (on the EE site) and try to replicate this bootstrap in your code, this could instansiate the EE object if you get it right, but that is a complicated affair!

| improve this answer | |
  • Hey! Thankyou this is almost what i have written in another trial almost line for line haha. Its a local dir for the same server so i will have a look at instantiating the class in the external document. That is ultimately my goal but finding info on it has proven difficult! thanks for your answer. – Derple May 8 '15 at 16:18
  • 1
    difficult to find for a reason mate, if you look at it from the 3rd person, essentually what you're trying to do is access the EE CMS via a file inserted onto a server. This has the same heuristics as an injection attack potentually. I'm sure your not trying anything dodgy but you can see why this would cause 'questions' :) However like you say if you have tried an AJAX sort of method you won't need to instansiate anything, just calling the URL www.eesite.com/api/getUserEmail will create the EE object for itself and just return you the good stuff! – Blatant May 8 '15 at 16:21
  • The system i am developing utilises the CMS 60% and 40% of the system is external. For several benefits and personal choice. Getting data one way is a breeze, but getting these key variables out in the first place is what i seek the solution for. – Derple May 8 '15 at 16:27
  • Your example of www.eesite.com/api/getUserEmail has enlightened me now feeling silly to have posted. funny how naturally i can complicate things sometimes. haha! – Derple May 8 '15 at 16:29
  • 1
    Happens to us all my friend :) Glad we could help, Just be sure to add a good security model to your api's so other people can't discover and use them to steal your users personal details! – Blatant May 8 '15 at 16:30

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.