0

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();
$this->_customerSession->logout()->setBeforeAuthUrl($this->_redirect->getRefererUrl())
            ->setLastCustomerId($lastCustomerId);
$value = $this->getCookieManager()->getCookie('mage-cache-sessid');

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

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

$logoutUrl = 'http://127.0.0.1/Magento2/frontendname/controllername/actionname';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $logoutUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
$curl_errno = curl_errno($ch);
if($curl_errno){
    echo $curl_errno.'--in error'; exit;
}
print_r($result);
curl_close($ch);

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)
{
    $this->_customerSession->logout();
    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

0

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

event:  customer_logout

In your custom module create

app/code/Vendorname/Modulename/etc/frontend/events.xml

Then create your observer in

VendornameModulename\Observer\Frontend\CustomerLogOutObserver.php

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
    }
}

check:

app/code/Magento/Customer/Controller/Account/Logout.php

/**
 * Customer logout action
 *
 * @return \Magento\Framework\Controller\Result\Redirect
 */
public function execute()
{
    $lastCustomerId = $this->session->getId();
    $this->session->logout()->setBeforeAuthUrl($this->_redirect->getRefererUrl())
        ->setLastCustomerId($lastCustomerId);
 ....

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()]);
        $this->_logout();
....
  • I need some functionality before login, so i override the logout and tried – senthil May 15 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 at 10:33
  • You should go with event observer, so that you can perform an action before logout. – Saphal Jha May 15 at 10:35

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.