ankh2054 Posted May 15, 2014 Share Posted May 15, 2014 Hi all, I am wondering what code to use to redirect to the previous page requested, I have a profile page, which redirects to the login page. But once the use logs in I wold like to redirect back to profile page (or any other page that requires the user to be logged in) Profile.php // if user isn't logged in, forward to login page if(!$user->isLoggedin()) { $session->redirect("/login/"); } Login.php if($session->login($user, $pass)) { $session->redirect($page->path); } The above is wrong I know, but not sure how to get the previous page URL. if I output $page->path I get the login page. thanks Link to comment Share on other sites More sharing options...
adrian Posted May 15, 2014 Share Posted May 15, 2014 This is my version of Ryan's starter: $requested_pid = (int) $input->get->pid; $t = $pages->get($requested_pid)->path; if($user->isLoggedin()){ if($requested_pid == ''){ $session->redirect('/'); } else{ $session->redirect($t); } } if($input->post->username && $input->post->pass) { $username = $sanitizer->username($input->post->username); $pass = $input->post->pass; $u = $users->get($username); if($u->id && $u->tmp_pass && $u->tmp_pass === $pass) { // user logging in with tmp_pass, so change it to be their real pass $u->of(false); $u->pass = $u->tmp_pass; $u->save(); $u->of(true); } $u = $session->login($username, $pass); if($u) { // user is logged in, get rid of tmp_pass $u->of(false); $tmp_pass = $u->tmp_pass; $u->tmp_pass = ''; $u->save(); // now redirect to the profile edit page if($tmp_pass == $pass){ $session->redirect('/profile/'); } else{ $session->redirect($t); } } } // present the login form $headline = $input->post->username ? "Login failed" : "Please login"; $content = " <h2>$headline</h2> <form action='./?pid=".$requested_pid."' method='post'> <p> <label>Username <input type='text' name='username'></label> <label>Password <input type='password' name='pass'></label> </p> <input class='bluebutton' type='submit' name='login' value='Login'> </form><hr /><br /> <p><a href='/reset-pass/'>Forgot Password?</a></p> "; Then wherever you have a link to login: <a href="/logout/?pid='.$page->id.'">Login</a> Of course you can do the same with logout too. Hope that helps 4 Link to comment Share on other sites More sharing options...
ankh2054 Posted May 16, 2014 Author Share Posted May 16, 2014 thanks Adrian, I have amended my login.php but not sure how I can use the login link in my profile page, as it automatically re-directs a user, instead of the user actually pressing the login page. Link to comment Share on other sites More sharing options...
adrian Posted May 16, 2014 Share Posted May 16, 2014 Not sure I understand, but if you want to redirect from your profile edit page back to the last page they were on, then the initial link to the profile page should include the ?pid= and then would want the same redirect code in your profile edit page to grab the page id and use that for the redirect. Does that make sense? Sorry, in a bit of a rush. Link to comment Share on other sites More sharing options...
ankh2054 Posted May 17, 2014 Author Share Posted May 17, 2014 Hi Adrian, Thanks for your help, what I would like to happen is the following. If a user not currently logged in tries to access the profile page, it would redirect them to the login page, then once logged in, I would like it to redirect back to profile page. profile.php $requested_pid = (int) $input->get->pid; $t = $pages->get($requested_pid)->path; // if user isn't logged in, forward to login page if(!$user->isLoggedin()) { $session->redirect("/test/"); } login.php $requested_pid = (int) $input->get->pid; $t = $pages->get($requested_pid)->path; //if user not logging if with tmp_pass proceed with standard login try { if($session->login($user, $pass)) { $session->redirect($t); } Link to comment Share on other sites More sharing options...
adrian Posted May 17, 2014 Share Posted May 17, 2014 So then in your profile.php redirect you need: if(!$user->isLoggedin()) { $session->redirect('/login/?pid='.$page->id); } Link to comment Share on other sites More sharing options...
bwakad Posted May 18, 2014 Share Posted May 18, 2014 Or, as I did it, just use the menu to output only certain links if logged in: if (!$user->isLoggedIn()) { // menu link login here // menu link register here } else { // menu link profile here } for no direct access through url: on the profile page - if not logged in - session redirect to login, OR make it not appear in search/etc via admin settings. on the login page - after succesfull login - use session redirect to profile Link to comment Share on other sites More sharing options...
onjegolders Posted May 18, 2014 Share Posted May 18, 2014 You could also set a $session variable at the top of every page (or header.inc) and then call that. $session->prevPage = $page; Courtesy of Diogo, 2 years ago https://processwire.com/talk/topic/1257-non-admin-users-login/?p=11250 3 Link to comment Share on other sites More sharing options...
Craig Posted May 18, 2014 Share Posted May 18, 2014 From a security point of view, it would be better putting the requested page to redirect to it in the session anyway. Otherwise, malicious users could send people to the wrong page just be specifying another ID in a link. Link to comment Share on other sites More sharing options...
ankh2054 Posted May 20, 2014 Author Share Posted May 20, 2014 thanks all I will give t You could also set a $session variable at the top of every page (or header.inc) and then call that. $session->prevPage = $page; Courtesy of Diogo, 2 years ago https://processwire.com/talk/topic/1257-non-admin-users-login/?p=11250 thanks that worked perfectly Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now