0

How can I pass the customer session and information from Magento to Codeigniter in same domain. In codeigniter I have created a function to check the customer session then if the customer is logged in then returning the customer information or else redirecting to magento customer login page if not.

In codeigniter Created a function as

public function loggedIn(){

    //$customerInfo = array();
    $dir = $_SERVER['DOCUMENT_ROOT'];
    $fullPath = $dir.'/magento/app/Mage.php';
    echo $fullPath;

    require_once( "$fullPath" );
    umask(0);

    // Initialize Magento
    Mage::app();

    // You have two options here,
    // “frontend” for frontend session or “adminhtml” for admin session
    Mage::getSingleton('core/session', array('name' => 'frontend'));
    $session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
    $customer_data = Mage::getModel('customer/customer')->getCustomer();
    if($session->isLoggedIn()){
    echo "Logged In";
    var_dump($customer_data);
    }
    else{
    echo "Logged out";
    header('Location:http://localhost/magento/customer/account');
   // Mage::app()->getFrontController()->getResponse()->setRedirect(""); 

    }

 }  

When customer is loggedIn in magento then it shows "Logged In" and for logged out user it is redirecting. However, it is not showing any logged in user information.

I had tried both-- getSingleton('customer/session') and getModel('customer/customer'); but it is not returning any value. And How can I use entire magento functionality in there. I am not able to use redirect function as well so just used PHP header function.

Thanks in advance

1

You are using a not getting the customer from the session. You are creating an instance of the customer model and calling a method that returns null. getCustomer is the same as getData('customer') on the customer model. This is always null.
Try it like this:

$session = Mage::getSingleton('customer/session');
if ($session->isLoggedIn()) { //if the customer is logged in
    return $session->getCustomer();
}
else {
    //the customer is not logged in
    return false;//or do what ever you want for this case
}
  • Thank you very much. Can you please explain a bit where I was wrong once again. if in place of customer session can I use customer model to check customer login and information? – MagentoBoy Jan 24 '14 at 7:43
  • Why customer model was returning Null values ?? :( – MagentoBoy Jan 24 '14 at 7:47
  • 3
    @MagentoBoy I just explained in the answer. Calling Mage::getModel('customer/customer')->getCustomer(); will call in Mage_Customer_Model_Customer::getCustomer, The method does not exist so you end up calling __call in the customer model. and getCustomer translates into getData('customer') that returns _data['customer'] which is empty because you have a clean instance of the model. You need to get the customer from the session. – Marius Jan 24 '14 at 8:00
  • ...Nice explanation.. Thank you and one more thing... When customer logged out I am redirecting to login page where I use the magento functions $url ="localhost/magento/customer/account"; Mage::app()->getFrontController()->getResponse()->setRedirect("$url"); but it is not redirecting and not showing any error as well. – MagentoBoy Jan 24 '14 at 8:30
  • Post an other question for this. It's off topic on this question. Also other people might find it easier. – Marius Jan 24 '14 at 8:33

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.