php 关闭页面后session自动销毁二种方法

 更新时间:2016年11月25日 15:10  点击:1709

浏览器关闭的过程中,服务器上的 session 数据并没有被销毁,因为这时候没有发送任何请求,服务器那边不会知道是否要销毁 session 的数据。浏览器再次启动重新打开页面的时候,发送的 session id 还是原来的 id,虽然你说没用到 cookie,但是这个 id 就是通过 cookie 发送的。

你可以看看 php.ini 的设置,主要看这两项:

 代码如下 复制代码
session.gc_maxlifetime
session.cookie_lifetime


方法二利用onload函数

 代码如下 复制代码

<body onunload="ajax()">
function ajax(){
使用ajax执行unset($_session);
}

在php中有这么一个函数checkdnsrr来验证dns是否可访问来检测邮箱地址是否存在
*/

 代码如下 复制代码

$email ="abc@111cn.net";
$check_email = checkdnsrr($email,"a");

if($check_email) {

return true;

} else {

return false;

}


/*
关于checkdnsrr函数详细说明

bool checkdnsrr ( string $host [, string $type = "mx" ] )

checkdnsrr. 检查指定网址的dns 记录

<?php

 代码如下 复制代码

//this will not work
if(checkdnsrr("round-robin-example.com"),"all")){
     return true;
}else{
     return false;
}

//but every value other than "any" will work
if(checkdnsrr("round-robin-example.com"),"a")){
     return true;
}else{
     return false;
}

指定的参数 host 可以是网络位址 (ip address),也可以用机器名称 (domain name)。参数 type 可以省略,内定值为 mx。而参数 type 的值可为以下的其中之一:a、mx、ns、soa、ptr、cname 或 any。若找到了指定网址的 dns 字段,返回 true;若未找到指定的 dns 字段或是有错误均会返回 false。

主要是购物车部分代码的讲解,和对session操作的php教程代码演示。

<?php
//
// add_item.php:
//  add an item to the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
    session_register('cart');
}

require 'lib.inc.php'; // loadproducts()

loadproducts(); // load products in $master_products_list

// make $curr_product global
$curr_product = array();

// loop through all the products and pull up the product
// that we are interested in

 

foreach ($master_products_list as $prod_id => $product) {
    if (trim($prod_id) == trim($_get[id])) {
        $curr_product = $product;
    }
}


// register our session
//session_register('cart');
//if(session_is_registered('cart')) echo "已经注册";

 

if ($_post[ordered]) {  // if they have chosen the product

    array_push($_session[cart][products], array(trim($_post[id]), $_post[quantity]));
    $_session[cart][num_items] += $_post[quantity];
 
}
?>

<html>
<head>
    <title>
    <?php if ($_post[ordered]) {  ?>
        已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮
    <?php } else {  ?>
        添加 <?php echo $curr_product[name]; ?> 到您的购物篮
    <?php } ?>
    </title>
</head>
<body>
<?php if ($_post[ordered]) {  ?>
    <h1><?php echo $curr_product[name]; ?>
        添加至购物篮成功</h1>

    <a href="cart.php">返回</a> 商品列表页面.
<?php }  else {  ?>
    <h1>添加 <?php echo $curr_product[name]; ?> 到您的购物篮</h1>

    <form action="<?php echo $php_self; ?>" method="post">
    商品名称: <?php echo $curr_product[name]; ?>
    <br>
    商品说明: <?php echo $curr_product[desc]; ?>
    <br>
    商品单价: rmb<?php echo $curr_product[price]; ?>
    <br>
    商品数量: <input type="text" size="7" name="quantity">
    <input type="hidden" name="id" value="<?php echo $_get[id]; ?>">
    <input type="hidden" name="ordered" value="1">

    <input type="submit" value="添加至购物栏">
    </form>
<?php } ?>
</body>
</html>


php代码

<?php
//
// cart.php:  the main file
//
session_start();

require 'lib.inc.php';
//判断购物篮会话变量cart是否注册,不注册则注册cart变量
if (session_is_registered('cart')) {
    session_register('cart');
}


// 如果购物篮没有初始化,则初始化购物篮
if (!isset($_session[cart][num_items])) {
    $_session[cart] = array("num_items" => 0,
                  "products"  => array());
}


// from site_lib.inc, loads the $master_products_list array
loadproducts(); //载入物品列表
?>

<html>
<head>
    <title>演示会话跟踪的购物篮程序</title>
</head>

<body>

<h1>欢迎进入网上商店</h1>

<?php
if ($_session[cart][num_items]) {  // if there is something to show
?>
<h2>当前在购物篮里的物品</h2>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名称
    </th>
    <th>
        商品说明
    </th>
    <th>
        单价
    </th>
    <th>
        数量
    </th>
    <th>
        &nbsp;
    </th>
</tr>
<?php
  
    // loop through the products
    foreach ($_session[cart][products] as $i => $product) {
        $product_id = $product[0];
        $quantity   = $product[1];

        $total += $quantity *
                  (double)$master_products_list[$product_id][price];
?>
<tr>
    <td>
        <?php echo $master_products_list[$product_id][name]; ?>
    </td>
    <td>
        <?php echo $master_products_list[$product_id][desc]; ?>
    </td>
    <td>
        <?php echo $master_products_list[$product_id][price]; ?>
    </td>
    <td>
        <form action="change_quant.php" method="post">
        <input type="hidden" name="id" value="<?php echo $i; ?>">
        <input type="text" size="3" name="quantity"
                value="<?php echo $quantity; ?>">
    </td>
    <td>
        <input type="submit" value="数量更改">
        </form>
    </td>
</tr>
<?php
    }
?>
<tr>
    <td colspan="2" align="right">
       <b>合计: </b>
    </td>
    <td colspan="2">
        rmb:<?php echo $total; ?>
    </td>
 <td>&nbsp;</td>
</tr>
</table>
<br>
<br>
<?php
}
?>

<h2>商店待出售的商品</h2>
<br>
<i>
    我们提供以下商品待售:
</i>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
    <th>
        商品名称
    </th>
    <th>
        商品说明
    </th>
    <th>
        单价
    </th>
    <th>
        &nbsp;
    </th>
</tr>
<?php
    // show all of the products
    foreach ($master_products_list as $product_id => $item) {
?>
<tr>
    <td>
        <?php echo $item[name]; ?>
    </td>
    <td>
        <?php echo $item[desc]; ?>
    </td>
    <td>
        $<?php echo $item[price]; ?>
    </td>
    <td>
        <a href="add_item.php?id=<?php echo $product_id; ?>">
            添加至购物篮
        </a>
    </td>
</tr>
<?php
    }

?>
</table>

购物车

<?php
//
// change_quant.php:
//   change the quantity of an item in the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
    session_register('cart');
}

// typecast to int, making sure we access the
// right element below
$i = (int)$_post[id];

// save the old number of products for display
// and arithmetic
$old_num = $_session[cart][products][$i][1];

if ($_post[quantity]) {
    $_session[cart][products][$i][1] = $_post[quantity]; //change the quantity
} else {
    unset($_session[cart][products][$i]); // send the product into oblivion
}

// update the number of items
$_session[cart][num_items] = ($old_num >$_post[quantity]) ?
                   $_session[cart][num_items] - ($old_num-$_post[quantity]) :
                   $_session[cart][num_items] + ($_post[quantity]-$old_num);
?>

<html>
<head>
    <title>
        数量修改
    </title>
</head>
<body>
    <h1> 将数量: <?php echo $old_num; ?> 更改为
         <?php echo $_post[quantity]; ?></h1>
    <a href="cart.php">返回</a> 商品列表页面.
</body>
</html>

打开文件

<?php
//物品数组
$master_products_list = array();


//载入物品数据函数
function loadproducts() {
    global $master_products_list;
    $filename = 'products.txt';

    $fp = @fopen($filename, "r")
        or die("打开 $filename 文件失败");
    @flock($fp, 1)
        or die("锁定 $filename 文件失败");

    //读取文件内容
    while ($line = fgets($fp, 1024)) {
        list($id, $name, $desc, $price) = explode('|', $line); //读取每行数据,数据以| 格开
        $id = trim($id); //去掉首尾特殊符号
        $master_products_list[$id] = array("name" =>  $name, //名称
                                           "desc" =>  $desc, //说明
                                           "price" => $price); //单价
    }

    @fclose($fp)  //关闭文件
        or die("关闭 $filename 文件失败");
}
?>

本文章利用用大量的例子来讲解php教程代码对图片操作的讲解。下面来看看一个个实例教程吧.

<?php
$height = 300;
$width = 300;
//创建背景图
$im = imagecreatetruecolor($width, $height);
//分配颜色
$white = imagecolorallocate ($im, 255, 255, 255);
$blue = imagecolorallocate ($im, 0, 0, 64);
//绘制颜色至图像中
imagefill($im, 0, 0, $blue);
//绘制字符串:hello,php
imagestring($im, 10, 100, 120, 'hello,php', $white);
//输出图像,定义头
header ('content-type: image/png');
//将图像发送至浏览器
imagepng($im);
//清除资源
imagedestroy($im);
?>

实例二生成缩略图片

<?php
header("content-type: image/jpeg");
// 载入图像
$imagen1 = imagecreatefromjpeg("imagen1.jpg");
$imagen2 = imagecreatefromjpeg("imagen2.jpg");

// 复制图像
imagecopy($imagen1,$imagen2,0,0,0,0,200,150);

// 输出jpeg图像
imagejpeg($imagen1);

//释放内存
imagedestroy($imagen2);
imagedestroy($imagen1);

?>

获取图片大小信息

<?php
$info = getimagesize("imagen2.jpg");
print_r($info);

?>

绘制png图片

<?php
//png格式图像处理函数
function loadpng ($imgname) {
    $im = @imagecreatefrompng ($imgname);
    if (!$im) {    //载入图像失败                     
        $im = imagecreate (400, 30);     
        $bgc = imagecolorallocate ($im, 255, 255, 255);
        $tc  = imagecolorallocate ($im, 0, 0, 0);
       imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
       imagestring($im, 4, 5, 5, "error loading: $imgname", $tc);
    }
    return $im;
 }
 $imgpng=loadpng("./karte.png");
   /* 输出图像到浏览器 */
 header("content-type: image/png");
 imagepng($imgpng);
 ?>


给图片加文字

<?php
  //创建 100*30 图像
  $im = imagecreate(100, 30);
  // white background and blue text
  $bg = imagecolorallocate($im, 200, 200, 200);
  $textcolor = imagecolorallocate($im, 0, 0, 255);
 
  // write the string at the top left
  imagestring($im, 5, 0, 0, "hello world!", $textcolor);
 
  // output the image
header ("content-type: image/jpeg");
imagejpeg ($im);
imagedestroy($im);

?>

本款教程主要是讲对数据库操作的php代码范例,有php操作mysql数据连接,以及删除数据,查询数据 修改数据,修改更新记录等实例。

简单查询数据

 代码如下 复制代码

<?php
//连接数据库
$link_id = mysql_connect("localhost","root","") or die("连接失败");
if($link_id)
{
 //选择数据库
 mysql_select_db("my_test");
 //以上为头部数据库连接部分,为以下公用的部分。
 if(!$_get[id]){

  //显示用户列表
  $sql = "select * from userinfo";
  $result=mysql_query($sql);
  
  echo "<table border=1>
    <tr>
     <td>编号</td>
     <td>用户名称</td>
     <td>性别</td>
     <td>年龄</td>
     <td>注册时间</td>
     <td>详细信息</td>
    </tr>";

 

  while($row=mysql_fetch_array($result)){
   echo "<tr>
     <td>".$row[id]."</td>
     <td>".$row[username]."www.111cn.net</td>
     <td>".$row[gender]."</td>
     <td>".$row[age]."</td>
     <td>".$row[regdate]."</td>
     <td><a href=query.php?id=".$row[id].">查看</a></td>
    </tr>";
  }
  echo "</table>";
 }
 else
 {  
  //显示指定用户的详细信息
  $sql="select * from userinfo where id=".$_get[id];
  $result=mysql_query($sql);
  $row=mysql_fetch_array($result);
  echo "编号:".$row[id]."<br>用户名:".$row[username]."<br>性别:".$row[gender]."<br>年龄:".$row[age]."<br>注册时间:".$row['regdate'];

  echo "<br><br><br><a href=query.php>继续查询</a>";
 }
}//end if
?>

删除数据

 代码如下 复制代码

<?php
//连接数据库
$link_id = mysql_connect("localhost","root","") or die("连接失败");
if($link_id)
{
 mysql_select_db("my_test");
 if(!$_get[id])
 {

  $result=mysql_query("select * from userinfo");
  echo "<table border=1>
    <tr>
     <td>编号</td>
     <td>用户名称www.111cn.net</td>
     <td>性别</td>
     <td>年龄</td>
     <td>注册时间</td>
     <td>操作</td>
    </tr>";

 

  while($row=mysql_fetch_array($result)){
   echo "<tr>
     <td>".$row[id]."</td>
     <td>".$row[username]."</td>
     <td>".$row[gender]."</td>
     <td>".$row[age]."</td>
     <td>".$row[regdate]."</td>
     <td><a href=delete.php?id=".$row[id].">删除</a></td>
    </tr>";
  }
  echo "</table>";

 }//显示列表的内容
 else
 {

   $sql="delete from userinfo where id=".$_get[id];
   $result=mysql_query($sql);
   if($result)
    echo "记录已经成功删除<br><a href='delete.php'>返回</a>";
   else
    echo "记录删除失败<br><a href=delete.php.php?id=".$_get[id].">返回</a>";

 }//else($id部分)
} // end if
?>

