PHP avoids cyclic redirection of logins

Source: Internet
Author: User

This is often the case when we write a website, and when you log in, you always say that your page contains the content of the cyclic redirection.

Let me show you a few common situations.

In thinkphp, if it is the background, you must log in to access the main management interface. Then we can do it this way.

Create a Commoncontroller universal controller in the controller, then write a

Public Function _initialize () {

Check if there is a session (' username ') content, if not then jump to the login page ...

No login, skip to login controller

$this->error ("Please login", U (' Login/login '));

}


Then all the controllers in the background have to inherit this commoncontroller, and write this function in each controller

Public Function _initialize () {

Parent::_initialize (); //must write this sentence, otherwise the subclass will replicate the parent class's method, so that the parent class validation operation is invalidated.

}

Have you ever considered such a problem here ....

If our login controller is Logincontroller, also inherit this commoncontroller, the problem comes, when you log in to any page in the background, will be commoncontroller in advance _ Initialize Check the login operation, no login, then jump to the login controller, after jumping to the login controller, and will call the Commoncontroller _initialize function: This is where the problem lies ...

Solution, either Logincontroller does not inherit Commoncontroller, or must write _initialize in Logincontroller ()

But do not execute the parent class's _initialize ()

Public Function _initialize () {

Do the things you want to do.

Do not use Parent::_initialize ();

}


Well, if it is the front desk, the front desk is divided into several situations. One is a must login, one is a part of the module needs to log in.

This is similar to the background for which you must log on.

Let's say that some of the modules need to be logged in.

The same way to use the background ...

What we might need is when I click on a module, check to see if I'm logged in, log in without logging in, and then jump into the module I just clicked.

The question here is how do we save the content we clicked on before?

There are a number of solutions, and the best way to do this is by using the Ajax method. The page has no dynamic commit, and we can bring in the login template directly in the corresponding module that needs to be logged in.

I wonder if everyone is aware of my ideas.

Is that we join into the module of my blog.

Public Function index () {

if (Not_login) {

$this->display ("Bring in the Login View");

Exit

}

else{

///////

}

}


Of course, compared to our login controller after the successful login do not jump .... Direct return login is successful.

Public Function login () {

if (success) {

$this->success (' Login successful! ');

}

}

All right, it's done.

This is the implementation of PHP, in the Laravel framework or other modules, we have to consider these issues ...

We use filters to solve this problem, and again, let's not add this filter to the login route.

The other thing is, if you write this in a filter,

if (detection succeeded) {

Jump to Home page.

}else{

Jump to login ....

}

There's a problem with this piece. If you jump to the home page, good, the first time the program detects that you are not logged in, and then jump to the login screen, when you are finished,

The system will jump to the homepage ...

In the first page of the controller, will also determine whether to log in, found login, and jump to the homepage ....

This is also a cyclic redirection ...

Solution, do not write success in the filter, the direct detection failure is not OK.

If you want to succeed directly to the home page, and its login there to write, if the login success, on the unified jump to the home page.


Well, I hope you can read it.

Persistence makes life valuable.

Best Wishes.



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.