服务器信息
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
- <?
- session_start();//开启会话
- $username=$_SESSION['username'];//从会话中读取用户名
- $password=$_SESSION['password'];//从会话中读取密码
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>用户登录</title>
- </head>
- <!--用JS验证表单是否为空-->
- <script language="javascript">
- function check_login()
- {
- var f=document.form1;
- var username=f.username.value;
- var password=f.password.value;
- if(username=="")
- {
- alert("请输入用户名");
- f.username.focus();//让焦点回到文本域
- return false;//中断程序
- }
- if(password=="")
- {
- alert("请输入密码");
- f.password.focus();
- return false;
- }
- return true;
- }
- </script>
- <body>
- <?
- if($username=='' and $password=='')//判断会话中用户名和密码是否为空
- {
- ?>
- <form id="form1" name="form1" method="post" action="checklogin.php" onsubmit="return check_login()">
- <table width="275" border="0" align="center">
- <tr align="center">
- <td colspan="2">用户登录</td>
- </tr>
- <tr>
- <td width="71">用户名:</td>
- <td width="194" height="22"><label for="textfield"></label>
- <input type="text" name="username" id="textfield" /></td>
- </tr>
- <tr>
- <td>密码:</td>
- <td height="22"><label for="textfield2"></label>
- <input type="text" name="password" id="textfield2" /></td>
- </tr>
- <tr>
- <td> </td>
- <td><input type="submit" name="button" id="button" value="提交" />
- <input type="reset" name="button2" id="button2" value="重置" />
- <input name="action" type="hidden" id="action" value="login" /></td>
- </tr>
- </table>
- <? } else {?>
- </form>
- <table width="275" border="0" align="center">
- <tr align="center">
- <td colspan="2">用户信息</td>
- </tr>
- <tr>
- <td width="71">用户名:</td>
- <td width="194" height="22"><? echo $username;?> </td>
- </tr>
- <tr>
- <td>密码:</td>
- <td height="22"><? echo $password; ?></td>
- </tr>
- <tr>
- <td> </td>
- <td>进入管理页面
- <a href="login_out.php">退出登录</a></td>
- </tr>
- </table>
- <? }?>
- </body>
- </html>
checklogin.php
- <?
- session_start();//开启会话
- header("Content-Type:text/html; charset=utf-8");
- include('function.php');
- $action=$_REQUEST['action'];
- $username=$_POST['username'];
- $password=$_POST['password'];
- if ($action=='login')//防恶意登录 隐藏域的值是否为login
- {
- if($username=='prometheus' and $password=='sm520517')//判断会话中用户名和密码
- {
- $_SESSION['username']=$username;
- $_SESSION['password']=$password;
- msg_box('恭喜你,登录成功','login.php');
- }
- else
- {
- history_box('对不起,你输入的用户名或密码错误');
- }
- }
- else
- {
- seturl('login.php');
- }
- ?>
function.php
- <?
- //提示文本
- function msg_box($text,$url) //声明提示框函数
- {
- $msg="<script>alert('$text');location.href='$url';</script>";
- echo $msg;
- }
- //后退上一步
- function history_box($text)
- {
- $msg="<script>alert('$text');history.go(-1);</script>";
- echo $msg;
- }
- //直接跳转页面
- function seturl($url)
- {
- $msg="<script>location.href='$url';</script>";
- echo $msg;
- }
- //注销登录
- function dellogin($text,$url)
- {
- $msg="<script>alert('$text');location.href='$url';</script>";
- echo $msg;
- }
- ?>
login_out.php
- <?
- session_start();
- header("Content-Type:text/html; charset=utf-8");
- include('function.php');
- session_destroy();
- dellogin('您已成功退出登录','login.php');
- ?>
由这四个页面组成了完整的登录程序
注意:1.php与HTML包含的写法
2.script中的写法
二 cookie
注册一个cookie
setcookie('键','值');
注销一个会话
setcookie('键','',time()-1)
转载于:https://blog.51cto.com/phpmylove/641345