0

我創建了CakePHP 3應用程序,女巫具有登錄功能。登錄功能在我的本地機器上完美運行。但是,在生產服務器上,當登錄憑證是corrcet時,它會將我重定向到「\」而不是我指定的URL。我相信這是Session的問題。我的意思是由於某種原因,會話在重定向線路上丟失了。

的代碼:

的AppController的Initilaize方法

public function initialize() 
{ 
    parent::initialize(); 

    $this->loadComponent('RequestHandler'); 
    $this->loadComponent('Flash'); 
    $this->loadComponent('Auth',[ 
      'authenticate'=>[ 
        'Form'=>[ 
          'fields'=>[ 
            'username'=>'username', 
            'password'=>'password' 
          ], 
          'finder' => 'auth', 
        ] 
      ], 
      'loginAction'=>[ 
        'controller'=>'Users', 
        'action'=>'login' 
      ], 
      'loginRedirect' => [ 
        'controller'=> 'Pages', 
        'action'=> 'dashboard' 
      ], 
    ]); 
} 

UserController.php:

class UsersController extends AppController { 


public function initialize() 
{ 
    parent::initialize(); 
} 

public function login() { 
    if ($this->request->is('post')) { 
     $user=$this->Auth->identify();  
     if($user) { 
      $this->Auth->setUser($user); 
      return $this->redirect($this->Auth->redirectUrl()); 
     } else { 
      $this->Flash->error(_('your username or password is incorrect, please try agian'), [ 
       'key' => 'falseSignIn']); 
     } 
    } 
} 
public function logout() { 
    return $this->redirect($this->Auth->logout()); 
} 

}

這裏是我的會話信息使用phpinfo(); enter image description here

+0

你能告訴我你定義loginredirect的應用程序控制器的初始化函數嗎?我在主要問題中添加了 – Aparna

+0

。請看看那裏 – Tanan

回答

0

返回的URL是按以下規則:

1.Returns從會議Auth.redirect值歸一化的URL,如果它存在,並且當前應用程序正在運行的同一個域。

2.如果沒有會話值並且存在config loginRedirect,則返回loginRedirect值。

3.如果沒有會話並且沒有loginRedirect,則返回/。

試試這個代碼:

public function initialize() 
{ 
    parent::initialize(); 
    $this->loadComponent('Auth', [ 
    'loginAction' => [ 
     'controller' => 'Users', 
     'action' => 'login', 
     'plugin' => 'Users' 
    ], 
    'authError' => 'Did you really think you are allowed to see that?', 
    'authenticate' => [ 
     'Form' => [ 
      'fields' => [ 'username'=>'username', 
          'password'=>'password'] 
     ] 
    ], 
    'storage' => 'Session' 
    ]); 
} 

您還可以使用簡單的方法:

$this->redirect(['action' => 'dashboard']); 
0

您需要創建一個隱藏的文本框和分配的$這個 - >值Auth-> redirectUrl()。然後,您可以從表單中檢索文本框的值,並將其作爲參數傳遞給$ this-> redirect()。