Follow this Blog!

Oct 9, 2013

f Comment

[SOLVED] Yii: Fixing Infinite Loop on Login Page of Gii Module or Yii Code Generator. Yii cannot start Gii!

Amazon I used to be able to log into the Gii module, or Yii Code Generator, at http://localhost/index.php?r=gii to automatically generate code for controllers, models, views, modules, forms for my database tables. One day I realized I could no longer access the Yii code generator tool. How do I fix this problem?


When I go to localhost/index.php?r=gii I am redirected to localhost/index.php?r=gii/default/login. When I log in with the password I set in the configuration I am redirected to the login page again. I am caught in this infinite login loop.

To be specific I am 302 redirected from localhost/index.php?r=gii/default/login to localhost/index.php?r=gii/default/index, then 302 redirected from localhost/index.php?r=gii/default/index to localhost/index.php?r=gii/default/login.

Read on to find the solution to this nasty issue.

I need to use the Gii Module to generate code for controllers, views, models, modules, forms for my database models.


The cause of this problem is most likely that the PHP session is not maintained correctly, which can be due to cookies not being set correctly. Gii needs to use cookies to track PHP sessions. Does your web browser accept cookies from localhost? Some web browsers do not accept cookies from localhost. Chrome 20 is a case in point. localhost is technically a top-level domain and Chrome may not allow setting cookies on top-level domains.

To find out if this is the cause simply view the cookies of localhost and see if there are any cookies there.

Solution #1

If the cause of the Gii infinite loop issue is due to cookies not being saved in the web browser, simply use a domain that the web browser would accept cookies from. The domain can be anything because it is just going to be a synonym of localhost on your machine. Let's say the domain is

Open and edit C:\Windows\System32\drivers\etc\hosts with admin privileges to add this line:

Then use your browser to go to and try again. This time observe that cookies are set correctly. You should see a cookie named PHPSESSID.

Solution #2

Or you can use a browser that would accept cookies from localhost. As long as cookies are set correctly the PHP session can be maintained, and Yii and Gii will be able to track the PHP session and Gii will work correctly.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael