Redirect invalidation and BOM header in ZENDFRAMEWORK2 Controller

Source: Internet
Author: User
Tags ord

When we use the ZENDFRAMEWORK2 framework, we often redirect operations inside the controller, such as: In usercontroller.php, the user requests/user/index, and if the user is not logged in, it is necessary to use the "$this-> Redirect ()->toroute (); Automatically redirect to/user/login, as follows

     Public Function indexaction ()
     {
         $logginedName = $this->isloggined ();
         if (! $logginedName) {return
             $this->redirect ()->toroute (' User ', Array (
                   ' action ' => ' login '
                )
         } return         
         new ViewModel (Array (
             ' users ' => $this->getusertable ()->fetchall ())
         ;
     }

For redirection operations in the controller, if the php.ini configuration output_buffering is set to OFF, "$this->redirect ()->toroute () may sometimes appear." Failure of the situation, the specific performance is PHP implementation to the "$this->redirect ()->toroute ();" The output is terminated.

There are three possible causes for this problem:

1) "$this->redirect ()->toroute ();" This sentence is preceded by the echo output;

2) "$this->redirect ()->toroute ();" Before this sentence, the PHP code block Terminator "?>" has string output (including spaces);

3 The *.php file in the Src/user/controller, Src/user/from or Src/user/model directory contains the BOM header.

BOM headers are three of invisible characters (0xEF, 0xBB, and 0xBF) that are inserted at the beginning of a file when you save an UTF-8 encoded file by using software such as Notepad, which is brought by Windows. PHP does not recognize these three characters and directly outputs these three characters to the content of HTTP. In this case, if the php.ini configuration output_buffering is off,php, the header function can no longer be executed to modify the HTTP header, and "$this->redirect ()->toroute ();" is to modify the HTTP headers to achieve redirection, so there is an error, ZENDFRAMEWORK2 encountered this error will automatically terminate the output.

For the first and second cases, just check the code to avoid it, and for the third case, you can use the following method.

Save the following copy of the code as a clearbom.php file, copy the clearbom.php file to the module/directory of the ZENDFRAMEWORK2 project, and then access the http://....../module/directly in the browser clearbom.php (specific URL based on project adjustment) can be removed from the module/directory and its subdirectories all the *.php files of the BOM header:

function Clearbom ($dirname) {$fileList = Scandir ($dirname); foreach ($fileList as $file) {$pathfile = $dirname. "
        /". $file;
                if (Is_dir ($pathfile)) {if ($file!= "." && $file!= "...")
        Clearbom ($pathfile);
                }else{if (strlen ($file) > 4 && strtolower (substr ($file,-4) = = ". php") {//processing *.php files only
                $content = file_get_contents ($pathfile);
                    if (strlen ($content) > 3) {$char [1] = substr ($content, 0, 1);
                    $char [2] = substr ($content, 1, 1);
                    $char [3] = substr ($content, 2, 1); if (Ord ($char [1]) = = 239 && ord ($char [2]) = = 187 && Ord ($c
                        HAR[3] = = 191) {$restcontent = substr ($content, 3);
                        $f = fopen ($pathfile, "w");
                        Fwrite ($f, $restcontent); FclOSE ($f); }}}}} Clearbom (__dir__);

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.