修改,更新记录

 代码如下 复制代码

<?php
//连接数据库
$link_id = mysql_connect("localhost","root","") or die("连接失败");
if($link_id)
{
 mysql_select_db("my_test");
 if(!$_get[id])
 {

  $result=mysql_query("select * from userinfo");
  echo "<table border=1>
    <tr>
     <td>编号</td>
     <td>用户名称</td>
     <td>性别</td>
     <td>年龄</td>
     <td>注册时间</td>
     <td>操作</td>
    </tr>";

 

  while($row=mysql_fetch_array($result)){
   echo "<tr>
     <td>".$row[id]."</td>
     <td>".$row[username]."</td>
     <td>".$row[gender]."</td>
     <td>".$row[age]."</td>
     <td>".$row[regdate]."</td>
     <td><a href=modify.php?id=".$row[id].">编辑</a></td>
    </tr>";
  }
  echo "</table>";

 }//显示列表的内容
 else
 {
  if(!$_post[ok])
  {
   $sql="select * from userinfo where id=".$_get[id];
   $result=mysql_query($sql);
   $row=mysql_fetch_array($result);
   ?>
   <form method=post action='modify.php?id=<? echo $_get[id];?>'>
   <?
   echo $row[id]."<br>"; 
   ?>
   <input type="hidden" name="id" value=<?echo $row[id];?>>
   姓名 www.aimeige.com.cn<input type=text name="username" value=<?echo $row[username];?>><br>
   性别 <input type=text name="gender" value=<?echo $row[gender];?>><br>
   年龄 <input type=text name="age"   value=<?echo $row[age];?>><br>
   注册时间 <input type=text name="regdate"   value=<?echo $row['regdate'];?>><br>
   <input type=submit name=ok value="提交">
   </form>
   <?
  }// if(!$_post[ok])
  else{//针对$ok被激活后的处理:
   
   $sql="update userinfo set username='".$_post[username]."',gender='".$_post[gender]."',age='".$_post[age]."',regdate='".$_post[regdate]."' where id='".$_post[id]."'";
   $result=mysql_query($sql);
   if($result)
    echo "记录已经成功修改<br><a href='modify.php'>继续修改记录</a>";
   else
    echo "记录修改失败<br><a href=modify.php?id=".$_post[id].">返回</a>";
  }
 }//else($id部分)
} // end if
?>

保存记录到数据库

 代码如下 复制代码

<?php
if($_post[ok])
{
 $link_id = mysql_connect("localhost","root","") or die("连接失败");
 if($link_id)
 {
  //选择数据库
  mysql_select_db("my_test");
  //插入数据sql语句
  $sql="insert into userinfo values('".$_post[id]."','".$_post[name]."','".$_post[gender]."','".$_post[age]."','".$_post[regdate]."')";
  //执行sql语句
  $result=mysql_query($sql);
  if($result)
  {
   echo "记录已经成功插入<br><a href='insert.php'>继续插入记录</a>";
  }
  else
   echo "执行插入sql语句失败";
  //关闭数据库
  mysql_close($link_id);
 }
}
else
{
 ?>
 <form method=post action=insert.php>
 编号<input type=text name="id"><br>
 姓名<input type=text name="name"><br>
 性别<input type=text name="gender" ><br>
 年龄<input type=text name="age"><br>
 注册时间<input type=text name="regdate"><br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <input type=submit name=ok value="提交">
 </form>
 <?
}//end if
?>

[!--infotagslink--]

