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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      Despite my searches of the forum I'm somewhat confused about how to create new child pages on the frontend when a user clicks on a button on the parent page. I also have an equivalent button that is intended for uploading a .csv file to automatically create multiple new pages. This basically relates to a club (parent) and members (child) template configuration. Hopefully this explanation makes sense.
      A button should be able to launch the code needed to initiate the script required to create a new page using something like:
      <a href="/path/page.php">New +</a> <a href="/path/page.php">New ++</a> Does the code to create the new page or new pages need to be run from the template file for the child or the parent?
      A new individual member page will need to be editable manually at the point of page creation as well as subsequently, whereas multiple new pages will need to be editable after they have created and populated with data, again, as well as subsequently.
      I would very grateful for any advice or pointers as to how to achieve this.
       
    • By ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I've setup a page where a member can edit contact details via the frontend displaying the field content using the <edit> ... </edit> tags.
      This works fine when the fields actually contain data. However fields that contain no data (i.e. empty) do not appear to be editable. No edit cursor appears (- possibly owing to the field width being 0px?)
      Is the only solution to recreate the page using a form, for example,  or is there a simple way to allow blank fields to be editable on the frontend?
      I wondered if anyone else has found a solution to this problem. Any assistance would be appreciated.
    • By ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I'm trying to automatically redirect a logged-in user to a custom profile page using $session->redirect() and need to add $user->name to the redirect path.
      All my attempts appear to have failed:
      $session->redirect('/user-profile/')->name; $session->redirect('/user-profile/')->$user->name; $session->redirect('/user-profile/' . get($user->name . '/')); $session->redirect('/user-profile/' & get($user->name)); Can anyone point out where I am going wrong?
    • By cosmicsafari
      Hi all,
      This is my first foray into a multi lingual PW site, what i'm unsre about is whether the frontend output is translated automatically or do I have to provide the content in another language also?
      As it stands I have installed the following modules:

      And have created and uploaded the Chinese language pack.

      At this point I can successfully change my profile and the backend successfully displays in Chinese.
      However I am confused about how I now get that translation to work on the frontend?
      I have setup the alternative url for the page I wish to view in Chinese

      So as a basic test I tried adding the following into the template used for the page above.

      However both urls still display the message in English?
      I take it I have missed something, do I then need to enter the frontend output in Chinese in another field within the backend to enable Chinese url to output the content?
      Any ideas would be greatly appreciated.