2011-12-10 296 views
0

我想完成这个网站在以前在普通的PHP,但作为cakephp世界新手蛋糕PHP ..我发现了一些困难,如这些。避免重定向并坚持会话

1)。当我点击登录时,它将我转移到另一个页面,虽然我没有在应用程序控制器中指定重定向,除非有注册。登录位于顶部,并可通过所有页面查看,但如果我点击登录,它会将我重定向到登录页面(我不想)。

2.)我登录后,它会带上用户名说欢迎'用户名'的会话,但如果我去另一个页面,它似乎忘记会话,并带回登录表单的输入在页面顶部。

这是我的代码

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; 

     } 
} 
?> 

UsersController

<?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

添加以下到app_controller

<?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; 

     } 
} 
?> 

WHEN我LOGIN我得到这个ARRAY

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))) 

但是当我导航到新的PAGE

Array ([Config] => Array ( 
    [userAgent] => 8f12200c2d48fa7955465842befe1c9e 
    [time] => 1323562591 [timeout] => 10)) 

ADDED的print_r将元素/登录表单看到变量内容在所有网页

<?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)这是因为在你的UsersController你有用户后重定向被记录下来。 您的登录功能应改为阅读:

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'] = ''; 

    } 

至于2),我认为这可能是因为你没有设置数据之后,所以,虽然它在服务器(使用会话组件上的tmp目录下存在),它实际上不是被传递到视图。所以基本上我认为这是因为你不能在视图中调用会话方法。即使我对此并不完全正确,但我建议要做的事情应该可行:不要在视图中调用会话方法,而要在控制器中调用它们,将它们设置在变量中以便使用$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); 
} 

另外,如果你只需要这几个行动,你可以只设置在这些行动中的用户数据有:

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

现在,我建议你在你的观点一个做<?php debug($userData); ?>所以你可以看到如何将数据在阵列中时结构它被设置,以便您可以使用或测试c辩论反对。最后,你可以用视图中的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

好吧,我会做一些调查研究,并尽快给您 – swiecki

+0

时又会出现什么情况在你的应用程序控制器中执行'$ this-> Session-> read()'你做了'$ this-> Auth-> user();'' – swiecki