4.字符集操作原理与乱码解决方案

show character set;

显示出字符集校对

show variables like "%character%";
show variables like "%collation%";
显示环境变量
 
+----------------------------+--------------------------------+
| Variable_name                   | Value                                       |
+----------------------------+--------------------------------+
| character_set_client           | gbk                                          |
| character_set_connection | gbk                                          |
| character_set_database     | utf8                                         |
| character_set_filesystem   | binary                                      |
| character_set_results         | gbk                                          |
| character_set_server          | latin1                                       |
| character_set_system        | utf8                                          |
| character_sets_dir              | D:\XAMPP\mysql\share\charsets\ |
+----------------------------+--------------------------------+
 
character_set_server            | latin1    默认操作字符集
character_set_system           | utf8    系统元数据字符集
 
 character_set_database     | utf8   当前选中的数据库字符集
character_set_filesystem   | binary  文件系统字符       
 
 
drop database if exists u;    如果存在u数据库就执行删除
create database u default character set utf8;
show databases;
use u;
create table user(name char(30) character set gbk) default character set utf8;
show create table user\G
 
 
 
create table user(name char(30) character set gbk) default character set utf8;
show create table user\G
select * from user;
alter table user modify name char(30) character set utf8;    修改表的字符集
show create table user\G
实现了字符的转换
 
set names gbk;
set names utf8;
 
set character_set_result=utf8;
 
create table user(id int(10) primary key auto_increment , username char(30) ,password char(32) , tel char(12) , qq char(12)) default character set gbk;
desc user;
insert into user (username , password , tel , qq) values("admin" ,md5("admin") ,15051986853 ,1440016892);
其中的md5是加密的一种方式
 
验证登录php脚本
 
 
<?php
header('Content-Type:image/png');
header('Content-Type:no-cache');
header('Pragma:no-cache');
header('Expires:0');
setcookie('userName', 'Tommy');
$w=90;//图片的宽
$h=30;//图片的高
//在服务器端内存中创建图片
$img=imagecreatetruecolor($w, $h);
 
//分配一个新的颜色
$c = imagecolorallocate($img,  rand(180,240),  rand(180,240),  rand(180,240));
 
//在图像向画个矩形,当背景色
imagefilledrectangle($img,  0, 0,  $w,  $h,  $c);
 
$src = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$code = '' ;
for($i =0; $i<4; $i++){
$code  .=  $src[ rand(0, strlen($src) )]; 
}
$c = imagecolorallocate($img,  rand(60,160),  rand(60,160),  rand(60,160));
imagestring( $img,  5,  20, 7, $code,  $c);
 
//把图片发送给客户端
imagepng($img);
imagedestroy($img); //在服务器内存中销毁此图片
?>
posted @ 2018-01-10 23:05  journeyIT  阅读(2)  评论(0编辑  收藏  举报