2013-02-26 16 views
1

View1.ctp라는보기가 있습니다.이보기에서 'captcha'라는 제어기 기능을 호출하는데보기는 captcha.ctp입니다. $ text라는 변수가 있습니다. view captcha.ctp. view1.ctp에서이 $ text 변수에 액세스하려고합니다. 어떻게해야합니까? (참고 : CakePHP의 버전 -2.3) 더 좋은 방법은 보안 문자를 설정하는 것입니다 View1.ctp다른보기에서보기 변수에 액세스하기 --Cakephp

 <h1>Add Comment</h1> 
     <?php 
     $post_id= $posts['Post']['id']; 
     echo $this->Form->create('Comment',array('action' => 'comment','url' =>   array($post_id,$flag))); 
     echo $this->Form->input('name'); 
     echo $this->Form->input('email'); 
     echo $this->Form->input('text', array('rows' => '3')); 
     echo "Enter Captcha Code: "; 
     echo $this->Html->image(
     array('controller' => 'posts', 'action' => 'captcha')); 
     echo $this->Form->input('code'); 
     echo $this->Form->end('Add comment'); 
      ?> 

     captcha.ctp: 

      <?php 
      $this->Session->read(); 
      $text = rand(10000,99996); 
      $_SESSION["vercode"] = $text; 
      $height = 25; 
      $width = 65; 
      $image_p = imagecreate($width, $height); 
      $black = imagecolorallocate($image_p, 0, 0, 0); 
      $white = imagecolorallocate($image_p, 255, 255, 255); 
      $font_size = 14; 
      imagestring($image_p, $font_size, 5, 5, $text, $white); 
      imagejpeg($image_p, null, 80); 
      ?> 

답변

0

가 더 적합하는 대신 Helper로 볼 수 있습니다. 따라서 응용 프로그램 /보기/도우미/CaptchaHelper.php에 captcha.ctp를 이동하고 같은 클래스의 내용입니다 포장 : 귀하가 PostsController에 그런

<?php 
App::uses('AppHelper', 'View/Helper'); 

class CaptchaHelper extends AppHelper { 

    function create() { 
     // This line doesn't make much sense as no data from the session is used 
     // $this->Session->read(); 

     $text = rand(10000, 99996); 

     // Don't use the $_SESSION superglobal in Cake apps 
     // $_SESSION["vercode"] = $text; 

     // Use SessionComponent::write instead 
     $session = new SessionComponent(new ComponentCollection()); 
     $session->write('vercode', $text); 

     $height = 25; 
     $width = 65; 
     $image_p = imagecreate($width, $height); 
     $black = imagecolorallocate($image_p, 0, 0, 0); 
     $white = imagecolorallocate($image_p, 255, 255, 255); 
     $font_size = 14; 
     imagestring($image_p, $font_size, 5, 5, $text, $white); 

     // Return the generated image 
     return imagejpeg($image_p, null, 80); 
    } 

} 

, 헬퍼 배열에 Captcha을 추가

public $helpers = array('Captcha'); 

(이미 헬퍼 배열이있는 경우 또는, 그냥 배열에 추가합니다.) 당신은 단지 이미지를 반환하는 도우미를 호출 할 수 있습니다

당신의 View1.ctp에서 다음

:

"expected"값은 세션 키 vercode에 저장되며 양식 처리 로직에서 PostsController에서도 읽을 수 있습니다.

+0

덕분에 많이. 시도해 보니 선생님 – user1479469

+0

알 수 있지만 다른 컨트롤러에서이 vercode에 액세스해야합니다. CommentsController.php PostsController.ctp가 아닙니다. – user1479469

+0

이 오류가 발생합니다. 오류 : 부재 기능 호출 non-object에서 read()를 호출한다. – user1479469

관련 문제