服务器信息

1.$_cookie

2.$_get

3.$_post

4.$_file

5.$_server

6.$_env

一.会话session

1.如何启用会话

session_start()

注册会话 session_register(会话名)

判断一个会话是否被注册session_is_registered(会话名)

实例,制作一个简单的登录程序

本程序分为4个页面 login.php checklogin.php function.php login_out.php

login.php

 
  
  1. <? 
  2.  session_start();//开启会话  
  3.  $username=$_SESSION['username'];//从会话中读取用户名  
  4.  $password=$_SESSION['password'];//从会话中读取密码  
  5. ?> 
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  7. <html xmlns="http://www.w3.org/1999/xhtml"> 
  8. <head> 
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  10. <title>用户登录</title> 
  11. </head> 
  12. <!--用JS验证表单是否为空--> 
  13. <script language="javascript"> 
  14.  function check_login()  
  15.  {  
  16.      var f=document.form1;  
  17.      var username=f.username.value;  
  18.      var password=f.password.value;  
  19.      if(username=="")  
  20.      {  
  21.          alert("请输入用户名");  
  22.          f.username.focus();//让焦点回到文本域  
  23.          return false;//中断程序  
  24.      }  
  25.      if(password=="")  
  26.      {  
  27.          alert("请输入密码");  
  28.          f.password.focus();  
  29.          return false;  
  30.      }  
  31.      return true;  
  32. }  
  33. </script> 
  34. <body> 
  35. <? 
  36.  if($username=='' and $password=='')//判断会话中用户名和密码是否为空  
  37.  {  
  38. ?> 
  39. <form id="form1" name="form1" method="post" action="checklogin.php" onsubmit="return check_login()"> 
  40.   <table width="275" border="0" align="center"> 
  41.     <tr align="center"> 
  42.       <td colspan="2">用户登录</td> 
  43.     </tr> 
  44.     <tr> 
  45.       <td width="71">用户名:</td> 
  46.       <td width="194" height="22"><label for="textfield"></label> 
  47.       <input type="text" name="username" id="textfield" /></td> 
  48.     </tr> 
  49.     <tr> 
  50.       <td>密码:</td> 
  51.       <td height="22"><label for="textfield2"></label> 
  52.       <input type="text" name="password" id="textfield2" /></td> 
  53.     </tr> 
  54.     <tr> 
  55.       <td>&nbsp;</td> 
  56.       <td><input type="submit" name="button" id="button" value="提交" /> 
  57.       <input type="reset" name="button2" id="button2" value="重置" /> 
  58.       <input name="action" type="hidden" id="action" value="login" /></td> 
  59.     </tr> 
  60.   </table> 
  61.     
  62. <? } else {?> 
  63. </form> 
  64. <table width="275" border="0" align="center"> 
  65.     <tr align="center"> 
  66.       <td colspan="2">用户信息</td> 
  67.     </tr> 
  68.     <tr> 
  69.       <td width="71">用户名:</td> 
  70.       <td width="194" height="22"><? echo $username;?> </td> 
  71.     </tr> 
  72.     <tr> 
  73.       <td>密码:</td> 
  74.       <td height="22"><? echo $password; ?></td> 
  75.     </tr> 
  76.     <tr> 
  77.       <td>&nbsp;</td> 
  78.       <td>进入管理页面  
  79.           <a href="login_out.php">退出登录</a></td> 
  80.     </tr> 
  81.   </table> 
  82.   <? }?> 
  83. </body> 
  84. </html> 

checklogin.php

 
  
  1. <? 
  2. session_start();//开启会话  
  3. header("Content-Type:text/html; charset=utf-8");  
  4. include('function.php');  
  5. $action=$_REQUEST['action'];  
  6. $username=$_POST['username'];  
  7. $password=$_POST['password'];  
  8. if ($action=='login')//防恶意登录 隐藏域的值是否为login  
  9. {  
  10.     if($username=='prometheus' and $password=='sm520517')//判断会话中用户名和密码  
  11.     {  
  12.           
  13.         $_SESSION['username']=$username;  
  14.         $_SESSION['password']=$password;  
  15.         msg_box('恭喜你,登录成功','login.php');  
  16.     }  
  17.     else  
  18.     {  
  19.         history_box('对不起,你输入的用户名或密码错误');  
  20.           
  21.     }  
  22. }  
  23. else  
  24. {  
  25.     seturl('login.php');  
  26.       
  27. }  
  28. ?> 

function.php

 
  
  1. <? 
  2. //提示文本  
  3. function msg_box($text,$url) //声明提示框函数  
  4. {  
  5.     $msg="<script>alert('$text');location.href='$url';</script>";  
  6.     echo $msg;  
  7. }  
  8. //后退上一步  
  9. function history_box($text)  
  10. {  
  11.     $msg="<script>alert('$text');history.go(-1);</script>";  
  12.     echo $msg;  
  13. }  
  14. //直接跳转页面  
  15. function seturl($url)  
  16. {  
  17.     $msg="<script>location.href='$url';</script>";  
  18.     echo $msg;  
  19. }  
  20. //注销登录  
  21. function dellogin($text,$url)  
  22. {  
  23.     $msg="<script>alert('$text');location.href='$url';</script>";  
  24.     echo $msg;  
  25. }  
  26. ?> 

login_out.php

 
  
  1. <? 
  2. session_start();  
  3. header("Content-Type:text/html; charset=utf-8");  
  4. include('function.php');  
  5. session_destroy();  
  6. dellogin('您已成功退出登录','login.php');  
  7. ?> 

由这四个页面组成了完整的登录程序

注意:1.php与HTML包含的写法

            2.script中的写法

 二 cookie

注册一个cookie

setcookie('键','值');

注销一个会话

setcookie('键','',time()-1)