Fixing Magento Login Problem after a Fresh Installation

This is just a quick little note to suggest two ways to solve the problem that you cannot log in to your Magento admin interface after a fresh install of Magento.

The Problem

The problem will manifest itself as a redirect back to the login screen, even though you typed the right username and password. If this problem is affecting you, you will be redirected back and see no error message. This indicates you have the right credentials, but the Magento Admin is just not letting you in. You can verify it by typing the wrong username and password, you’ll see you get redirected back and it shows an error message.

The problem occurs because the Magento backend tries to set a cookie on your browser, and then for some reason when you next make a request, the cookie is gone(or was never there). This makes Magento think you have never logged in, and of course it redirects you to the login screen. So the real guts of it is the missing cookie, we need to find out why it’s missing.

There are two solutions I have come across that will solve this, there may be others too, so please feel free to post them below. Both of these solutions have been suggested in the comments of my post on Setting up Apache Virtual Hosting.

Solution 1: Domain Name with no dots

This is the most common solution, if you have set up Magento to run locally (on MAMP for example) then you may be accessing the Apache webserver using the localhost hostname. A security setting in browsers means that the cookie will not be set, though apparently in FF3 at least, this behavior is a bug?.

So simply stop using localhost, you can use your localhost interface (e.g. 127.0.0.1 or 127.0.1.1). To determine your localhost interface you can look at the contents of your hosts file:

# Look for the number to the left of localhost
cat /etc/hosts

or your interface configuration.

# Look for interface loX with the LOOPBACK flag (probably lo0)
ifconfig

Once you know which number to use, you can replace localhost with the number. If you have already installed Magento using localhost then it will keep writing out links to localhost, even after you have changed to using the IP address, you will need to change the base_url values in the core_config_data table, you can run a query like this to find the right config values to change:

SELECT * FROM core_config_data WHERE value LIKE "%localhost%";

This should identfy two config values that will need to update with a query like:

UPDATE core_config_data SET value="http://127.0.0.1/" WHERE path IN ('web/unsecure/base_url','web/secure/base_url') ;

I’m going to assume you know to put the right value into that query, and not use the example one I have provided!

After changing that value you should delete your var/cache contents, and then refresh the page. Now you should have Magento running on an IP address, not a hostname with no dots in it. Of course you could always set up a fake domain name like www.testing.com by using a Virtual Hosting setup like I describe in my post on how to configure a MAMP Virtual host.

Solution 2: Timezone differences between server and client

One other, less likely problem, is that the cookie is being set, but expiring immediately. To check this you can inspect the cookies your browser is holding, and check if there is one there from Magento. If there is then check both the timezone your magento installation is using , and the one you have set locally, perhaps your local time is not set properly?

Hopefully one of these two solutions will get you back on track and able to log in to your newly installed Magento. Feel free to post problems or suggest other solutons in the comments below. I’m always more than happy to update my posts with helpful tips from readers.

You might also be interested in:

  1. First Happy Customer: Free Magento Installation reviewed
  2. How to Bulk Enable Products when Google Checkout is Disabled in Magento
  3. Magento CSRF attack: A Simple Explanation
  4. Magento Error: It is not safe to rely on the system’s timezone settings
  5. Top 3 Solutions when your Magento Categories are not displaying


Tagged as , , , , , + Categorized as Magento, Magento Installation, PHP, Web Development

