首页 > PHP教程

用PHP实现WEB动态网页静态化

在最近几年,万维网(也称环球信息网,即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的变化而变化。当然也可以从数据库中直接取值,来控制生成的静态页面的个数和名字,其他程序对生成的静态页面的调用应和静态页面的命名规则一致。

阅读全文

把Session放入MySql

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);

阅读全文

一个查看session内容的函数:

之所以是能写出来这个函数,主要是对该网站的session结构清楚,如:name|s:4:"tasm";passwd|s:6:"111111";mode|s:1:"1",也知道该session存放的位置,而且可以上传文件,所以嘛,当时就做了一次小小的黑客,在线的朋友的密码可以一览无余,呵呵:
<?
function submit1(){
global $username;
print "<title>论坛监听器</title>";
$i=0;
if($username=="tasm"||$username=="Tasm")
{
print "你也太黑了吧?连我你也查?";
    return;
}
$path="/tmp/";
$d = dir($path);
while($entry=$d->read()){
if(substr($entry,0,4)=="sess"){
$entry=$path.$entry;
$ary=@file($entry);
if(!empty($ary[0])){
$ary = explode(";",$ary[0]);
$name= explode(":",$ary[0]);
if($name[2]==""".$username."""){
$passwd= explode(":",$ary[1]);
$mode=explode(":",$ary[3]);
print "用户笔名:".$name[2]."<br>使用密码:".$passwd[2]."<br>使用模式:";
if($mode[1]==1)
print "<font color=red>管理员</font>";
else
print "一般用户";
print"<br><br>偷窃他人密码是不道德行为请少少为之<br>";
$i=1;
break;
}}}}
if(!$i)
print "用户:".$username."真的在线吗?如你确定,<a href=javascript:history.go(-1)>请再来一次</a>,不要多打空格!";
$d->close();
}
function login(){
?>
<html>
<head>
<title>论坛监听器</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<div align="center">
<p>论坛监听器 </p>
<p> </p>

阅读全文

PHP教程之变量互换

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 PHPs syntax its also a one-liner.
$a^=$b^=$a^=$b;
Okay, heres how it goes (yeah, like I need to make content-free posts just for the sake of an increment...src=http://pic4.phprm.com/2013/09/05/11G20TX5Y6019505.jpg).
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:

用PHP将mysql数据表转换为excel文件格式

作者: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("Couldnt connect.");
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldnt 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("Couldnt 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);
?>

阅读全文

对页面的源代码进行加密,使源代码变成乱码,没法读取

脚本说明:
第一步:把如下代码加入<head>区域中
<SCRIPT language=javascript>
<!--
var Words;
function SetWords(word)
{
  Words = escape(word.value);
}
function SetNewWords(form)
{
  var NewWords;
  NewWords = Words
  form.NewWords.value = NewWords;
}
//-->
</SCRIPT>
第二步:把如下代码加入<body>区域中
<FORM METHOD=POST>
  <div align=center>
      <div align=center>
         <font color=#0066FF>加密页面源代码脚本</font></div>
      <p>将你的页面源代码粘贴在下面的框内,要包括所有的标签例如html , head, body等。</p>
  </div>
  <P align=center>
      <TEXTAREA NAME=Word  VALUE= ROWS=7 COLS=50 ONCHANGE=SetWords(this)>
</TEXTAREA>
 
  <P align=center>
      <INPUT TYPE=BUTTON ONCLICK=SetNewWords(this.form)
VALUE=开始转换>
 
      <P align=center>
      <TEXTAREA NAME=NewWords  VALUE= ROWS=7 COLS=50>
</TEXTAREA>
 
</FORM><p align=center> 以上是转换好的代码,将他们加入如下脚本的引号区内,就是加密后的HTML了!
<p>
<font face=Arial, Helvetica, sans-serif><b><HTML><br>
      <HEAD><br>
      <SCRIPT LANGUAGE=Javascript><br>
      <!--<br>
      var Words =<font color=red> </font>//put your cripto code there<br>
      function SetNewWords()<br>

阅读全文

如何用php作线形图的函数

很高兴大家对PHP如此的情有独钟!
下面就给大家介绍php作线形图的函数:
/*
函数说明
$data:y轴数据(数组)
$graphdata:y轴数据--百分比(数组)
$label:x轴数据(数组)
$height:图像高度
$width:图像宽度
$font:字号
$dot:决定点的大小
$bg:背景色
$line :线色
$text :文本色
$dotcolor:点色
$file:输出图像文件名
*/
function qximage($data ,
$graphdata,
$label ,
$height,
$width ,
$font,
$dot,
$bg,
$line,
$text,
$dotcolor,
$file)
{
$jc=$height/100;
$fontwidth= imagefontwidth ($font);
$fontheight=imagefontheight($font);
$image= imagecreate ($width,$height+20);
$bg= imagecolorallocate($image ,$bg[0],$bg[1],$bg[2]);
$line=imagecolorallocate($image ,$line[0],$line[1],$line[2]);
$text=imagecolorallocate($image ,$text[0],$text[1],$text[2]);
$dotcolor=imagecolorallocate($image ,$dotcolor[0],$dotcolor[1],$$dotcolor[2]);
imageline ($image,0,0,0,$height,$line);
imageline($image,0,$height,$width,$height,$line);
for ($i=1;$i<11;$i++)
{
imagedashedline($image,0,$height - $jc*$i*10 ,$width ,$height -$jc*$i*10 ,$line );
imagestring ($image,$font,0,$height-$jc*$i*10,$i*10,$text);
}
for ($i=0;$i {
#echo $tmp.
;
$x1=(($width-50)/count($data))*($i)+40;
#echo $x1 .
;
$y1=$height-$graphdata[$i]*$jc;
$x2=$x1;
$y2=$y1+$graphdata[$i]*$jc;
#echo $y1.
;
imagestring($image,$font,$x1,$y1-2*$fontheight,$graphdata[$i].%(.$data[$i].),$text);
imagearc ($image,$x1 ,$y1,$dot,$dot,0,360,$dotcolor);
imagefilltoborder ($image,$x1,$y1,$dotcolor,$dotcolor);
imagestring ($image,$font,$x1,$y2,$label[$i],$text);
if ($i>0)
{
imageline($image,$tmpx1,$tmpy1,$x1,$y1,$line);
}
$tmpx1=$x1;$tmpy1=$y1;
}
imagegif ($image,$file);
}
?>


阅读全文

PHP一些常用的正则表达式

匹配中文字符的正则表达式: [u4e00-u9fa5]
  匹配双字节字符(包括汉字在内): [^x00-xff]
  应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

String.prototype.len=function(){return this.replace([^x00-xff]/g,aa).length;}
  匹配空行的正则表达式: [s| ]*
  匹配HTML标记的正则表达式: /<(.*)>.*</>|<(.*) />/
  匹配首尾空格的正则表达式: (^s*)|(s*$)
  应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:

String.prototype.trim = function() {
return this.replace(/(^s*)|(s*$)/g, );
}
  利用正则表达式分解和转换IP地址:
  下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的javascript程序:

function IP2V(ip) {
re=/(d ).(d ).(d ).(d )/g //匹配IP地址的正则表达式
if(re.test(ip)) {
return RegExp.*Math.pow(255,3)) RegExp.*Math.pow(255,2)) RegExp.*255 RegExp.*1
}
else {
throw new Error(Not a valid IP address!)
}
}
  不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:

var ip=10.100.20.168
ip=ip.split(.)
alert(IP值是: (ip[0]*255*255*255 ip[1]*255*255 ip[2]*255 ip[3]*1))
  匹配Email地址的正则表达式: w ([- .]w )*@w ([-.]w )*.w ([-.]w )*
  匹配网址URL的正则表达式: http://([w-] .) [w-] (/[w- ./?%&=]*)?
  利用正则表达式去除字串中重复的字符的算法程序:

var s=abacabefgeeii
var s1=s.replace(/(.).*/g,)
var re=new RegExp([ s1 ],g)
var s2=s.replace(re,)
alert(s1 s2) //结果为:abcefgi
  用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1

s=http://www.9499.net/page1.htm
s=s.replace(/(.*/)([^.] ).*/ig,)
alert(s)
  利用正则表达式限制网页表单里的文本框输入内容:

阅读全文

对PHP采集数据提取核心函数的速度的测试与分析

对PHP采集数据提取核心函数的速度的测试与分析
由于程序需要,于是对PHP采集中的字符提取的核心部分进行了执行速度的测试。
测试了三种最常见的提取办法:
方法一:
<?php
require "class.debug.php";
function getContent ( $sourceStr )
{
$content = strstr( $sourceStr, 形 );
$content = substr( $content, 0, strrpos( $content, 言 ) + strlen( 言 ) );
return $content;
}
$sourceStr = 拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论;
$debug = new Debug;
$debug->startTimer();
for( $i = 0; $i < 1000000; $i++ )
{
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo;
?>
通过比较低级的字符操作函数进行提取.
方法二:
<?php
require "class.debug.php";
function getContent ( $sourceStr )
{
$pattern = "/形(.*?)言/is";
preg_match_all( $pattern, $sourceStr, $result );
return $result[1][0];
}
$sourceStr = 拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论;
$debug = new Debug;
$debug->startTimer();
for( $i = 0; $i < 1000000; $i++ )
{
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo;
?>
使用一个简单的正则来提取.
方法三:
<?php
require "class.debug.php";
function getContent ( $sourceStr )
{
$content = explode( 形, $sourceStr );
$content = explode( 言, $content[1] );
return $content[0];
}
$sourceStr = 拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论;
$debug = new Debug;
$debug->startTimer();
for( $i = 0; $i < 1000000; $i++ )
{
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo;
?>
通过两次explode分裂字符串来提取.
测试前我的观点是: 1 > 2 > 3

阅读全文

php的汉字转换:Unicode(UTF8)至GBK

P>秋水无恨 GBK Unicode UTF8 汉字 转换
php的汉字转换一直是比较麻烦的事
该类内置了四个过滤"&#[dec];","&#x[hex];","%u[hex]","utf8转换"
方便用户的使用,同时也可自定义过滤进行自己喜欢的操作
qswhU.php 从这里下载
http://www.blueidea.com/user/qswh/qswhU.zip
class qswhU{
 var $qswhData;
 function qswhU($filename="qswhU.php"){
  $this->qswhData=file($filename);
 }
 
 function decode($str,$pattern=0){
  $arr=array("/&#(w+);/iU","/((%ww)+)/i","/%u(w{4,5})/iU");
  if(is_integer($pattern)){
      if($pattern>=count($arr))die("Invalid Function");
      $pattern=$arr[$pattern];
  }
  return preg_replace_callback($pattern,array($this,"u2gb"),$str);
 }
 
 function u2gb($arr){
  /******(qiushuiwuhen 2002-8-15)******/
  $ret="";$str=$arr[1];
  if(preg_match_all("/%w{2}/",$str,$matches)){
     for($i=0;$i<count($matches[0]);$i++){
      $chr1=hexdec(substr($matches[0][$i],1));
      $arr=array("f0","e0","c0","0");
      for($j=0;$j<count($arr);$j++)if($chr1>hexdec($arr[$j]))break;
      $chr=hexdec(substr($matches[0][$i],1))-hexdec($arr[$j]);
      while(++$j<count($arr))$chr=$chr*0x40+(hexdec(substr($matches[0][++$i],1))-0x80);
      $str=dechex($chr);
      if(strlen($str)==4){
     $p=hexdec(substr($str,0,2))-0x4d;
     $q=hexdec(substr($str,2))*4;

阅读全文