Magento: log in and go to the account page from outside magento

I use the code below to login and redirect to the account page:

<?php include('store/app/Mage.php'); Mage::app(); if($_POST && $_POST['login']['username'] && $_POST['login']['password']){ $email = $_POST['login']['username']; $password = $_POST['login']['password']; $session = Mage::getSingleton('customer/session'); try { $log = $session->login($email, $password); $session->setCustomerAsLoggedIn($session->getCustomer()); $customer_id = $session->getCustomerId(); $send_data["success"] = true; $send_data["message"] = "Login Success"; $send_data["customer_id"] = $customer_id; Mage::getSingleton('customer/session')->loginById($customer_id); Mage_Core_Model_Session_Abstract_Varien::start(); }catch (Exception $ex) { $send_data["success"] = false; $send_data["message"] = $ex->getMessage(); } }else { $send_data["success"]=false; $send_data["message"]="Enter both Email and Password"; } echo json_encode($send_data); ?> 

And then in the file where I am making the ajax request, I use this code:

 if(data.success){ window.location = "http://domain.com/store/customer/account/" } 

But it always displays the user as logging out, although I get the correct client ID as well as success.

+6
source share
2 answers
 $email = strip_tags($_GET["login"]); $password = strip_tags($_GET["psw"]); function loginUser( $email, $password ) { umask(0); ob_start(); session_start(); Mage::app('default'); Mage::getSingleton("core/session", array("name" => "frontend")); $websiteId = Mage::app()->getWebsite()->getId(); $store = Mage::app()->getStore(); $customer = Mage::getModel("customer/customer"); $customer->website_id = $websiteId; $customer->setStore($store); try { $customer->loadByEmail($email); $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer); if($session->login($email, $password)){ return true;} else { }; }catch(Exception $e){ return $e->getMessage(); } } if (loginUser($email,$password) == 1) { echo ".. user loged as ".Mage::getSingleton('customer/session')->getCustomer()->getName()."<br>";} else { //bad things goes here } 
+1
source

In my case, Martin code works if I change the session name

 session_name('frontend'); session_start(); 

If you leave only the session name, the default value is PHPSESSID, which does not match the name created by Magento when manually logging in and does not work in my installation. This may change, try logging in manually and checking your cookie names.

documentation session_name: http://php.net/manual/en/function.session-name.php

+1
source

Source: https://habr.com/ru/post/988412/


All Articles