cancel
Showing results for 
Search instead for 
Did you mean: 

How to Manually (Programmatically) login a customer on the store

How to Manually (Programmatically) login a customer on the store

Hi,

 

For a couple of reasons, I had to create a login page (a PHP script if you will) autonomous from Magento, but in that page, I need to log the user in the Magento store (frontend) as well.

 

I have found dozens of answers to this question online, and most of them theoretically seem to work. But then when I try to access the customer "dashboard", it always redirects to the Magento default Login Page, so I assume that its not really working at all.

 

What I meant by "theoretically", is because I can return the user data (with "->getCustomer()") and if I check if the user is logged in (with "->isLoggedIn()") its returns true.

 

But then ill try to go to the dashboard page ("customer/account") and it redirects to the login page ("customer/account/login").

 

Can someone help me with this problem?

Is it maybe some Magento configuration that Im missing?

Can it be a Cookies/Cache problem?

 

Im using Magento 1.9 by the way.

 

Thanks in advance.

 

8 REPLIES 8

Re: How to Manually (Programmatically) login a customer on the store

Are you logging in a human or a machine?

 

A machine maybe use the the api.

 

A human could use off the shelf software if they are special needs.

 

If you are trying to do testing there are other solutions.  If you are doing pen-testing there are other solutions.

 

What are you really trying todo and can you show us the code?

 

Hope that helps.

Re: How to Manually (Programmatically) login a customer on the store

Hi jed, thanks for the reply.

 

Im not really understanding the Human vs Machine thing to be honest,

I am currently still testing, but I need to absolutly log in the Customer / Client, so yes, a human I guess.

 

The Magento Login Page asks for the email and the password. Im basically using the methods Ive seen online to "programmatically" do that myself, but on a different PHP page, a page of my own.

 

Ill give one exemple of a code I used that does what Ive explained on the OP:

 

Mage::getSingleton("core/session", array("name" => "frontend"));

$email = "MYMAIL@MAIL.COM";
$password = "MYPASS";
$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);
	$session->login($email, $password);
	echo "LOGIN: SUCESS";
}catch(Exception $e){
	echo "LOGIN: FAILED";
}

What Im trying to do, to put it simply, is stop using the Magento default PHP page for Login and use my own, which is exactly what I did in the case of the Create New Account page, where I have my own php page with a form, and I can create new users (clients) easily. 

 

Thanks,

Re: How to Manually (Programmatically) login a customer on the store

Hello,

 

Sorry for the bump, but I still havent figured out this issue.

Does anyone have an ideia of why this is happening?

 

Thanks,

Re: How to Manually (Programmatically) login a customer on the store

Hi terradasideias,


Could you please share the code for your extension to be able to help you? You can do it by attaching a ZIP file containing the files for your extension.


Best regards.
Gabriel

Welcome to the Magento Forums. Remember to introduce yourself and read the Magento Forums Guidelines.

Re: How to Manually (Programmatically) login a customer on the store

Hello,

 

I got it working, but only if I create the PHP file in the Magento root folder.

Its not what I wanted exactly but I can work with that Smiley Happy

 

Thanks,

Re: How to Manually (Programmatically) login a customer on the store

Hi @terradasideias,

 


Glad that you got it working.

Just to clarify, PHP scripts should be stored on the /shell/ folder to follow Magento conventions.

 

 

Best regards.

Gabriel

Welcome to the Magento Forums. Remember to introduce yourself and read the Magento Forums Guidelines.

Re: How to Manually (Programmatically) login a customer on the store

Hello Terradasdeias,

 

I am facing same issue when I try to login customer outside from magento login module.

 

Can you please share your working code with me;It would be very helpful for me.

 

Thanks,

Re: How to Manually (Programmatically) login a customer on the store

yes fixed my issue blogger at : PTCL Speed Test