天天看点

数组序列化和反序列化

<?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);//二维数组

?>

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>

        <td>登录密码:</td>

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

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

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

             

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

    </table>

    </form>

  </body>

</html>

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

$str = fgets($handle);

echo  $str;

继续阅读