Jump to content
benbyf

stuck redirecting url after login

Recommended Posts

Sure this has been asked a million times but cant find a simple answer.

Trying to set the page to redirect to after login in the front end - i.e. not on the admin login page.

some reason this works for super users but not normal users redirecting to a page which is viewable by members not guest role users.

 

$showLoginError = false;
    if($input->post->user && $input->post->pass) {
    	$username = $sanitizer->username($input->post->user);
    	$pass = $input->post->pass;

        try {

			$session->login($username, $pass);

			// redirect user
			$session->redirect("/events/");

        } catch (\Exception $e) {
            $showLoginError = true;
        }
    }

 

Share this post


Link to post
Share on other sites

But what happens with the normal users?

Can normal users view the page "/events/" ? Is the url correct? Can you get the url by API instead?

Share this post


Link to post
Share on other sites

In my _init.php file, the first thing I include is _login.php:

<?php

// Handle logouts
if(isset($input->get->logout)) {
    $session->logout();
    $session->redirect($page->path);
}

// If they aren't logged in, then show the login form
if(!$user->isLoggedin()) {

    // check for login before outputting markup
    if($input->post->user && $input->post->pass) {

        $user = $sanitizer->username($input->post->user);
        $pass = $input->post->pass;

        if($session->login($user, $pass)) {
            // login successful
            $session->redirect($page->path);
        } else {
            $session->login_error = 'Login Failed. Please try again, or use the forgot password link below.';
        }
    } ?>

    <!DOCTYPE HTML>
    <html lang="en">
    <head>
        <title>Login</title>
        <link rel="stylesheet" href="/build/css/app.css">
    </head>
    <body>
        <div class="container py-5">
            <form action='./' method='post'>
                <div class="row">
                    <div class="col-12 col-sm-6 offset-sm-3">
                        <h1>You must be logged in.</h1>
                        <div class="login">
                            <? if($input->post->user && $input->post->pass) {
                                echo "<p class='error'>" . $session->login_error . "</p>";
                            }?>
                            <p><input class="form-control" type='text' id="user" name='user' placeholder='Username'/></p>
                            <p><input class="form-control" type='password' id="pass" name='pass' placeholder="Password" /></p>
                            <p><input type='submit' class="btn btn-primary" name='submit' value='Login' /></p>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </body>
    </html>

    <? die(); // don't go any further if not logged in

}

If I go to site.dev/whatever, I am met with a login. After logging in, it takes me straight to the URL I intended to go to before needing to log in.

You could change the $page->path to $pages->get('/events/')->httpUrl; - would that work?

  • Like 1

Share this post


Link to post
Share on other sites

thanks @ethanbeyer. its so unbelievebly strange, it must be server settings as it works fine as I have currently on dev but not on live. Thanks anyways though!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By NorbertH
      When saving a page with the "order" template in the backend  IftRunner is nice to me and sends the order via page action into my accounting software. 
      When saving "order" in a frontend page it does nothing.   So i certainly missing something . Any ideas? 
       
    • By angelo, italy
      Hi guys,
      I've always used WP but I want to swtich to PW. I'm not sure ....
      I'd like to know if it's possible to create a website for an online photo contest.
      The participants of the competition could create their own account, in which they upload their photos. The photos uploaded remain visible only to themselves and the judges.
      From their account they can make the "entrance fee" payment.
      The judges of the competition can create their own account... entering they see the photos of the participants and vote photos
      At the main page I imagine the title of the competition, a button to read the regulation, and a button to register.
      The website should be in Italian and English.
      Thank you!!
       
       
    • By Orkun
      Hi Guys
      How can I make a redirect inside the .htaccess to my custom maintenance.html file when any URL of my Website is accessed except the processwire admin (www.example.com/processwire/).
      Because I want that my User's still can access the website when they are loggedin in Processwire.
      When the current url starts with /processwire or if there is a processwire-login-cookie (Is there a cookie when user is logged in Processwire?) available the redirect should not work. Otherwise it should work.
      How can I achieve this?
    • By Marcel
      Hey,
      I am about to use the tool Create Users Batcher. We have 450 users. When I tryed it with like 20 test users it worked but it took a while. Now my concerns are that 450 might be to stressful? Does anyone have experience with several hundreds of users? My thoughts are to split it in smaller goups and upload them one group after another.
      best wishes
      marcel
    • By SwimToWin
      ProcessWire is setting a "wires" cookie for each guest session.
      Is it possible drop that cookies, so there are no cookies at all for guests?
      That way, I don't need to spam the user with a cookie consent box.
      I don't need cookies for user preferences and marketing purposes.
      (Why are cookies being set by default in the first place?)
×
×
  • Create New...