1. 程式人生 > >PHP中的亂碼問題

PHP中的亂碼問題

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" />,來指定編碼格式。