PHP编译过程中的乱码是大家都头痛的问题,今天我也遇到了一个特殊的乱码问题,贴出来共享下。
问题:采用UTF-8编码的页面,在浏览器中初次打开正常,提交后始终显示乱码,网上的多种解决方法无效,在浏览器中单击“右键”——源文件,显示的内容是正的,就是在浏览器中显示不正常。
PHP源码:
<?php
session_start();
if(isset($_POST['submit'])){
if($_POST['username']=="sunyang"){
if($_POST['password']=="sunyang"){
$_SESSION['userinfo']=array('username'=>$_POST['username'],'password'=>$_POST['password'],'name'=>'三杨科技');
echo "登录成功</br>";
echo "<a href=checkfolder.php>创建网络文件夹</a>";
exit;
}else{
echo "用户密码错误";
}
}else{
echo "用户名不正确";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录页面</title>
</head>
<body>
<form action="index.php" method="post">
<table border="1" align="center" width="400" height="200" bgcolor="gray" style="text-align:center">
<tr><td colspan="2">用户登录</td></tr>
<tr>
<td>用户名:</td><td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密码:</td><td><input type="text" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" />||<input type="reset" />||<a href="registeruser.php">注册</a></td>
</tr>
</table>
</form>
</body>
</html>
解决方法:将PHP代码部分写入到网页里面,即HTML标签里面问题解决。希望对大家遇到此问题的解决时有所帮助。或者在PHP代码中加入header('Content-Type:text/html;charset=utf-8');
注意:在纯PHP代码页面也可以通过在页首添加<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />,来指定编码格式。