數組序列化和反序列化

<?php

  header("content-type:text/html;charset=utf-8");

  //作用:登錄處理(驗證登錄是否成功)

  //獲得表單提交的用戶名、密碼

  $userName = $_POST["userName"];

  $password = $_POST["password"];

  //獲得user.txt中的用戶列表信息

  $user = "";

  $handle = fopen("user.txt","r");

  while($str=fgets($handle))

  {

    $user .= $str;

  }

  fclose($handle);

  $userList = unserialize($user);//二維數組

  

  

?>

<?php

header("content-type:text/html;charset=utf-8");

//作用:將$userList二維數組寫入user.txt文件中

$userList = array(

       array("userName"=>"hello","password"=>"123456"),

       array("userName"=>"張三","password"=>"123456"),

       array("userName"=>"tom","password"=>"123456"),

       array("userName"=>"田七","password"=>"123456")

);

$handle = fopen("user.txt","w");

fputs($handle,serialize($userList));

fclose($handle);

?>

<html>

  <head>

 <title>會員登錄</title>

<meta http-equiv="content-type" content="text/html;charset=utf-8">

    <meta charset=utf-8"/>

  </head>

  <body>

    <form  name="frm" method="post" action="check.php">

    <table  border="1" align="center">

      <tr>

        <td>登錄名稱:</td>

        <td><input type="text" name="userName" size="20"></td>

      </tr>

      <tr>

        <td>登錄密碼:</td>

        <td><input type="password" name="password" size="20"></td>

      </tr>

      <tr>

        <td colspan="2" align="center">

          <input  type="submit" value="登錄">

          &nbsp;&nbsp;&nbsp;

          <input  type="reset" value="重置">

          

      </tr>

    </table>

    </form>

  </body>

</html>

<?php

header("content-type:text/html;charset=utf-8");

$handle = fopen("haha.txt","r");

$str = fgets($handle);

fclose($handle);

echo  $str;

?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章