相关文章

  • PHP session_start()很慢问题分析与解决办法

    本文章来给各位同学介绍一下关于PHP session_start()很慢问题分析与解决办法,希望碰到此问题的同学可进入参考。 最近在做东西的时候发现一个问题 有一个接口挂...2016-11-25
  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • HTTP 408错误是什么 HTTP 408错误解决方法

    相信很多站长都遇到过这样一个问题,访问页面时出现408错误,下面一聚教程网将为大家介绍408错误出现的原因以及408错误的解决办法。 HTTP 408错误出现原因: HTT...2017-01-22
  • Android子控件超出父控件的范围显示出来方法

    下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02
  • ps把文字背景变透明的操作方法

    ps软件是现在非常受大家喜欢的一款软件,有着非常不错的使用功能。这次文章就给大家介绍下ps把文字背景变透明的操作方法,喜欢的一起来看看。 1、使用Photoshop软件...2017-07-06
  • intellij idea快速查看当前类中的所有方法(推荐)

    这篇文章主要介绍了intellij idea快速查看当前类中的所有方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-09-02
  • Mysql select语句设置默认值的方法

    1.在没有设置默认值的情况下: 复制代码 代码如下:SELECT userinfo.id, user_name, role, adm_regionid, region_name , create_timeFROM userinfoLEFT JOIN region ON userinfo.adm_regionid = region.id 结果:...2014-05-31
  • js导出table数据到excel即导出为EXCEL文档的方法

    复制代码 代码如下: <!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 ht...2013-10-13
  • mysql 批量更新与批量更新多条记录的不同值实现方法

    批量更新mysql更新语句很简单,更新一条数据的某个字段,一般这样写:复制代码 代码如下:UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_value';如果更新同一字段为同一个值,mysql也很简单,修改下where即...2013-10-04
  • ps怎么制作倒影 ps设计倒影的方法

    ps软件是一款非常不错的图片处理软件,有着非常不错的使用效果。这次文章要给大家介绍的是ps怎么制作倒影,一起来看看设计倒影的方法。 用ps怎么做倒影最终效果&#819...2017-07-06
  • js基础知识(公有方法、私有方法、特权方法)

    本文涉及的主题虽然很基础,在许多人看来属于小伎俩,但在JavaScript基础知识中属于一个综合性的话题。这里会涉及到对象属性的封装、原型、构造函数、闭包以及立即执行表达式等知识。公有方法 公有方法就是能被外部访问...2015-11-08
  • PHP传值到不同页面的三种常见方式及php和html之间传值问题

    在项目开发中经常见到不同页面之间传值在web工作中,本篇文章给大家列出了三种常见的方式。接触PHP也有几个月了,本文总结一下这段日子中,在编程过程里常用的3种不同页面传值方法,希望可以给大家参考。有什么意见也希望大...2015-11-24
  • 安卓手机wifi打不开修复教程,安卓手机wifi打不开解决方法

    手机wifi打不开?让小编来告诉你如何解决。还不知道的朋友快来看看。 手机wifi是现在生活中最常用的手机功能,但是遇到手机wifi打不开的情况该怎么办呢?如果手机wifi...2016-12-21
  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • c#中分割字符串的几种方法

    单个字符分割 string s="abcdeabcdeabcde"; string[] sArray=s.Split('c'); foreach(string i in sArray) Console.WriteLine(i.ToString()); 输出下面的结果: ab de...2020-06-25
  • js控制页面控件隐藏显示的两种方法介绍

    javascript控制页面控件隐藏显示的两种方法,方法的不同之处在于控件隐藏后是否还在页面上占位 方法一: 复制代码 代码如下: document.all["panelsms"].style.visibility="hidden"; document.all["panelsms"].style.visi...2013-10-13
  • 连接MySql速度慢的解决方法(skip-name-resolve)

    最近在Linux服务器上安装MySql5后,本地使用客户端连MySql速度超慢,本地程序连接也超慢。 解决方法:在配置文件my.cnf的[mysqld]下加入skip-name-resolve。原因是默认安装的MySql开启了DNS的反向解析。如果禁用的话就不能...2015-10-21
  • jQuery实现切换页面过渡动画效果

    直接为大家介绍制作过程,希望大家可以喜欢。HTML结构该页面切换特效的HTML结构使用一个<main>元素来作为页面的包裹元素,div.cd-cover-layer用于制作页面切换时的遮罩层,div.cd-loading-bar是进行ajax加载时的loading进...2015-10-30
  • C#方法的总结详解

    本篇文章是对C#方法进行了详细的总结与介绍,需要的朋友参考下...2020-06-25