Authenticating a user directly from an email, although it is a very interesting feature from a user experience point of view, needs to be implemented with care and balance from a system security point of view.
I will present some approaches, which include some things that the other answers have already talked about.
Security Concern
As @Zuul mentioned, email link authentication is a major security issue. This includes two main reasons:
Link-based authentication is easy to intercept. The Urls you access are stored in logs and can be tracked in various ways. There are several ways a person can access the link and pass themselves by you. On the other hand, user authentication and password in forms, when used on a secure connection, are not stored and cannot be viewed by a third party who is monitoring traffic between client and server.
Emails can be answered and forwarded, mistakenly displaying the restricted link to third parties.
Even so, we can think of different ways to improve the user experience with direct links without compromising security so much.
Login with redirection
The most secure way that does not affect the user experience so much is to have the email link point to a page that performs the following procedure:
Check if the user is logged in. If the user is logged in will have a cookie or something that identifies him, right?
2.1. If authentication occurs, simply redirect the request to the e-mail link.
2.2 If the user is not authenticated, the system displays the login page. After the user login, the system should then redirect him to the email link.
In this approach, the original link can be stored in the URL itself. Examples:
This way after login it is possible to know which screen the user should be redirected to.
The authentication approach makes it necessary for the user to authenticate. However, the login can be maintained longer than the current browser session. For example, Evernote has a login option to "remember the user for a week". This means that the user would not need to authenticate for a week. Note that this is only feasible for private computers.
Login with token single
This is the approach mentioned by the user @gerep. The idea is to generate a single token per link so that no one can guess it and then invalidate the token after its use.
The problem with this approach is that the user will certainly try to repeat the same action some time later and will be denied access. This is good from a security point of view, but a bad user experience.
Limited login with token
Another alternative that some sites like Linkedin use is to generate a token that is not invalidated, however, it gives only restricted access to the system.
In the case in question, the token can give access only to the screen referring to the email. Or maybe to screens that are only data view.
If the user tries to access some action or editing in the system, then login complete would be required.
Considerations
Finally, define the level of security and, depending on the system, you can use a mixed set of approaches.
Okay @Zuul, got it. So these emails we receive from notifications from a system where we have registration, such as Free Market, which lets you know when someone bought a product from you and gives you a link to see the purchase, isn’t correct? Or do these companies have, use some secure way to do this redirect? Valeu.
– GustavoSevero
@Gustavosevero These emails will link to the web-site, but if the login is not active, the "normal" is to present the login page so that the user authenticates and then is directed to the correct page. If you already have active login, the web-site knows this and sends the user directly to the correct page. Override login for convenience is to throw away all application security.
– Zuul
Sorry @Zuul, that was it!!!! That’s right, how you can make this redirect, after the user logs in?
– GustavoSevero
@Gustavosevero Edited with an illustrative example of how to achieve the redirect.
– Zuul
Got it @Zuul, I’ll try.
– GustavoSevero
I edited the question and put the code of how I should do, to see if you’re right @Zuul.
– GustavoSevero
@Gustavosevero You should not edit the question, the existing answers lose their meaning and are no longer useful to the topic. You should create a new question about the new problem at hand so you can get help on this new subject without harming this topic.
– Zuul
get me your email?
– GustavoSevero
Guys, to "locked" on how to get the url from the email, and pass to the $_SESSION. Help @Zuul
– GustavoSevero
@Gustavosevero You do as I have in the example of the answer. If it is not clear or not solve your new problem, it is best to put a new question to deal with it and receive help from the community. Here in the comments it is difficult to solve...
– Zuul