As per my requirement i want to logout the customer using PHP curl.

for that I used the following code in my custom controller action(execute)

$lastCustomerId = $this->_customerSession->getId();
$value = $this->getCookieManager()->getCookie('mage-cache-sessid');

if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
   $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
   $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);

Then in my localhost i created curl.php with the following code

$logoutUrl = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $logoutUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
$curl_errno = curl_errno($ch);
    echo $curl_errno.'--in error'; exit;

When i run the curl file, it has hit the above logout page but the customer is not logout, but if i run the url directly on the browser the customer has logout.

Also tried this in controller

$pin = 'hitesh_12345678';
$customerData = $this->_customer->getCollection()->addAttributeToFilter('pin', array('eq' => $pin))->getFirstItem();
$customerId = $customerData->getData('entity_id');
if($this->_customerSession->getId() == $customerId)
    echo 'Customer logout successfully!'; exit;
} else {
    echo "Customer is not login"; exit;

But none of the logout functionality is not working.

share your thoughts and ideas for logout using CURL functionality.

Thanks in advance for your better idea and support

1 Answer 1


If you want to perform some action before logout then Create an observer before customer logout:

event:  customer_logout

In your custom module create


Then create your observer in


namespace Vendorname\Modulename\Observer\Frontend;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class CustomerLogOutObserver implements ObserverInterface

    public function execute(EventObserver $observer)
        //do stuff here



 * Customer logout action
 * @return \Magento\Framework\Controller\Result\Redirect
public function execute()
    $lastCustomerId = $this->session->getId();

Then app/code/Magento/Customer/Model/Session.php

 * Logout customer
 * @api
 * @return $this
public function logout()
    if ($this->isLoggedIn()) {
        $this->_eventManager->dispatch('customer_logout', ['customer' => $this->getCustomer()]);
  • I need some functionality before login, so i override the logout and tried
    – senthil
    May 15, 2019 at 10:25
  • Also i removed the override concept and tested the as per the Magento core logout action, but still the logout is not working
    – senthil
    May 15, 2019 at 10:33
  • You should go with event observer, so that you can perform an action before logout.
    – Saphal Jha
    May 15, 2019 at 10:35

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.