简体   繁体   中英

AuthComponent successfully redirects to login page when not logged in, but fails for some controllers and shows blank page?

I had a site in the local windows system made in cakephp. When I uploaded it to server (linux environment) the site works fine but some controllers are showing blank pages. The site works fine in local system though.

For example, when I type http://example.com/dashboard it correctly redirects back to login page but when I type http://example.com/settings it shows only blank page.

I have tried clearing tmp folder caches, renaming the controller to something else like Books, changing debug level, but neither any error shows up nor the page loads correctly. But error shows up on other pages if I make a mistake. So error reporting is working fine.

If I allow the controller action in the beforeFilter() using $this->Auth->allow() the page loads up but the codes like AuthComponenet::user('name') return empty values.

So the problem seems to be in the AuthComponenet which is successfully redirecting some pages to login and not redirecting some. And for those pages (or controllers) that it doesn't redirect it shows only a blank page irrespective of whether I am logged in or not. So what can be the possible error? The CakePHP version is 2.5.5.

Following is the AuthComponent initializing code in the AppController:

public $components = array(
    'Auth' => array(
                'loginAction' => array(
                                    'controller' => 'members',
                                    'action' => 'login'
                                ),
                'authError' => 'Did you really think you are allowed to see that?',
                'authenticate' => array(
                                    'Form' => array(
                                        'userModel' => 'Member',
                                        'fields' => array('username' => 'name','password'=>'password'),
                                        'passwordHasher' => array(
                                                            'className' => 'Simple',
                                                            'hashType' => 'md5'
                                                        )
                                    )
                                )
                ),
    'Session',
    //'DebugKit.Toolbar'
);

Behold the height of my stupidity...

So as it seemed there was some problem in the AuthComponent I tried var_dumping from the AuthComponent library codes step by step to detect the problem. While doing so I noticed a peculiar thing. When the text from var_dump() is output every controller behaved the same and returned a blank page. So it became apparent that the problem was due to presence of blank spaces somewhere in the controller, because in the view source mode also one blank line was getting output:

在此处输入图片说明

But I had thoroughly checked and made sure there were no blank spaces or lines after the end of the Controller code. Even there were no closing ?> php tags at the end to cause the error. So where could it be? Behold my stupidity that I had an empty line just above the beginning of the Controller codes .

在此处输入图片说明

Clearing the empty line in the beginning solved the problem. But alas! such a silly thing took out 3 hrs of my useless time :D

Moral of the story:

  • Make sure to check both beginning and end of files for empty space and lines when deploying code in linux systems.

  • Try some light code coloring schemes in the editor while debugging so that empty lines can be easily visible :D

Moral seems great but will be little better if we remove the php ending tag from all the files. Anyhow white space death occurs if you have white space on top before beginning php tag. But leaving the ending tag will make sure that if there is any white space it will not output to view some better saved partially :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM