这次的作业是文件的写入与读取,写一个注册页面,注册信息写入到目录下某文件,然后写一个登录页面,验证用户的登录信息.
这是注册页面
- <html>
- <head>
- <title>新用户注册</title>
- </head>
- <body text="#030060" style="font:12px;">
- <h3>注册信息</h3>
- <form action="login.php" method="POST">
- 用户名: <input type="text" size="18" maxlength="14" name="userid" /> *<br />
- 密 码: <input type="password" size="19" maxlength="16" name="userpw" /> *<br />
- 确认密码: <input type="password" size="19" maxlength="16" name="userpw_p" /> *<br />
- <br />
- <input type="submit" value="提交注册" name="submit" />
- </form>
- </body>
- </html>
注册后的提示页面:
- <html>
- <body text="#036060" style="font:12px">
- <?php
- $userid=$_POST['userid']; $userpw=$_POST['userpw']; $userpw_p=$_POST['userpw_p'];
- if ($userid==""||$userpw=="")
- {
- echo '*为必填项!';
- }
- elseif (strlen($userid)<2)
- {
- echo '用户名不能小于2位!';
- }
- elseif (strlen($userpw)<3)
- {
- echo '密码不能小于3位!';
- }
- elseif ($userpw_p!=$userpw)
- {
- echo '确认密码不正确!';
- }
- else
- {
- echo '注册成功!欢迎您,'.$userid.'!<br /><br />';
- echo '<a href="user.php">您可以点此登录</a>';
- //$rootname=$_SERVER['DOCUMENT_ROOT'];
- $filename=fopen("txt.txt",'a+');
- if ($filename)
- {
- $str = "$userid-$userpw\n";
- $len = fwrite($filename,$str );
- fclose($filename);
- }
- }
- ?>
- </body>
- </html>
登录后的提示页面:
- <html>
- <head>
- <title>登录</title>
- </head>
- <body text="#030060" style="font:12px;">
- <h3>登录</h3>
- <form action="" method="POST">
- 用户名: <input type="text" size="18" maxlength="14" name="userid" /> *<br />
- 密 码: <input type="password" size="19" maxlength="16" name="userpw" /> *<br />
- <br />
- <input type="submit" value="登录" name="submit" />
- </form>
- </body>
- <?php
- $userid=$_POST['userid'];$userpw=$_POST['userpw'];
- $file='txt.txt';
- $filename=fopen($file,'r');
- //$myfile=file($file);
- $text=fread($filename,filesize($file));
- fclose($filename);
- $xx=$userid.'-'.$userpw;
- if ($userid=="")
- {
- echo '请登录,<a href=reg.php>或点此注册</a>';
- }
- else
- {
- if (stristr($text,$xx))
- {
- echo "登录成功,欢迎您回来,$userid!";
- echo '<br />';
- echo '很可惜,您登录了可是什么也不能干,<a href=reg.php>要不您点这再注册一遍?^_^</a>';
- }
- else
- {
- echo '登录失败,请检查用户名及密码是否正确!';
- }
- }
- ?>
- </html>
演示:
http://zhaopeng.info/myphp/zhuce/
呵呵,暂时只能写到这,肯定有不少漏洞,欢迎大家拍砖~


#1