打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
PHP登录表单例程

 代码:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN" dir="ltr">  
  3. <head profile="http://gmpg.org/xfn/11">  
  4. <title>Login登录演示示例-www.jbxue.com</title>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  6. <meta http-equiv="imagetoolbar" content="no" />  
  7. </head>  
  8.   
  9. <?php   
  10.     function userauth( $user$passwd ) {  
  11.         //TODO: 在这里应该连接数据库验证用户  
  12.         if($user=="test"&&$passwd=="0000")return true;  
  13.         else return false;  
  14.     }  
  15. ?>  
  16.   
  17. <body id="top">  
  18. <?php  
  19.     if$_SERVER['REQUEST_METHOD']!='POST' ){   
  20.         //确认这是一个GET请求,而不是POST请求。  
  21. ?>  
  22.   
  23. <!-- <form action="/login.php?next=<?php echo urlencode($_SERVER['REQUEST_URI']); ?>" method="post"> -->  
  24. <?php   
  25.     #如果从其他页面提交过来,可以在action中指定next参数,一般指定为自身URL,  
  26.     #这样登入成功可以重定向回来。$_SERVER['REQUEST_URI']表示自身URL。通过浏览器查看本页面的源代码可以看到产生的输出。*/   
  27. ?>  
  28.   
  29. <form action="/login.php" method="post">  
  30.     <div>请输入账号和密码:</div><br/>  
  31.     <?php if( isset($_GET["r"]) ){ //验证失败重定向过来则显示错误信息。 ?>  
  32.     <div style="color:red;">登录失败</div><br/>  
  33.     <?php } ?>  
  34.     <div>  
  35.         <input id="txtUser" name="txtUser" type="text" />  
  36.         <input id="txtPasswd" name="txtPasswd" type="password" />  
  37.     </div><br/>  
  38.     <div>  
  39.         <input type="submit" name="btnlogin" id="btnlogin" value="OK" />  
  40.     </div>  
  41. </form>  
  42.   
  43. <?php  
  44.     }else{  
  45.         if( !userauth($_POST['txtUser'],$_POST['txtPasswd']) )   
  46.             Header("Location: /login.php?r=err"); //没有通过验证,重定向回去  
  47.         else{  
  48.             //TODO:设置Cookie或Session,记录用户的登入状态。  
  49.             #如果设置Cookie则必须加密,如果设置Session则网站不能在集群服务器或多核负载均衡下工作,所以各有利弊。  
  50.             if ( isset($_GET["next"]) ){  
  51.                 Header("Location: /".$_GET["next"]); //通过验证,重定向到next参数指定的页面  
  52.             }  
  53.         }  
  54. ?>  
  55. <div>  
  56.     <h2>您好!<?php echo $_POST['txtUser'] ?></h2>  
  57.     <p>欢迎光临本站。</p><br/>  
  58.     <!-- <p><a href="profile">? 账户</a> | <a href="/logout">退出 ?</a></p> -->  
  59. </div>  
  60. <?php   
  61.     }   
  62.     #注意括号对应!要注意把动态语言和静态语言分清楚。  
  63.     #这就好比做分次积分。服务器执行动态语言好比第一次积分,静态代码这时候充其量都是字符串。  
  64.     #客户端运行静态代码和脚本代码好比第二次积分,这时动态语言中该输出的早已输出,都是常量了。  
  65. ?>  
  66.   
  67. <script type="text/javascript">  
  68.     <?php #这些javascript负责在客户端对表单合法性完成初步的校验。至少各项不空才提交,节省服务器的计算资源。 ?>  
  69.     function checkfields(){  
  70.         var uname=document.getElementById("txtUser").value;  
  71.         //var ualias=document.getElementById("txtName").value;  
  72.         var upwd=document.getElementById("txtPasswd").value;  
  73.         //var upwd2=document.getElementById("txtPasswdAgain").value;  
  74.         var buttonSubmit=document.getElementById("btnlogin");  
  75.         //if(uname.match(/^[a-z0-9A-Z]+$/)&&upwd==upwd2&&upwd!=""&&ualias.match(/^[a-z0-9A-Z\u4E00-\u9FA5\u00A0-\u00FF\u0400-\u052F]+$/))buttonSubmit.disabled=false;  
  76.         if(uname.match(/^[a-z0-9A-Z]+$/)&&upwd!="")buttonSubmit.disabled=false;  
  77.         else buttonSubmit.disabled=true;  
  78.     }  
  79.     window.setInterval('checkfields()',100);  
  80. </script>  
  81. </body>  
  82. </html> 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
javascript表单验证大全,
AJAX技术在PHP开发中的简单应用
常用JS語句 - DOoRle‘s Blog
Html5页面使用javascript setCutomValidity()函数验证表单判断输入
显示时间
掌握 ajax,第 1 部分: ajax 简介
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服