Prevent User from navigating away from page

Adrian Räuchle Posted in Component Development 7 years ago

Hey guys,
When a user does his first login i want him to be redirect to a page were he should enter additional data.
This works well!

now i wrote a hook which listens on page:load event.

The Hook Handler looks the following:

function my_page_load($hook, $type, $return, $params)
{
if (ossn_isLoggedin()) {
    $subpage = $params['page'][1];

    if ($subpage !== 'address') {

        ossn_trigger_message(ossn_print('user:address:error'), 'error');

        $user = ossn_loggedin_user();

        $url = $user->profileURL('/address');
        header("Location: {$url}");
        exit();
    }
}

}

The redirection works, the user gets redirect to his address page. But the styles, javascripts are also prevented from being loaded.

Any idea how i can prevent user clicking away from the page and force him to enter his address and get the styles etc. being loaded?

Replies
Slovak Ondrej Janicko Replied 7 years ago

@Adrian

Hi Adrian,

are you already at home?

Can you publish example code for page redirecting after firs login as you promissed please?

Thank you very much.

Indonesian Arsalan Shah Replied 7 years ago

Hi try following, (you should post your code next time to gist)

function my_page_load($hook, $type, $return, $params) {
        if(ossn_isLoggedin()) {
                $subpage = $params["page"][1];
                $array = array(
                        'address',
                        'css',
                        'js'
                );
                if(!in_array($subpage, $array)) {
                        ossn_trigger_message(ossn_print("user:address:error"), "error");
                        $user = ossn_loggedin_user();
                        $url = $user->profileURL("/address");
                        header("Location: {$url}");
                        exit();
                }
        }
}
German Michael Zülsdorff Replied 7 years ago

Hey Adrian,

since this forum/editor isn't really made and meant for exchanging program code I'd recommend to put your complete stuff onto Github instead. It's much easier and more foolproof to add corrections in the right place via pull requests.

German Adrian Räuchle Replied 7 years ago

yes ondrej, will do it as soon as i am at home. then i will post you my working example

Slovak Ondrej Janicko Replied 7 years ago

@Adrian

I was tring to write something like you described but was not successful.

Can you post example code for page redirecting after first login?

Thanks.

German Adrian Räuchle Replied 7 years ago

@arsalan Sha: can u provide a simple code example for this? that would be great.
could you transfer this into the code i posted please?

Would help me a lot!

Slovak Ondrej Janicko Replied 7 years ago

@Adrian

Thanks.

Indonesian Arsalan Shah Replied 7 years ago

You need to make sure that $subpage !== "address" , 'js', 'css', 'action'.....

German Adrian Räuchle Replied 7 years ago

@ondrej:

this is quite easy to achieve.
you have to build a listener to ossnaddhook('user', 'login');

than in ur handler u get the current user with ossnloggedinuser(); on the user you have a field called last_login. if that is 0 than it is the first user login and than you could redirect it to a page like redirect('mypage');

Slovak Ondrej Janicko Replied 7 years ago

I can not help with component dev now but i am very interesting for such improvment like page redirecting after first login. Please do publis such component here on ossn website. Thanks in advance.