We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to disable the view of a page if user is signed in and redirect back to the page where the user was on?

I want to disable the page if user is signed in and then redirect them back to the page he was originally in.

What I have:

public function indexAction()
{
    Tag::setTitle('Home');
    if($this->component->user->hasSession()){
        $this->view->disable();
        $this->response->redirect($this->request->getServer('HTTP_REFERER'));
    }else{
        parent::initialize();
    }       
}

But this gives me a redirect loop. Thanks for the help in advance.

You may need to add the option of an external source, event if it is internal, if you are getting a url.


    $this->response->redirect($this->request->getServer('HTTP_REFERER'), true);

https://docs.phalcon.io/en/latest/api/Phalcon_Http_Response.html

Thanks for the help. I tried this and it still gives me "This webpage has a redirect loop". Aside from doing it this way is there any other method suitable for disabling a page when user is signed in so he/she cannot go to that page when signed in?

You may need to add the option of an external source, event if it is internal, if you are getting a url.


   $this->response->redirect($this->request->getServer('HTTP_REFERER'), true);

https://docs.phalcon.io/en/latest/api/Phalcon_Http_Response.html

Sorry that didn't work.


I'm wondering if the redirect is sending them to the back you are trying to keep them from? If you display the value of $this->request->getServer('HTTP_REFERER') what does it show?

edited Aug '15

Hi, it gives me 2 things: the page the user was orginally on and the page I want to disable which is https://localhost/index

Sorry that didn't work.


I'm wondering if the redirect is sending them to the back you are trying to keep them from? If you display the value of $this->request->getServer('HTTP_REFERER') what does it show?

for me looks like you're redirecting to the current page, which of course will result in redirect loop. You should also check if HTTP_REFERER != from current page

Oh ok. I got another response that suggest the only way to do what I intended(user be redirected back to the page he was orginally in) is to store the page in cookies. Is that the only way to do it?

for me looks like you're redirecting to the current page, which of course will result in redirect loop. You should also check if HTTP_REFERER != from current page



16.0k
Accepted
answer

that will be the best way. HTTP_REFERER may be or may not be set and sometimes gives you the wrong page; is not reliable so is better to be avoided. I'm also using cookies for redirect since I know for sure where I'll be redirecting to don't end up with redirect loops or 404 redirects

I see thanks alot.

that will be the best way. HTTP_REFERER may be or may not be set and sometimes gives you the wrong page; is not reliable so is better to be avoided. I'm also using cookies for redirect since I know for sure where I'll be redirecting to don't end up with redirect loops or 404 redirects