2011-12-10 18 views
0

私はこのウェブサイトを以前は普通のPHPであったケーキのphpで終わらせようとしていますが、cakephpの世界では初心者でした。セッションをリダイレクトしないようにしてください

1)。私がログインをクリックすると、登録がある場合を除いて、アプリケーションコントローラにリダイレクトが指定されていませんが、別のページに移動します。ログインは一番上にあり、すべてのページから見ることができますが、ログインをクリックするとログインページ(私は欲しくない)にリダイレクトされます。

2.)私はログインした後、「username」というユーザー名でセッションを開始しますが、別のページに行くと、セッションを忘れてログインフォームの入力を取り戻すようですページの上部に表示されます。ここで

は私のコード

app_controller

<?php 
class AppController extends Controller {  

    var $helpers = array('Html', 'Form', 'Javascript', 'Session'); 
    var $components = array('Auth', 'Session'); 

    function beforeFilter() { 
     $this->Auth->allow('add','get_categories','get_home', 'get_others', 'pages'); 
     $this->Auth->autoRedirect = false; 

     } 
} 
?> 

がUserController

<?php 
class UsersController extends AppController { 

    var $uses = array("User"); 
    var $components = array('Auth', 'Session'); 


    function index() 
    { 
     $this->set('users', $this->User->find('all')); 
     $this->layout = 'master_layout'; 
    } 

     function add() { 

      if (!empty($this->data)) { 
      //pass is hashed already 
      //->data['User']['password'] = $this->Auth->password($this->data['User']['password']); 
      if ($this->User->save($this->data)) { 
       $this->Session->setFlash('Your were registered!.'); 
           $this->redirect(array('action' => 'get_home')); 
      } 
      } 

     $this->layout = 'master_layout'; 
     } 

    //IF THE DATABASE IS SET UP CORRECTLY CAKE AUTHENTICATES AUTOMATICALLY NO 
    //LOGIC IS NEEDED FOR LOGIN http://book.cakephp.org/view/1250/Authentication 
    function login() { 

     $this->layout = 'master_layout'; 

       if ($this->data) { 

        if ($this->Auth->login($this->data)) { 

        // Retrieve user data 
     $results = $this->User->find(array('User.username' => $this->data['User']['username']), array('User.active'), null, false); 

     $this->redirect(array('controller' => 'users', 'action' => 'login')); 

          } 
        } 

     $this->data['User']['password'] = ''; 

    } 

    function logout() { 

    $this->redirect($this->Auth->logout()); 

    } 

} 
?> 

要素/ loginform.ctp

<?php 
if ($this->Session->read('Auth.User.username')):?> 
<?php 
    echo "Welcome".' ' ; 
echo $this->Session->read('Auth.User.username'); 
echo " "; 
echo $html->link('logout', array('action'=>'logout')); 
?> 
<?php else : ?> 
<div class="types form"> 
     <?php echo $form->create('User', array('controller' => 'Users','action' => 'login')); ?> 
     <?php echo $form->input('username', array('label' => 'username')); ?> 
     <?php echo $form->input('password',array('type'=>'password', 'label' => 'password')) ?> 
     <?php echo $form->submit('Submit'); ?> 
     </div> 
<?php endif; ?> 

UPDATEである

私はこの配列

