William Kheng
William Kheng

Reputation: 671

PHP URL Redirecting

I am working on a site that allows system user login. And I face some scenario below:

If let say User A clicked on an email link (https://www.example.com/user/1), User A will be redirected to the login page if haven't login or User A will be redirected to the email clicked URL if User A already logged in. The question here is that how to redirect User A to the email clicked URL right after login successful instead of https://www.example.com/home by default.

Correct me if I'm wrong.

Upvotes: 0

Views: 46

Answers (1)

Eliran Levi
Eliran Levi

Reputation: 135

There are 2 approaches to achieve it:

1. Sending the data in GET request: I see many (big) websites are doing that, just send a GET info to the www.example.com/home, let's say will look like that: www.example.com/home?redirect=email, you can send how many variables you want, you can also use hash to make it more secure and less readable for the user, you can choose this approach if you don't want to create a SESSION for the user for this tiny issue.

2. Saving the request in SESSION: if the user is not logged in, create a SESSION with the desired location to redirect, then get that SESSION variable in the /home through $_SESSION or through other methods (if you use a framework such as Laravel).

I suggest go with approach 1, it is very common. The reason I think it is better than approach 2 is because that this task is not what Sessions are for.

Upvotes: 1

Related Questions