Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:58:26+00:00 2026-05-23T13:58:26+00:00

In CakePHP when a user tries to access an action that is protected with

  • 0

In CakePHP when a user tries to access an action that is protected with the Auth component they are redirected to the login page.

So for example if I tried to acccess: domain.com/posts/add it would take me to domain.com/users/login

What I want to do is when anything like this happens is add a query string with the previous url like so: domain.com/users/login?back=/posts/add This is because I have disabled autoRedirect so that the redirect is no longer based on the session.

How would I add this query string though? Thanks

Just to confirm I know how to redirect the user using the query string in the login method just not how to send the query string in the first place when a user is sent to the login page from action that requires authorisation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:58:27+00:00Added an answer on May 23, 2026 at 1:58 pm

    I do it by accessing $this->Auth->redirect() in the login action, and parsing that value as a hidden param to the login form, check for it’s existence if the form is submitted, and send the user back to it.

    ie controller:

    function login() {
        if($this->Auth->user()) { // User got logged in
             if(!empty($this->data['User']['referer']) {
                 $this->redirect($this->data['User']['referer']);
             } else {
                  // Take them to dashboard or something instead
             }
        } else {
             // Set return_to to last page
         $referer = $this->Auth->redirect();
             if($referer == $this->here) {
             $referer = false;
             }
             $this->set(compact('referer') );
        }
    }
    

    And in your form something like $this->Form->input('referer', array('type' => 'hidden', 'value' => $referer ?: false); – Note PHP 5.3 ternary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small cakePHP app that allows for user's to login and be
How would I create a login link on a CakePHP page that contains a
I have built a cakephp app that needs the user to login or register(for
In my CakePHP 1.2.5 app, I have a Profile model that belongsTo a User
I am learning cakephp by myself. I tried to create a user controller with
I have a CakePHP model - User - that has ties to an external
I have built a very simple CakePHP website using the Auth component and have
I am trying to alter my edit action so that a user can only
Is it possible in cakephp that a user be registered automatically with a predefined
I have a CakePHP controller action where I would like to let the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.