zoukankan      html  css  js  c++  java
  • 关于【会话控制】的题

    一:COOKIE应用

    *setcookie()函数:setcookie ("自设变量名", "传递的变量", time()+—毫秒数);

    1.控制登录用户的过期时间

    编写用户登陆界面

    <table width="464" height="336" border="0" cellpadding="0" cellspacing="0" background="images/bg.JPG">
      <tr>
        <td width="107" height="136">&nbsp;</td>
        <td width="274">&nbsp;</td>
        <td width="83">&nbsp;</td>
      </tr>
      <tr>
        <td height="100">&nbsp;</td>
        <td align="center"><form name="form1" method="post" action="index_ok.php">
          <p>
            用户名:<input name="user" type="text" size="20">
          </p>
          <p>
            密码:<input name="pass" type="password" maxlength="20">
          </p>
          <p>
            <input type="submit" name="Submit" value="提交">
          </p>
        </form>      <a href = "cookie.php"></a></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td height="100">&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>

    处理界面,处理用户名和密码,并赋给cookie,设置cookie的过期时间,跳转到cookie.php界面

    <?php
    header ( "Content-type: text/html; charset=UTF-8" ); //设置文件编码格式
    if($_POST['user']!="" && $_POST['pass']!=""){
    if($_POST['user']=="mr" && $_POST['pass']=="mrsoft"){
        setCookie("user",$_POST['user'],time()+60)or die("禁止cookie");
        setCookie("pass",$_POST['pass'],time()+60)or die("禁止cookie");
        echo "<script>alert('登录成功!'); window.location.href='cookie.php';</script>";
    }else{
        echo "<script>alert('用户名或者密码不正确!'); window.location.href='index.php';</script>";
    }
    }else{
        echo "<script>alert('用户名或者密码不能为空!'); window.location.href='index.php';</script>";
    }
    ?>

    cookie.php界面

    <?php
        if($_COOKIE['user']=="mr" && $_COOKIE['pass']=="mrsoft"){
            echo "欢迎".$_COOKIE['user']."光临!";
        }else{
            echo "<script>alert('COOKIE已经过期,请重新登录'); window.location.href='index.php';</script>";
        }
    ?>

    2.删除客户端的cookie值

            <table id="__01" width="580" height="440" border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td colspan="3">
                <img src="images/log_01.jpg" width="580" height="133" alt=""></td>
        </tr>
        <tr>
            <td>
                <img src="images/log_02.jpg" width="122" height="161" alt=""></td>
            <td width="336" height="161"><form action="" method="post" class="STYLE1">
            用户名:
              <input name="name" type="text" id="name" />
              <br>
              <br>&nbsp;码:
              <input type="password" name="pwd" />
              <br>
              <br>
            <i>保存时间:
            <input name="check" type="radio" value="3600" checked="checked" />
            1小时
            <input name="check" type="radio" value="86400" />
            24小时
            <input name="check" type="radio" value="604800" />一星期</i><br>
            <br>
            <input type="submit"name="sub"value="确定" />&nbsp;&nbsp;
            <input type="reset"name="res"value="重置" />
        </form></td>
            <td>
                <img src="images/log_04.jpg" width="122" height="161" alt=""></td>
        </tr>
        <tr>
            <td colspan="3">
                <img src="images/log_05.jpg" width="580" height="146" alt=""></td>
        </tr>
    </table>
    </body>
    </html>
    <?php
        if($_POST['sub']){
            if($_POST['name'] == "" || $_POST['pwd'] == ""){
                echo "<script>alert('用户名或密码不能为空');location.href='index.php'</script>";
            }else{
                echo "<script>window.location.href='main.php'</script>";
            }
            
        }
    ?>
    <?php
    if($_COOKIE['username']!="" &&$_COOKIE['pwd']!=""){
    
        if(isset($_GET[cookie])){
                setcookie("username","",time()-1);
                setcookie("pwd","",time()-1);
                setcookie("IP","",time()-1);
                echo "<script>alert('COOKIE已删除,确认退出?');location.href='index.php'</script>";
        }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>删除指定IP客户端的COOKIE</title>
    </head>
    <body>
    <img src="images/bg.jpg" width="761" height="559" border="0" usemap="#Map">
    <map name="Map"><area shape="rect" coords="622,130,731,185" href="main.php?cookie=0"></map>
    </html>
    <?php
        if(!isset($_COOKIE['IP'])){
            echo"<script>alert('在有限时间内未进行任何操作,请重新登录');location.href='index.php'</script>";
        }
        }else{
            echo"<script>alert('您没有正确登录,请重新登录');location.href='index.php'</script>";
        }
    ?>

    二:SESSION的用法

    1.掌控用户登录的权限

    详细代码略

    2.屏蔽页面刷新对计数器的影响

    <?php 
    session_start();
    if($_SESSION[temp]==""){ //判断$_SESSION[temp]==""的值是否为空,其中的temp为自定义的变量
        if(($fp=fopen("counter.txt","r"))==false){ 
            echo "打开文件失败!";
        }else{ 
            $counter=fgets($fp,1024);        //读取文件中数据
            fclose($fp);                    //关闭文本文件
            $counter++;                     //计数器增加1
            $fp=fopen("counter.txt","w");   //以写的方式打开文本文件<!---->
            fputs($fp,$counter);            //将新的统计数据增加1
            fclose($fp);    
        }                                   //关闭文    
         $_SESSION[temp]=1;                     //登录以后,$_SESSION[temp]的值不为空,给$_SESSION[temp]赋一个值1
    }
    ?>
    <!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>
    <body>
    <table id="__01" width="761" height="559" border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td colspan="3">
                <img src="images/index_01.jpg" width="761" height="333" alt=""></td>
        </tr>
        <tr>
            <td>
                <img src="images/index_02.jpg" width="543" height="57" alt=""></td>
            <td width="207" height="57" background="images/index_03.jpg"><img src="gd1.php" /></td>
            <td>
                <img src="images/index_04.jpg" width="16" height="57" alt=""></td>
        </tr>
        <tr>
            <td colspan="3">
                <img src="images/index_05.jpg" width="761" height="169" alt=""></td>
        </tr>
    </table>
    <?php 
    header ( "Content-type: text/html; charset=UTF-8" ); //设置文件编码格式
    //以图形的形式输出数据库中的记录数
    
    if(($fp=fopen("counter.txt","r"))==false){
        echo "打开文件失败!";
    }else{
        $counter=fgets($fp,1024);
        fclose($fp);
        //通过GD2函数创建画布
        $im=imagecreate(200,24);
        $gray=imagecolorallocate($im,255,255,255);
        $color =imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));    //定义字体颜色
        //输出中文字符
        $text="网站的访问量:";                           //对指定的中文字符串进行转换
        $font = "Fonts/FZHCJW.TTF";  
        imagettftext($im,14,0,20,18,$color,$font,$text);                           //输出中文
        //输出网站的访问次数
        imagestring($im,5,160,5,$counter,$color);
        imagepng($im);
        imagedestroy($im);
    }      
    
    ?>

    3.在不同页面之间传递数据

    if($_POST['user']!="" && $_POST['pwd']!=""){    //判断用户名和密码是否为空
            if($_POST['user']=="mr" && $_POST['pwd']=="mrsoft"){        //判断管理员用户名和密码是否正确
                $_SESSION['user'] = $_POST['user'];
                $_SESSION['pwd'] = $_POST['pwd'];
                $_SESSION['check'] = $_POST['check'];
                echo "<script>alert('管理员登录成功!');window.location.href='ini.php'</script>";
            }else{
                echo "<script>alert('用户名或者密码不正确!');window.location.href='index.php'</script>";
            }
        }else{
            echo "<script>alert('登录用户名或者密码不能为空!');window.location.href='index.php'</script>";
        }
    echo "当前时间:". date('Y-m-d H:i:s')."<br>";
        echo "<br>";
        echo "用户名:".$_SESSION['user']."<br>";
        echo "<br>";
        echo "密码:".$_SESSION['pwd']."<br>";
        echo "<br>";
        echo "时效:".$_SESSION['check']."秒<br>";
        echo "<br>";    
        echo "<a href='logout.php'>退出</a>";    
    ?></td>
    <?php 
    session_start();
    session_destroy();
    echo "<script>alert('退出登录!');window.location.href='index.php';</script>";
    ?>

    4.session购物车

    具体代码见购物车博客随笔

    5.session更换聊天室界面

    <!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>应用Session技术实现聊天室换肤</title>
    <style type="text/css">
    <!--
    .style6 {
        font-size: 24px;
        color: #000000;
        font-weight: bold;
    }
    .STYLE7 {
        font-size: 12px;
        color: #E32FDA;
    }
    body,td,th {
        font-size: 12px;
    }
    body {
        margin-left: 0px;
        margin-top: 0px;
        margin-right: 0px;
        margin-bottom: 0px;
    }
    -->
    </style>
    </head>
    <?php
        $_SESSION['bgcolor']=$_GET['col'];
    ?>    
    <body bgcolor="<?php if($_SESSION['bgcolor']==""){echo "white";}else{echo $_SESSION['bgcolor'];}?>">
    <table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr align="center" valign="middle">
        <td height="115" colspan="3" background="images/01.jpg" class="style6"><table width="700" border="0" cellspacing="0" cellpadding="0">
          
          <tr>
            <td width="227">&nbsp;</td>
            <td width="349">&nbsp;</td>
            <td width="106"><img src="images/02.jpg" width="240" height="89" border="0" usemap="#Map" /></td>
          </tr>
          
        </table></td>
      </tr>
      <tr valign="middle">
        <td height="5" colspan="3" valign="top"></td>
      </tr>
      <tr valign="middle">
        <td width="179" height="350" valign="top" bgcolor="#DFEEFF"><br>
          <span class="STYLE7">&nbsp;&nbsp;***进入聊天室,欢迎光临!!<br>
    &nbsp;&nbsp;<?php echo date("y-m-d h:i:s");?></span></td>
        <td width="2" valign="top"></td>
        <td width="700"  valign="top" bgcolor="#FFFFFF"><p><br>
      &nbsp;&nbsp;&nbsp;&nbsp;</p>
          <p> &nbsp;&nbsp;&nbsp;&nbsp;在这里显示聊天的内容,希望大家遵守聊天室的规则,</p>
          <p>  &nbsp;&nbsp;&nbsp;&nbsp;不要在聊天室中传播不健康的和非法的内容,谢谢合作! </p>
          </span></td>
      </tr>
      
      <tr valign="middle">
        <td height="2" colspan="3"></td>
      </tr>
      <tr valign="middle">
        <td height="50" colspan="3" bgcolor="#FFC7FF">
          &nbsp;&nbsp;&nbsp;&nbsp;选择您喜欢的颜色,您的页面也将随即更新</td>
      </tr>
    </table>
    
    <map name="Map" id="Map">
    <area shape="rect" coords="4,3,27,26" href="index.php?col=0066FF" />
    <area shape="rect" coords="64,5,87,26" href="index.php?col=00CCFF" />
    <area shape="rect" coords="31,34,55,56" href="index.php?col=999900" />
    <area shape="rect" coords="218,64,237,85" href="index.php?col=CC9933" />
    <area shape="rect" coords="184,33,209,55" href="index.php?col=CC6600" /><area shape="rect" coords="214,7,237,28" href="index.php?col=3399FF" />
    <area shape="rect" coords="8,63,27,87" href="index.php?col=996633" />
    <area shape="rect" coords="65,61,88,87" href="index.php?col=99CC33" />
    <area shape="rect" coords="152,61,177,87" href="index.php?col=CC3333" />
    </map></body>
    </html>

    6.清理session

    $_SESSION = array();
            session_destroy();
            echo "<script>alert('清理SESSION缓存成功');
  • 相关阅读:
    HDU 5090 Game with Pearls
    HDU 1394 Minimum Inversion Number
    HDU 1698 Just a Hook
    POJ 2104 K-th Number
    UVA 1160
    HDU 5895 Mathematician QSC
    HDU 3294 Girls' research
    HDU 3068 最长回文
    PyCharm每日技巧-1
    如何一年考过日语一级
  • 原文地址:https://www.cnblogs.com/xingyue1988/p/6442329.html
Copyright © 2011-2022 走看看