把Session放入MySql

 更新时间:2016年11月25日 16:32  点击:1685
session通常放在/tmp目录下,而该文件夹的权限是everbody可读,这个就非常可怕了!学校的论坛曾经就有人通过session来盗取帐号!所以后来就尝试把session放入数据库,表的结构和过程如下:
//创建表
//create sesslib.sql
CREATE TABLE sesslib (
    data text,
    time datetime,
    id int(11) DEFAULT '0' NOT NULL auto_increment,
    sid varchar(32) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE sid (sid)
);
//End
//XX.php自定义了session的数据库路径,当某个页面需要使用//session时,可以include这个部分,使用方法为:
<?
include "XX.php";//XX.php
session_start();
//以下就可以正常使用session了
?>
/******************************************************/
    XX.php 内容:
/*****************************************************/
<?
$sess_dbh="";
$sess_maxlifetime=get_cfg_var("session.gc_maxlifetime");
function sess_open($save_path, $session_name) {
    global $hostname, $dbusername, $dbpassword, $dbname, $sess_dbh;
    
    //$sess_dbh=mysql_pconnect($hostname,$dbusername,$dbpassword) or die("不能连接数据库!");
    $sess_dbh=mysql_pconnect('localhost','test','test') or die("不能连接数据库!");
    // mysql_select_db("$dbname") or die("不能选择数据库!");
mysql_select_db('test') or die("不能选择数据库!");
    return(true);
}
function sess_close() {
    //mysql_close();
    return(true);
}
function sess_read($sid) {
    global $sess_dbh;
    $result = mysql_query("select data from sesslib where sid='$sid'", $sess_dbh);
Attributed to Solomon W. Golomb; a method for swapping the values of two integer variables without using an intermediate variable (you can tell this dates from the Elder Days, when variables were expensive!). Thanks to PHP's syntax it's also a one-liner.
$a^=$b^=$a^=$b;
Okay, here's how it goes (yeah, like I need to make content-free posts just for the sake of an increment...).
First, simplify the line; noting that ^= is right-associative, which means that in that line the rightmost operator is evaluated first, that the assignment operators also return the value that they assign to their lvalue, and that foo^=bar is shorthand for foo=foo^bar:

Code:

$a^=$b^=$a^=$b;
$a^=($b^=($a^=$b));
$a=$a^b;
$a^=($b^=$a);
$a=$a^$b;
$b=$b^$a;
$a=$a^$b;

Recall what ^ does. Takes each pair of corresponding bits from its arguments (the internal binary representation of its arguments, that is), and xors them together to produce the corresponding bit of the result ("corresponding" means that the first bits of both arguments produce the first bit of the result, the second bits of both arguments produce the second bit of the result, and so on. This is why, as BuzzLY noted, it's important that both variables are the same size - demons probably start flying out of your nose if one of them runs out of bits to xor before the other. So to figure out what ^ does to a pair of variables, we only need to recap what it does to single bits


在最近几年,万维网(也称环球信息网,即WWW)不断改变信息处理技术的面貌。WEB已经快速地成为一种有效的媒介,并适合人们和商业沟通和协作。几乎所有的信息技术领域都普遍受到WEB的影响。Web访问带来更多用户和更多数据,这意味着给服务器和数据库更多压力和最终用户得到越来越慢的响应速度。与不断靠增加CPU,磁盘驱动器及内存来跟上这种增长的需求相比, WEB动态网页面静态化应该是一个更实用,更经济的选择。
用PHP实现WEB动态网页静态化的具体实现函数如function gen_static_file()所示
function gen_static_file($program, $filename)
{
$program 1= "/usr/local/apache/htdocs/php/" . $program;
$filename1 = "/usr/local/apache/htdocs/ static_html/" . $filename;
$cmd_str = "/usr/local/php4/bin/php " . $program1 . " } " . $filename1 . " ";
system($cmd_str);
echo $filename . " generated.〈br〉";
}
这个函数是实现静态化的关键,即PHP动态页面程序不是被送到浏览器中,而是输入到名为$filename的文件中去(如图2)。两个参数中$program是PHP动态页面程序,$filename是生成的静态页面的名字(可根据需要自己制定命名规则,这一点很重要,见下文),/usr/local/php4/bin/php是PHP中具有把程序输入文件功能的部分,System是PHP中执行外部命令的函数。我们还可以看出所有生成动态页面的php程序需放在/php/目录下,所有新产生的静态页面则会出现在/static_html/目录下(这些路径可以根据具体需要设置)。
下面让我们举个具体例子,看一下college_static.php的静态页面是怎样生成的。
function gen_college_static ()
{
for ($i = 0; $i 〈= 32; $i++〉
{
putenv("province_id=" . $i); //*.php文件从数据库取数据时要用到。
$filename = " college_static". $i . ".html";
gen_static_file("college_static.php", $filename);
}
从这个函数我们可以看到通过调用函数gen_static_file(), college_static.php经过静态化,变成了33个静态页面college.static0.html~college.static33.html,其中$filename会随着$I的变化而变化。当然也可以从数据库中直接取值,来控制生成的静态页面的个数和名字,其他程序对生成的静态页面的调用应和静态页面的命名规则一致。
$message= <<<EOD
......成堆的字符串 如批量HTML代码 可以念
EOD;
装载到  $message 中。
$to="digico@123.com";
$mail ="hts@123.com";
$zhuti ="我爱你";     // 信件主题
// $mailheaders .="Content-Type=text/html; charset=gb2312 ";
// $mailheaders .="Content-disposition: inline ";
// $mailheaders .="Reply-To: ";
// $mailheaders .="Content-Transfer-Encoding: 7bit ";
$mailheaders = "From: $mail ";//发信人的地址
// $mailheaders .= "To: $to ";
 $mailheaders .= "MIME-Version: 1.0 ";
 $mailheaders .= "Content-type: text/html; charset=gb2312";
mail($to,$zhuti,$message,$mailheaders);
OK !


作者:mydowns 出处:把握时间网站:http://www.85time.com, http://www.mydowns.com     
原贴地址如下:     
http://www.mydowns.com/article_show.php?id=32     
详细内容如下:     
<?php
$DB_Server = "localhost";
$DB_Username = "mydowns";
$DB_Password = "";
$DB_DBName = "mydowns";
$DB_TBLName = "user";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect.");
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=mydowns.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
$now_date = date('Y-m-d H:i');
$title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date";
$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database");
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
echo("$title ");
$sep = " ";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . " ";
}
print(" ");
$i = 0;
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= " ";
print(trim($schema_insert));
print " ";
$i++;
}
return (true);
?>
[!--infotagslink--]

相关文章

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

    本文章来给各位同学介绍一下关于PHP session_start()很慢问题分析与解决办法,希望碰到此问题的同学可进入参考。 最近在做东西的时候发现一个问题 有一个接口挂...2016-11-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • PostgreSQL判断字符串是否包含目标字符串的多种方法

    这篇文章主要介绍了PostgreSQL判断字符串是否包含目标字符串的多种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-23
  • PostgreSQL TIMESTAMP类型 时间戳操作

    这篇文章主要介绍了PostgreSQL TIMESTAMP类型 时间戳操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-26
  • 详解Mysql中的JSON系列操作函数

    新版 Mysql 中加入了对 JSON Document 的支持,可以创建 JSON 类型的字段,并有一套函数支持对JSON的查询、修改等操作,下面就实际体验一下...2016-08-23
  • postgresql 实现多表关联删除

    这篇文章主要介绍了postgresql 实现多表关联删除操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-02
  • Postgresql 如何选择正确的关闭模式

    这篇文章主要介绍了Postgresl 如何选择正确的关闭模式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-18
  • 深入研究mysql中的varchar和limit(容易被忽略的知识)

    为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类型的问题。 ...2015-03-15
  • postgresql数据添加两个字段联合唯一的操作

    这篇文章主要介绍了postgresql数据添加两个字段联合唯一的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04
  • MySQL 字符串拆分操作(含分隔符的字符串截取)

    这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • mysql的3种分表方案

    一、先说一下为什么要分表:当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,有可能会死在那儿了。分表的目的就在于此,减小数据库的负担,缩短查询时间。根据个人经验,mysql执行一个sql的过程如下:1...2014-05-31
  • Vscode上使用SQL的方法

    这篇文章主要介绍了Vscode上使用SQL的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-26
  • PostgreSQL 字符串处理与日期处理操作

    这篇文章主要介绍了PostgreSQL 字符串处理与日期处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-01
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • PHP分布式框架如何使用Memcache同步SESSION教程

    本教程主要讲解PHP项目如何用实现memcache分布式,配置使用memcache存储session数据,以及memcache的SESSION数据如何同步。 至于Memcache的安装配置,我们就不讲了,以前...2016-11-25
  • 用VirtualBox构建MySQL测试环境

    宿主机使用网线的时候,客户机在Bridged Adapter模式下,使用Atheros AR8131 PCI-E Gigabit Ethernet Controller上网没问题。 宿主机使用无线的时候,客户机在Bridged Adapter模式下,使用可选项里唯一一个WIFI选项,Microsoft Virtual Wifi Miniport Adapter也无法上网,故弃之。...2013-09-19
  • Centos5.5中安装Mysql5.5过程分享

    这几天在centos下装mysql,这里记录一下安装的过程,方便以后查阅Mysql5.5.37安装需要cmake,5.6版本开始都需要cmake来编译,5.5以后的版本应该也要装这个。安装cmake复制代码 代码如下: [root@local ~]# wget http://www.cm...2015-03-15
  • postgresql重置序列起始值的操作

    这篇文章主要介绍了postgresql重置序列起始值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-04
  • SQL Server中执行动态SQL

    本文详细讲解了SQLServer中执行动态SQL的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2022-05-19