21 Comments

  1. Just wanted to say your information on magento is a huge help. There aren’t many places that document it and I just wanted to say thanks! No need to reply, just smile and know that you and your site rock!

  2. I totally agree with Dan you information is great saved me a third reinstall
    You rock !

  3. Just wanted to say thanks. All I had to do was drop “localhost” and use my local IP.

  4. Sambir Shrestha says:

    Just wanna say thank you. That was really easy solution

  5. But I have this Problem also on a Webserver. This entries are set by my Domain in the database. I don’t know how to fix it. Any suggestions?

  6. Hi, thanks for commenting, you mean the cookie is not being set, or your domain name has no dots in it?

  7. There is no reason that Magento can’t set cookies on a local host with no dots in it. This is a bug in Magento. I do 100% of my development on various local hosts with no dots in them, and I can set cookies in them just fine. What’s going on here?

  8. Hi, Ryan

    Thanks for the feedback, I’m not sure what the answer is. I do know when I was investigating, the problem seemed browser dependent, and that the actual HTTP response coming back from Magento, appeared totally valid to me (i.e it was setting the cookie properly). The bug I found reported for FF3 (link in the post) seemed to make it seem a browser problem, maybe you luckily do 100% of your development in a browser that does not have this problem? Is this problem affecting you now only on Magento where it has not in the past?

  9. I was just re-reading the bug report, it looks like it may even be FF3 + Ubuntu, where the problem is – so it may be less of a problem to most users. I’d be keen to hear feedback on that.

  10. This really helped. Thanks for sharing

  11. After last upgrade magento, I have big problem with cookies, anybody have a solution?

  12. Can you describe the symptoms? What isn’t working?

  13. I reported the bug at https://edge.launchpad.net/ubuntu/+source/firefox-3.0/+bug/325266

    According to my findings it is due combination of PHP’s setcookie() call with the domain set AND the browser. Not only Firefox but Google Chrome is also affected, but not Opera (all under Ubuntu 9.04).

    Even if Firefox/Chrome somehow gets fixed in the future, it’d already be affecting many users. I think Magento should fix its setcookie() call and save all of us the trouble.

  14. I read somewhere that it was in an RFC that cookies only get set for FQDN’s and not just hostnames. With localhost being a hostname, that could be why localhost cookies do not work. I’m not sure of an RFC # though to confirm that, anyone got any clues?

  15. Further to that, setcookie() would just be telling the response to include a Cookie header – so nothing to fix there. It’s the fact the browser is not actually carrying out what it’s told to in the headers. I’m just not sure if that is a bug, or a security feature. Some vague reference to an RFC reckoned the latter.

  16. Here’s a solution i found really working. For those who want to try something else, this might be worth it. Check this site: http://markuzweb.blogspot.com/

  17. I already checked the site. It did solve the issue. Works great when you’re developing themes for Magento or building apps for it. Here’s how you get there directly: http://markuzweb.blogspot.com/2009/08/cant-login-to-magento-admin-panel-after.html

  18. thanks for helping solve my problem! the admin panel login was not going through after upgrade to 1.7. When i checked the cookies, found that cookies were not getting set at all.. all was fine when the cookie domain was reset in the core_config table to blank instead of the earlier .domain.com

  19. Hi all, i also have the same login problem once after installation of magento in locahost, but if you use opera browser you may not face this login problem.

Leave a Comment

Name:

Email:

Website:

Sporadic Tweeting...

  • New Post: Why I don’t use Flash, ever (and neither should you NZ Herald): Seriously, just don’t do it – I think I’... http://bit.ly/c9p3p9 1 week ago
  • New Post: A new ASchroder.com for 2010 or, why I haven’t written anything for a couple of weeks…: Tonight I releas... http://bit.ly/91NeAk 1 week ago
  • Don't skip the last session. One of the best is last at #irwd Stephan Spencer was insightful last year. Highly recommended. 3 weeks ago
  • More updates...

What I'm listening to

  • The xx - xx
  • Mos Def - The Ecstatic
  • Phoenix - Wolfgang Amadeus Phoenix
  • Jane's Addiction - Nothing's Shocking
  • Florence + The Machine - Lungs
  • Vampire Weekend - Contra
  • The Temper Trap - Conditions
  • The White Stripes - De Stijl
  • Grizzly Bear - Veckatimest
  • Passion Pit - Manners
  • Everclear - Sparkle And Fade
  • The White Stripes - White Blood Cells