我希望用户ID如何变为静态

时间:2010-12-10 03:23:30

标签: cakephp user-controls

我对cakephp很陌生,我想知道如何静态id下载或列出的id。我已经隐藏但它不进入数据库。这是我的编码:

这是在控制器中

function add() {
  if (!empty($this->data)) {
   $this->Post->create();
   if ($this->Post->save($this->data)) {
    $this->Session->setFlash(__('The post has been saved', true));
    $this->redirect(array('action' => 'index'));
   } else {
    $this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
   }
  }
  $users = $this->Post->User->find('list');
  $this->set(compact('users'));
  $this->set('userid',$this->Auth->user('id'));
 }

此编码位于ctp

<?php
  echo $this->Form->input('user_id');
?>

1 个答案:

答案 0 :(得分:2)

最好的方法是不将用户ID发送到浏览器并返回,因为这可能会形成修改和安全漏洞/无效结果。只需在保存之前将用户ID注入数据:

function add() {
    if (!empty($this->data)) {
        $this->Post->create();

        // setting user id
        $this->data['Post']['user_id'] = $this->Auth->user('id');

        if ($this->Post->save($this->data)) {
            $this->Session->setFlash(__('The post has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
        }
    }

    $users = $this->Post->User->find('list');
    $this->set(compact('users'));
}