Array ([Config] => Array 
      ([userAgent] => 8f12200c2d48fa7955465842befe1c9e 
       [time] => 1323562284 [timeout] => 10) 
       [Auth] => Array ( 
        [User] => Array ( 
         [id] => 63 
         [user_role] => 2 [ 
         [user_fname] => test 
         [user_lname] => test 
         [user_email] => [email protected] 
         [user_phone] => 677-988-7777 
         [user_cellphone] => 555-456-9999 
         [user_address1] => 1st Avenue 
         [user_address2] => 
         [user_city] => Citiland FL 
         [user_zip] => 55555 
         [username] => admin2))) 

しかし、私は新しいページに移動します

Array ([Config] => Array ( 
    [userAgent] => 8f12200c2d48fa7955465842befe1c9e 
    [time] => 1323562591 [timeout] => 10)) 
をGETログインしたとき、私は

<?php 
class AppController extends Controller {  

    var $helpers = array('Html', 'Form', 'Javascript', 'Session'); 
    var $components = array('Auth', 'Session'); 

    function beforeFilter() { 
     //new addition 
     $this->set('userData', $this->Session->read()); 
     $this->Auth->allow('add','get_categories','get_home', 'get_others', 'pages', '*'); 
     $this->Auth->autoRedirect = false; 

     } 
} 
?> 

app_controllerに次を追加しました

ADDEDますprint_r要素へ/ loginformは、すべてのページ

<?php 
print_r($userData); 
if ($this->Session->read('Auth.User.username')):?> 
<?php 
    echo "Welcome".' ' ; 
echo $this->Session->read('Auth.User.username'); 
echo " "; 
echo $html->link('logout', array('action'=>'logout')); 
?> 
<?php else : ?> 
<div class="types form"> 
     <?php echo $form->create('User', array('controller' => 'Users','action' => 'login')); ?> 
     <?php echo $form->input('username', array('label' => 'username')); ?> 
     <?php echo $form->input('password',array('type'=>'password', 'label' => 'password')) ?> 
     <?php echo $form->submit('Submit'); ?> 
     </div> 
<?php endif; ?> 

答えて

0

1)あなたがUserControllerで、ユーザーの後、リダイレクトがログインしている必要があるためこれはACROSS変数の内容を参照してください。 あなたのログイン機能が代わりに読んでください:

、私はそれがSessionコンポーネントを使用してサーバー(上のtmpディレクトリに存在している間)、それは実際にされていないようにあなたは、後でデータを設定されていないので、それがかもしれ信じる2については
function login() { 

     $this->layout = 'master_layout'; 

       if ($this->data) { 

        if ($this->Auth->login($this->data)) { 

        // Retrieve user data 
     $results = $this->User->find(array('User.username' => $this->data['User']['username']), array('User.active'), null, false); 

/*This is the offending line, I've commented it out, but you could have it redirect somewhere else (it might be a good idea to redirect to the index action, for example, or just delete it:*/ 

     //$this->redirect(array('controller' => 'users', 'action' => 'login')); 


          } 
        } 

     $this->data['User']['password'] = ''; 

    } 

)ビューに沿って渡されます。ですから、基本的には、ビュー内でSessionメソッドを呼び出すことができないからです。私がそれについて完全に正しいとはいえない場合、私は何をするべきか提案しています:ビュー内でセッションメソッドを呼び出すのではなく、それらをコントローラで呼び出す、ビューを変数に設定して$this->set();ビュー内のその変数に対してテストします。

あなたがビューとアクションのトンのためにこれを行う必要がある場合、あなたは、コントローラ、あるいはアプリのコントローラにこのようなものを追加することを検討できます。また

function beforeFilter() { 
// Get session data for appllication use 
$this->appuserstuff = $this->Session->read(); 
} 

function beforeRender() { 
// Make app variables available to view 
$this->set('userData', $this->appuserstuff); 
} 

、あなただけのカップルでこれを必要とする場合は、あなたはデータを配列するときに構成されているかを確認することができますので、今、私はあなたのビューのいずれかで<?php debug($userData); ?>を行うことをお勧めします

$this->set('userData', $this->Session->read()); 

:アクションは、あなただけでそれらのアクションでユーザデータを設定することができます設定されているので、ユーザーまたはテストc onditionides against。最後に、データの配列に対してチェックを行う代わりに、ビューのSessionへのダイレクトコールを置き換えることができます。

特定の配列がどのように構造化されているかわからないことに注意してください。それを動作させるための独自のキーで:

<?php 
if (!empty($userData['User'])):?> 
<?php 
    echo "Welcome".' ' ; 
echo $userData['User']['username']; 
echo " "; 
echo $html->link('logout', array('action'=>'logout')); 
?> 
<?php else : ?> 
<div class="types form"> 
     <?php echo $form->create('User', array('controller' => 'Users','action' => 'login')); ?> 
     <?php echo $form->input('username', array('label' => 'username')); ?> 
     <?php echo $form->input('password',array('type'=>'password', 'label' => 'password')) ?> 
     <?php echo $form->submit('Submit'); ?> 
     </div> 
<?php endif; ?> 
+0

私は – user975582

+0

[OK]を、私はいくつかのより多くの研究を行うとするとき代わりに何が起こるあなたに戻って – swiecki

+0

買ってあげるより多くの情報を追加して記事で参照、:(動作しませんでした – swiecki

関連する問題