Forum

Thread tagged as: Question, Members

How to control what page users are redirected to once logged in?

Is it possible to control what page users are served up once they successfully log in using the Members app?

Ed Pritchard

Ed Pritchard 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's down to how you put your site together. How do you have it structured?

I have a top-level navigation page that contains some standard description text about signing up for member-only access to resources. Then, on that same page have a login box in the sidebar. I want to make it so that once they enter valid credentials into the login box, and click log in, that they are directed to a hidden page that is not accessible via the top-level navigation.

Wouldn't you just check...

if (perch_member_logged_in()) {
     ...Redirect here
}

I am not sure if I missed something in your question so please excuse me if I have missed something.

Hi Robert, So would I just write my own redirect in PHP then? It seems like it would make sense to have some sort of parameter that can be passed into perch_members_login_form(); which could control the redirect upon successful authentication.

If writing my own PHP, is this something like what I should be looking to do: https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php?

Sorry Ed, Didn't see your question till now. I just tested a little function added to members runtime.php that redirects if logged in... If you are comfortable with a little coding here it is... ADD this function to /perch/addons/apps/perch_members/runtime.php

    function perch_member_logged_in_redirect($url='/') // redirects to front page if redirect not given
    {
        $Session = PerchMembers_Session::fetch();
        if($Session->logged_in) {
            PerchUtil::redirect($url);
        }
    }

THEN... ADD this bit of code directly after <?php include('perch/runtime.php'); ?> on members/index.php

<?php perch_member_logged_in_redirect('https://www.google.com'); ?>

Of course this is a very simple piece of code which you will need to tweak, and if you update the members app you will have to update the runtime.php too.

Ed Pritchard said:

If writing my own PHP, is this something like what I should be looking to do: https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php?

Ed, in this case I'm not sure this would work (of course I haven't tested) because perch has already begun outputting to the browser which means the header() has already been sent. For this reason, my example is placed right at the beginning of the page right after the call to perch runtime, before output to the browser has begun and if the member is logged in we use PerchUtil::redirect() for the redirect.

Hi Robert, I just wanted to thank you for your help with this. Although I went an alternative route to the redirect, it is helpful to have this answer out there.

Thanks again, Ed