emoji在瀏覽器中是如何傳遞給伺服器的

machenchi0207發表於2019-02-16

做兩個小測試

(a) utf8編碼情況下,傳送post請求,攜帶頁面輸入的emoji表情

<?php
header("Content-type:text/html;charset=utf-8");
$out = <<<HEREDOC
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>
        div.emoji{
            font-size:20px;
        }
    </style>
</head>
<body>
<div>
emoji:&#9785;
</div>
<div class="emoji">
    
<div>
<form method="POST" action="test.php">
emoji:<input type="text" name="emoji"/>
<button>submit</button>
</form>
</body>
HEREDOC;
echo $out;die;
提交後,抓包結果

emoji => %E2%98%B9 urldecode解碼後 :即為utf-8 的編碼: 0xE298B9 對應的unicode 十進位制:9785

gbk編碼情況下,傳送post請求,攜帶頁面輸入的emoji表情

<?php
header("Content-type:text/html;charset=gbk");
$out = <<<HEREDOC
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
    <style>
        div.emoji{
            font-size:20px;
        }
    </style>
</head>
<body>
<div>
emoji:&#9785;
</div>
<div class="emoji">
    
<div>
<form method="POST" action="test.php">
emoji:<input type="text" name="emoji"/>
<button>submit</button>
</form>
</body>
HEREDOC;
echo $out;die;
提交後,抓包結果

emoji => %26%239785%3B urldecode解碼後 : 9785;(這裡#和9我加了空格 不然就直接輸出emoji了) 即為NCR的值 對應的unicode 十進位制:9785

結論

  1. 當前編碼環境為unicode下的某一種,比如utf-8,則頁面內的emoji對應的unicode號碼進行相應的編碼後,傳送到伺服器
  2. 當前編碼不是unicode下的,比如gbk,則頁面內的emoji對應的NCR結果被髮送至伺服器

相關文章