在我正在开发的应用程序中使用汉字时,我遇到了一个非常奇怪的编码问题;除了字符“免”之外的所有汉字字符都可以正常工作。
当我尝试使用这个字符时,mongodb 抛出以下异常:

Exception: non-utf8 string: �

任何其他汉字字符都可以正常工作。

我试图删除处理字符串的函数,并将其直接输入到处理 mongodb 的函数中,但没有成功。
我也尝试从各种编码转换字符,但无济于事。

这就是我正在使用的:
  • Phalcon 2.0 - https://phalconphp.com
  • Apache 2.4.12
  • PHP 5.6.8
  • MongoDB 3.0.4

  • 谢谢!

    处理请求的 Controller 函数
    public function kanjiAction($findKanji = "")
    {
        // Really weird issue with the kanji 免 not working
    
        $findKanji = $this->filter->sanitize($findKanji, 'string');
        $findKanji = mb_substr($findKanji, 0, 1, 'UTF-8');
    
        $kanjiHash = md5($findKanji);
    
        // If entry does not exist in cache, create it
        if(!$this->viewCache->exists($kanjiHash)) {
    
            // Get data from DB
            $kanji = Kanji::findById($findKanji);
    
            $words = Words::containingKanji($findKanji);
    
            $showWords = array();
    
            foreach ($words as $word) {
                foreach ($word->k_ele["keb"] as $key => $value) {
                    if(strpos($value, $findKanji) != false) {
                        $showWords[] = (object) array(
                            "word" => $value,
                            "readings" => $word->r_ele["reb"],
                            "meanings" => $word->sense["gloss"]
                        );
                        break;
                    }
                }
            }
    
            // Fetch translations
            $this->view->onTitle = $this->translation->query('onTitle');
            $this->view->kunTitle = $this->translation->query('kunTitle');
            $this->view->radicalTitle = $this->translation->query('radicalTitle');
            $this->view->meaningsTitle = $this->translation->query('meaningsTitle');
            $this->view->strokeCountTitle = $this->translation->query('strokeCountTitle');
            $this->view->jlptTitle = $this->translation->query('jlptTitle');
            $this->view->containsWordsTitle = $this->translation->query('containsWordsTitle');
            $this->view->readingsTitle = $this->translation->query('readingsTitle');
            $this->view->wordsTitle = $this->translation->query('wordsTitle');
            $this->view->notFound = $this->translation->query('notFound');
    
            // Pass data to view
            $this->view->kanji = $kanji->_id;
            if(isset($kanji->reading["on"]) && count($kanji->reading["on"]) > 0)
                $this->view->onReadings = $kanji->reading["on"];
            if(isset($kanji->reading["kun"]) && count($kanji->reading["kun"]) > 0)
                $this->view->kunReadings = $kanji->reading["kun"];
            if(isset($kanji->radicals))
                $this->view->radicals = $kanji->radicals;
            if(isset($kanji->attributes['jlpt']))
                $this->view->jlpt = $kanji->attributes['jlpt'];
            if(isset($kanji->attributes['stroke_count']))
                $this->view->strokeCount = $kanji->attributes['stroke_count'];
            if(isset($kanji->meaning))
                $this->view->meanings = $kanji->meaning;
            if(count($showWords) > 0)
                $this->view->words = $showWords;
    
            $this->view->kanjiHash = $kanjiHash;
        } else {
            $this->view->kanjiHash = $kanjiHash;
        }
    }
    

    非工作汉字的请求头:
    GET /view/kanji/%E5%85%8D HTTP/1.1
    Host: localhost
    Connection: keep-alive
    Pragma: no-cache
    Cache-Control: no-cache
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
    

    非工作汉字的响应头:
    HTTP/1.1 200 OK
    Date: Fri, 03 Jul 2015 13:04:05 GMT
    Server: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
    X-Powered-By: PHP/5.6.8
    Content-Length: 29
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: text/html; charset=utf-8
    

    工作汉字的请求头:
    GET /view/kanji/%E5%A8%A9 HTTP/1.1
    Host: localhost
    Connection: keep-alive
    Pragma: no-cache
    Cache-Control: no-cache
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
    

    工作汉字的响应头:
    HTTP/1.1 200 OK
    Date: Fri, 03 Jul 2015 13:16:20 GMT
    Server: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
    X-Powered-By: PHP/5.6.8
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=utf-8
    

    编辑:

    免的堆栈跟踪:
    #0 [internal function]: MongoCursor->rewind()
    #1 [internal function]: Phalcon\Mvc\Collection::_getResultset(Array, Object(Purple\Model\Kanji), Object(MongoDB), true)
    #2 [internal function]: Phalcon\Mvc\Collection::findFirst(Array)
    #3 E:\xampp\htdocs\app\Purple\Controller\ViewController.php(33): Phalcon\Mvc\Collection::findById('\xE5')
    #4 [internal function]: Purple\Controller\ViewController->kanjiAction('\xE5')
    #5 [internal function]: Phalcon\Dispatcher->dispatch()
    #6 E:\xampp\htdocs\public\index.php(98): Phalcon\Mvc\Application->handle()
    #7 {main}
    

    编辑2:
    [Mon Jul 06 18:05:59.294298 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] add path info postfix: E:/xampp/htdocs/view -> E:/xampp/htdocs/view/kanji/\xe5\x85\x8d
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] strip per-dir prefix: E:/xampp/htdocs/view/kanji/\xe5\x85\x8d -> view/kanji/\xe5\x85\x8d
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] applying pattern '^$' to uri 'view/kanji/\xe5\x85\x8d'
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] add path info postfix: E:/xampp/htdocs/view -> E:/xampp/htdocs/view/kanji/\xe5\x85\x8d
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] strip per-dir prefix: E:/xampp/htdocs/view/kanji/\xe5\x85\x8d -> view/kanji/\xe5\x85\x8d
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] applying pattern '(.*)' to uri 'view/kanji/\xe5\x85\x8d'
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace2] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] rewrite 'view/kanji/\xe5\x85\x8d' -> 'public/view/kanji/\xe5'
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] add per-dir prefix: public/view/kanji/\xe5 -> E:/xampp/htdocs/public/view/kanji/\xe5
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace2] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] strip document_root prefix: E:/xampp/htdocs/public/view/kanji/\xe5 -> /public/view/kanji/\xe5
    [Mon Jul 06 18:05:59.294798 2015] [rewrite:trace1] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f045c0/initial] [perdir E:/xampp/htdocs/] internal redirect with /public/view/kanji/\xe5 [INTERNAL REDIRECT]
    [Mon Jul 06 18:05:59.296799 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] add path info postfix: E:/xampp/htdocs/public/view -> E:/xampp/htdocs/public/view/kanji/\xe5
    [Mon Jul 06 18:05:59.296799 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] strip per-dir prefix: E:/xampp/htdocs/public/view/kanji/\xe5 -> view/kanji/\xe5
    [Mon Jul 06 18:05:59.317801 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] applying pattern '^(.*)$' to uri 'view/kanji/\xe5'
    [Mon Jul 06 18:05:59.318301 2015] [rewrite:trace2] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] rewrite 'view/kanji/\xe5' -> 'index.php?_url=/view/kanji/\xe5'
    [Mon Jul 06 18:05:59.318301 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] split uri=index.php?_url=/view/kanji/\xe5 -> uri=index.php, args=_url=/view/kanji/\xe5
    [Mon Jul 06 18:05:59.318301 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] add per-dir prefix: index.php -> E:/xampp/htdocs/public/index.php
    [Mon Jul 06 18:05:59.318301 2015] [rewrite:trace2] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] strip document_root prefix: E:/xampp/htdocs/public/index.php -> /public/index.php
    [Mon Jul 06 18:05:59.318301 2015] [rewrite:trace1] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f06278/initial/redir#1] [perdir E:/xampp/htdocs/public/] internal redirect with /public/index.php [INTERNAL REDIRECT]
    [Mon Jul 06 18:05:59.318301 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f0d7d8/initial/redir#2] [perdir E:/xampp/htdocs/public/] strip per-dir prefix: E:/xampp/htdocs/public/index.php -> index.php
    [Mon Jul 06 18:05:59.318301 2015] [rewrite:trace3] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f0d7d8/initial/redir#2] [perdir E:/xampp/htdocs/public/] applying pattern '^(.*)$' to uri 'index.php'
    [Mon Jul 06 18:05:59.318801 2015] [rewrite:trace1] [pid 5728:tid 2180] mod_rewrite.c(475): [client 127.0.0.1:52741] 127.0.0.1 - - [localhost/sid#3dac08][rid#3f0d7d8/initial/redir#2] [perdir E:/xampp/htdocs/public/] pass through E:/xampp/htdocs/public/index.php
    

    编辑3:
    [Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] add path info postfix: E:/xampp/htdocs/view -> E:/xampp/htdocs/view/kanji/\xe5\x85\x8d
    [Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] strip per-dir prefix: E:/xampp/htdocs/view/kanji/\xe5\x85\x8d -> view/kanji/\xe5\x85\x8d
    [Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] applying pattern '^$' to uri 'view/kanji/\xe5\x85\x8d'
    [Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] add path info postfix: E:/xampp/htdocs/view -> E:/xampp/htdocs/view/kanji/\xe5\x85\x8d
    [Tue Jul 07 01:25:18.844429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] strip per-dir prefix: E:/xampp/htdocs/view/kanji/\xe5\x85\x8d -> view/kanji/\xe5\x85\x8d
    [Tue Jul 07 01:25:18.844929 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] applying pattern '(.*)' to uri 'view/kanji/\xe5\x85\x8d'
    [Tue Jul 07 01:25:18.844929 2015] [rewrite:trace2] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] rewrite 'view/kanji/\xe5\x85\x8d' -> 'public/view%2fkanji%2f%e5'
    [Tue Jul 07 01:25:18.844929 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] add per-dir prefix: public/view%2fkanji%2f%e5 -> E:/xampp/htdocs/public/view%2fkanji%2f%e5
    [Tue Jul 07 01:25:18.844929 2015] [rewrite:trace2] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] strip document_root prefix: E:/xampp/htdocs/public/view%2fkanji%2f%e5 -> /public/view%2fkanji%2f%e5
    [Tue Jul 07 01:25:18.844929 2015] [rewrite:trace1] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f240c8/initial] [perdir E:/xampp/htdocs/] internal redirect with /public/view%2fkanji%2f%e5 [INTERNAL REDIRECT]
    [Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] add path info postfix: E:/xampp/htdocs/public/view -> E:/xampp/htdocs/public/view/kanji/\xe5
    [Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] strip per-dir prefix: E:/xampp/htdocs/public/view/kanji/\xe5 -> view/kanji/\xe5
    [Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] applying pattern '^(.*)$' to uri 'view/kanji/\xe5'
    [Tue Jul 07 01:25:18.849429 2015] [rewrite:trace2] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] rewrite 'view/kanji/\xe5' -> 'index.php?_url=/view%2fkanji%2f%e5'
    [Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] split uri=index.php?_url=/view%2fkanji%2f%e5 -> uri=index.php, args=_url=/view%2fkanji%2f%e5
    [Tue Jul 07 01:25:18.849429 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] add per-dir prefix: index.php -> E:/xampp/htdocs/public/index.php
    [Tue Jul 07 01:25:18.849429 2015] [rewrite:trace2] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] strip document_root prefix: E:/xampp/htdocs/public/index.php -> /public/index.php
    [Tue Jul 07 01:25:18.849429 2015] [rewrite:trace1] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f25e28/initial/redir#1] [perdir E:/xampp/htdocs/public/] internal redirect with /public/index.php [INTERNAL REDIRECT]
    [Tue Jul 07 01:25:18.849930 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f2d448/initial/redir#2] [perdir E:/xampp/htdocs/public/] strip per-dir prefix: E:/xampp/htdocs/public/index.php -> index.php
    [Tue Jul 07 01:25:18.849930 2015] [rewrite:trace3] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f2d448/initial/redir#2] [perdir E:/xampp/htdocs/public/] applying pattern '^(.*)$' to uri 'index.php'
    [Tue Jul 07 01:25:18.850430 2015] [rewrite:trace1] [pid 7480:tid 2176] mod_rewrite.c(475): [client 127.0.0.1:63967] 127.0.0.1 - - [localhost/sid#5fac08][rid#3f2d448/initial/redir#2] [perdir E:/xampp/htdocs/public/] pass through E:/xampp/htdocs/public/index.php
    

    最佳答案

    从您的堆栈跟踪来看,似乎有些东西在到达您的 Controller 之前剥离了部分 $findKanji。而不是代表 免 的三个字节,在对 kanjiAction 的调用中只有第一个字节。

    另一个失败的汉字也包含一个 0x85 字节,所以我想知道在你点击 Controller 之前是否有东西在点击字节 0x85 时剥离所有字节。

    所以现在你需要在它到达你的 Controller 之前查看可能会干扰你的参数。您是否有任何事件监听器附加到 dispatch:beforeDispatchLoop 或任何 dispatch 事件? (如果有,是什么?)

    我会注意到我刚刚从头开始制作了一个 phalcon 应用程序(按照教程)来查看我得到的堆栈跟踪,我得到了堆栈跟踪:

    Exception: Because I can
     File=/var/www/tutorial/app/controllers/ViewController.php
     Line=15
    #0 [internal function]: ViewController->kanjiAction('???')
    #1 [internal function]: Phalcon\Dispatcher->dispatch()
    #2 /var/www/tutorial/public/index.php(39): Phalcon\Mvc\Application->handle()
    #3 {main}
    

    注意传递给 kanjiAction 的三个字符:如果我点击 url /view/kanji/%E5 我只会得到一个字符。

    那是一个简化的 Controller ,它的存在只是为了提供堆栈跟踪:
    <?php
    
    use Phalcon\Mvc\Controller;
    
    class ViewController extends Controller
    {
    
        public function indexAction()
        {
        }
    
        public function kanjiAction($findKanji = "")
        {
            var_dump($findKanji);
            throw new Exception('Because I can');
        }
    }
    

    这是使用 Phalcon 2.0.1。所以我们知道一个普通的 phalcon 应用程序将所有三个字节传递给处理程序,至少在我的 ubuntu 和 Apache 2.2 设置中是这样。

    看看我的设置(显然有效)和你的设置之间的区别,我想知道区别是否与 Windows 上的 Apache 重写规则有关。所以让我们打开重写过程的调试方式。因为你有 Apache 2.4,这是通过放置这个指令来完成的:
    LogLevel alert rewrite:trace3
    

    进入服务器配置文件中的 <VirtualHost> 部分。

    完成此操作后,请使用 apache 日志中包含 [rewrite: 的行更新答案,这些行是在您尝试访问这些错误汉字之一时生成的。

    编辑:现在你已经包含了这些信息,我想我有一个修复。

    我不知道你为什么看到你的问题。我将单独尝试重现它,以便我可以向 apache 提交错误(尽管在获得 Windows 框之前我可能无法提交),但与此同时,我想我有一个修复程序。

    首先,在服务器配置文件的 <VirtualHost> 部分添加以下指令:
    AllowEncodedSlashes On
    

    然后,假设您按照 Phalcon 教程设置了 .htaccess 文件,将 B 选项添加到顶层目录的 RewriteRule 文件中的第二个 .htaccess 中,这样该行看起来像:
    RewriteRule  (.*) public/$1 [B,L]
    

    然后也在B目录下的RewriteRule文件中的.htaccess行添加public/标志,使该行变成:
    RewriteRule ^(.*)$ index.php?_url=/$1 [B,QSA,L]
    

    可能不需要第二次更改;因为我的系统没有你的确切问题,所以我无法检查。

    仍然没有运气,但让我们再尝试一件事:将 (.*) 转换为 ((?s).*),以便之前修改的行现在变为:
    RewriteRule  ((?s).*) public/$1 [B,L]
    


    RewriteRule ^((?s).*)$ index.php?_url=/$1 [B,QSA,L]
    

    假设这有效,其背后的推理很奇怪。

    显然,字符 0x85 是 Unicode 中的“下一行”字符,可以被 PCRE(Apache 重写功能底层的正则表达式引擎)视为换行符 as detailed here 。 PCRE 的正常编译方式是仅将字符 10 视为换行符 (\n),但可以编译 PCRE 以将更多字符视为换行符。

    当字符是“换行”字符时,正则表达式元字符 . 将不匹配它,除非 s 正则表达式选项处于事件状态。 (?s) 位使该选项处于事件状态。

    如果可行,您可以消除 B 上的 RewriteRules 选项,并从 AllowEncodedSlashes 中删除 <VirtualHost> 指令。

    您使用的是本地编译的 apache 二进制文件,还是从官方获取的?因为如果你从官方那里得到一个,你应该提示他们用这个奇怪的选项编译 PCRE 浪费了你的周末。

    关于php - 特定汉字在 http get 请求期间被错误编码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31207949/

    10-11 01:23