A-A+

php jquery ajax注册与登录退出例子

2016年01月07日 前端设计 暂无评论 阅读 5 views 次

利用ajax注册或登录是非常的简单的个事情了,我们经常会碰到此类问题了,下面小编就来为各位介绍一下操作步骤吧。

  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">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>无标题文档</title>  
  6. <script src="temp/js/jquery-1.6.4.js" type="text/javascript"></script>  
  7. <script type="text/javascript">  
  8. // JavaScript Document  
  9. $(function(){  
  10. $(".cz").click(function(){  
  11. $(":text").val("");  
  12. $(":password").val("");  
  13. });  
  14. $(".zc").click(function(){  
  15. if($(":text").val()==""||$(":password").val()==""){  
  16. alert("不允许为空!");  
  17. return false;  
  18. }  
  19. var b=new Array();  
  20. $(":input").each(function(i){  
  21. if(i<=1){  
  22. b[i]=$(this).val();  
  23. }  
  24. });  
  25.   
  26. $.get(  
  27. "reg.php",  
  28. {"b":b,  
  29.   "time":Math.random()  
  30. },  
  31. function(d){  
  32. if(d=="ok"){  
  33. window.location="index.php";  
  34. }  
  35. }  
  36. );  
  37. })  
  38.   
  39. $(".tuichu").click(function(){  
  40. $.get(  
  41. "reg.php",  
  42. {"exit":"0",  
  43.   "ddd":Math.random()  
  44. },function(d){  
  45. if(d=="ok"){  
  46. window.location="index.php";  
  47. }  
  48. }  
  49. );  
  50. })  
  51. });  
  52. </script>  
  53. </head>  
  54. <body>  
  55. <table width="350" height="100%" border="0" align="center">  
  56.   <tr height="180">  
  57.     <th scope="row">&nbsp;</th>  
  58.   </tr>  
  59.   <tr>  
  60.     <th scope="row"><table width="395" border="0" cellspacing="1" bgcolor="#999999">  
  61. <tr>  
  62.   <th height="28" colspan="2" bgcolor="#CCCCCC" >已登录</th>  
  63.   </tr>  
  64. <tr bgcolor="#FFFFFF">  
  65.   <th width="28%" height="28" scope="row" align="right">用户名:</th>  
  66.   <td height="28" align="left"><?php echo iconv("utf-8","gbk",$_SESSION['n']); ?>&nbsp;</td>  
  67.   </tr>  
  68. <tr bgcolor="#FFFFFF">  
  69.   <th height="27" scope="row" align="right">密&nbsp;&nbsp;码:</th>  
  70.   <td height="27" align="left"><?php echo $_SESSION['m']; ?>&nbsp;</td>  
  71.   </tr>  
  72.     
  73.  <tr>  
  74.   <th height="28" colspan="2" bgcolor="#FFFFFF" ><input class="tuichu" value="退出" type="button" /></th>  
  75.   </tr>  
  76.     </table></th>  
  77.   </tr>  
  78.   <tr>  
  79.     <th scope="row">&nbsp;</th>  
  80.   </tr>  
  81. </table>  
  82. </body>  
  83. </html>  

注册页面,代码如下:

  1. <title>注册页面</title>  
  2. </head>  
  3. <body>  
  4. <table width="350" height="100%" border="0" align="center">  
  5.   <tr height="180">  
  6.     <th scope="row">&nbsp;</th>  
  7.   </tr>  
  8.   <tr>  
  9.     <th scope="row"><table width="395" border="0" cellspacing="1" bgcolor="#999999">  
  10.       <tr>  
  11.         <th height="28" colspan="2" bgcolor="#CCCCCC" >注册页面</th>  
  12.         </tr>  
  13.       <tr bgcolor="#FFFFFF">  
  14.         <th width="28%" height="28" scope="row" align="right">用户名:</th>  
  15.         <td height="28" align="left"><input name="r[]" type="text" />&nbsp;</td>  
  16.         </tr>  
  17.       <tr bgcolor="#FFFFFF">  
  18.         <th height="27" scope="row" align="right">密&nbsp;&nbsp;码:</th>  
  19.         <td height="27" align="left"><input name="r[]" type="password" />&nbsp;</td>  
  20.         </tr>  
  21.       <tr bgcolor="#FFFFFF">  
  22.       <th height="35" colspan="2" scope="row"> <input class="zc" type="button" value="注册" />&nbsp;<input name="重置" type="button" class="cz" value="重置" /></th>  
  23.       </tr>  
  24.     </table></th>  
  25.   </tr>  
  26.   <tr>  
  27.     <th scope="row">&nbsp;</th>  
  28.   </tr>  
  29. </table>  
  30.   
  31. </body>  
  32. </html>  

php处理页面,代码如下:

  1. session_start();  
  2. if(!emptyempty($_SESSION['n'])){  
  3.  include_once './temp/index.html';  
  4. }else {  
  5.  include_once './temp/log.html';  
  6. }  

reg.php页面

  1. session_start();  
  2. if(isset($_GET["b"])){  
  3.  $_SESSION['n']=$_GET["b"][0];  
  4.  $_SESSION['m']=$_GET["b"][1];  
  5.   
  6.  echo "ok";  
  7.  exit();  
  8. }  
  9.   
  10. if(isset($_GET['exit'])){  
  11.  $_SESSION['n']="";  
  12.  unset($_SESSION['n']);  
  13.  $_SESSION['m']="";  
  14.  unset($_SESSION['m']);  
  15.    
  16.  echo "ok";  
  17.  exit();  
  18. };  

给我留言