How does thinkphp determine whether to log on in each method?

Source: Internet
Author: User
[Tp version 3.2.3] I wrote a public class CommonController in the Controller. class. php, write a constructor to determine whether to log on. if you do not log on, redirect to the logon page: {code ...} there is a problem in this way, that is, the redirection will always be performed without logon. 127 errors... [Tp version 3.2.3]
I wrote a public class in the Controller. CommonController.class.php, Write a constructor to determine whether to log on. if you do not log on, redirect to the logon page:


  Redirect ('login/index'); return;} else {session ('islogin', $ islogin); // If you log on, refresh it? I don't know if I want to refresh it like this, because it may expire after I log on. }}}

There is a problem in this way, that is, the redirection will always be performed without logon. Error127.0.0.1 redirects you too many times.
So I changed it

Class CommonController extends Controller {public function isLogin () {$ isLogin = session ('islogin'); if (! $ IsLogin) {$ this-> redirect ('login/index'); return;} else {session ('islogin', $ islogin); // If you log on, refresh it? I don't know if this is the way to refresh }}}

Add a constructor to each of the other files that inherit the CommonController, as shown in figure
ArticleController.class.php


  Show ("hallo world ");}}

Error message:

Call to a member function display () on null error location FILE: X: \ domainX \ ThinkPHP \ Library \ Think \ Controller. class. php LINE: 69

In this case, no error is reported:


  Show ("hallo world ");}}

Problem:

  • Why can't classes inherited from CommonController. class. php be judged in _ construct?

  • If you need to determine whether to log on to the classes that inherit CommonController. class. php, you need to writeparent::isLogin();Is this reasonable? Or is it reasonable to do this?

Reply content:

[Tp version 3.2.3]
I wrote a public class in the Controller.CommonController.class.php, Write a constructor to determine whether to log on. if you do not log on, redirect to the logon page:


  Redirect ('login/index'); return;} else {session ('islogin', $ islogin); // If you log on, refresh it? I don't know if I want to refresh it like this, because it may expire after I log on. }}}

There is a problem in this way, that is, the redirection will always be performed without logon. Error127.0.0.1 redirects you too many times.
So I changed it

Class CommonController extends Controller {public function isLogin () {$ isLogin = session ('islogin'); if (! $ IsLogin) {$ this-> redirect ('login/index'); return;} else {session ('islogin', $ islogin); // If you log on, refresh it? I don't know if this is the way to refresh }}}

Add a constructor to each of the other files that inherit the CommonController, as shown in figure
ArticleController.class.php


  Show ("hallo world ");}}

Error message:

Call to a member function display () on null error location FILE: X: \ domainX \ ThinkPHP \ Library \ Think \ Controller. class. php LINE: 69

In this case, no error is reported:


  Show ("hallo world ");}}

Problem:

  • Why can't classes inherited from CommonController. class. php be judged in _ construct?

  • If you need to determine whether to log on to the classes that inherit CommonController. class. php, you need to writeparent::isLogin();Is this reasonable? Or is it reasonable to do this?

Public function _ initialize () {$ allow_actions = explode (',', C ('allow _ ACTIONS '); // Configure which operations can be accessed without logon, such as logon, verify login $ curr_action = MODULE_NAME. '. '. CONTROLLER_NAME. '. '. ACTION_NAME; if (! In_array ($ curr_action, $ allow_actions )&&! Is_login_admin () {// $ this-> redirect ('admin/Public/login') accessed after logon is not logged on ');}}

Do not redirect in the public class. in the public class, you can only determine whether to log on, return true or false, and then operate based on the returned results.

You wrote it at the program Portal.

In addition, write a login Guestcontroller, do not continue from commonController, just inherit the Controller directly, so that before login or exit login, it is handled by the Guestcontroller of guest. All sub-classes of commonController are processed after login.

Ideas
Perform logon verification in _ construct of CommonContrller. remember that parent :__ construct must be used in _ construct.

Then each Controller inherits this Common, of course, except your Login Controller, which inherits the Controller under Think.

In this case, you can try

The Login controller does not inherit. The public controller only determines whether to log on without writing logs. isn't that all right?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.