/    Sign up×
Community /Pin to ProfileBookmark

How to redirect user to the viewing page before logging in

Let say I have 2 pages and one is called page A which is the index page and the other page B and assuming the user has to login to page B before he can post comment or click on like button and the user is being redirected from page B to the logging page, how can the user be redirected back to page B after logging. That is the user should be redirected automatically back to page B instead of page A which is the index page.

P.s. I will prefer it if example is in MVC pattern

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@VITSUSANov 15.2018 — You can do this with the help of java scripts.
Copy linkTweet thisAlerts:
@ginerjmNov 15.2018 — Are you saying that the user is pointed to page b and does his login entries and hits the submit button. Your login script then runs and does the login (or not) and you want to send the user back to page b? A simple header() call will do that, but I have to ask - Am I understanding you correctly? Why send the user back to page b, ie, the login page, if he has done his login successfully?
Copy linkTweet thisAlerts:
@NogDogNov 15.2018 — One (of many?) ways is to do a redirect to the login page, including either a GET or SESSION parameter that tells the login page where to redirect the user upon successful login. It might be something like:
[code=php]
<?php
session_start();
if(empty($_SESSION['user_id'])) {
header('Location: https://example.com/login.php?from='.urlencode($_SERVER['REQUEST_URI']));
exit;
}
[/code]

Then in login.php you could use the value of $_GET['from'] for the redirect back to the original page if login is successful (and $_GET['from'] is not empty). You could stick that code in a function that would be included and called on any login-controlled page.
Copy linkTweet thisAlerts:
@rickysinghNov 15.2018 — In Chrome's developer tools you have to select "Resources" then under "Documents" or "Other" you can find the original request view the "Headers" to check a status code of 301.

You can also take the help of this article https://192-168-1-1ip.info/192-168-1-2/ also.
×

Success!

Help @oluwa86 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...