HTTP之实现basic认证

 header("WWW-Authenticate:Basic realm='Private'");

// 实现basic认证
header("WWW-Authenticate:Basic realm='Private'");
header('HTTP/1.0 401 Unauthorized');
print "You are unauthorized to enter this area.";
exit(0);
// 实现basic认证
header("WWW-Authenticate:Basic realm='你好'");

1,PHP iconv()

PHP iconv()函数字符编码转换的问题讲解    https://www.jb51.net/article/158282.htm 

最简单的应用,把gb2312置换成utf-8:

1

$text=iconv("GB2312","UTF-8",$text);

在用$text=iconv("UTF-8","GB2312",$text)过程中,如果遇到一些特别字符时,如:"—",英文名中的"."等等字符,转换就断掉了。这些字符后的文字都没法继续转换了。

针对这的问题,可以用如下代码实现:

1

$text=iconv("UTF-8","GBK",$text);

你没有看错,就这么简单,不使用gb2312,而写成GBK,就可以了。

还有一种方法,第二个参数,加上//IGNORE,忽略错误,如下:

1

iconv("UTF-8","GB2312//IGNORE",$data);

2, mb_convert_encoding

做一个GBK To UTF-8:

1

2

3

4

<?php

header("content-Type: text/html; charset=Utf-8");

echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");

?>

再来个GB2312 To Big5:

1

2

3

4

<?php

header("content-Type: text/html; charset=big5");

echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");

?>

--------------------------

3,不行,使用下面的函数:

<?php
function conv_utf8_iso8859_7($s)
{
    $len = strlen($s);
    $out = "";
    $curr_char = "";
    for ($i = 0; $i < $len; $i++) {
        $curr_char .= $s[$i];
        if ((ord($s[$i]) & (128 + 64)) == 128) {
            // 找到字符结尾
            if (strlen($curr_char) == 2) {
                // 字符结束foun2字节字符检查为希腊字符并转换
                if (ord($curr_char[0]) == 205) {
                    $out .= chr(ord($curr_char[1]) + 16);
                } else if (ord($curr_char[0]) == 206) {
                    $out .= chr(ord($curr_char[1]) + 48);
                } else if (ord($curr_char[0]) == 207) {
                    $out .= chr(ord($curr_char[1]) + 112);
                }else{
                    // 非希腊语2字节字符,丢弃字符
                } ;
            }else {
                // n字节字符,n>2,丢弃字符
            };
            $curr_char = "";
        } else if (ord($s[$i]) < 128) {
            // 字符是一个字节(ASCII)
            $out .= $curr_char;
            $curr_char = "";
        }
    }
    return $out;
}
// -------------------------------------------------------------

echo conv_utf8_iso8859_7('tenx');

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值