1

I've create a login function that make the customer login by email and password and return below response

{
    "status": "success",
    "message": {
        "id": "1",
        "name": "username",
        "firstname": "firstname",
        "lastname": "lastname",
        "email": "[email protected]",
        "session_id": "7ead152bcd78c30b3f7415c985ab3d0b",
        "info": {
            "postcode": "1300",
            "city": "city",
            "street": "street",
            "telephone": "1234567890",
            "fax": null,
            "country": "USA",
            "region": null
        }
    }
}

So i want to get the customer data from the session id above but it's not working.

$sess = Mage::getSingleton('customer/session', array( 'value' => $sessId ));
print_r($sess);

My Questions:

1- How i can get the customer data from session id?

2- how i can logout the customer from the session id?

NB: This request from mobile app.

2 Answers 2

0

Get the customer data from session:

if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    echo "<pre>";
    print_r($customer->getData());
    echo "</pre>";
}

Logout from session:

require_once 'app/Mage.php';
Mage::app('default');
Mage::getSingleton("core/session", array("name" => "frontend"));
// get login customer data
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customerData = Mage::getSingleton('customer/session')->getCustomer();
     //echo $customerData->getId();
}

//For Logout Customer
Mage::getSingleton('customer/session')->logout();

$data['responseCode']='1';
$data['msg']="Logged Out Successfully";

$data2=json_encode($data);
echo $data2;
0

You can get the customer data from sessions with the following code but this is valid if you are using Magento 2.

Using object Manager

$om = \Magento\Framework\App\ObjectManager::getInstance();  
$customerSession = $om->get('Magento\Customer\Model\Session');  
$customerData = $customerSession->getCustomer()->getData(); //get all data of customerData
$customerData = $customerSession->getCustomer()->getId();//get id of customer

if you are using Magento 1:

 if (Mage::getModel('customer/session')->isLoggedIn()) {
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    echo '<pre>';
    print_r($customer->getData());
} else {
    echo 'Not Logged in';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.