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

作兩個小測試

(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結果被髮送至服務器
相關文章
相關標籤/搜索