一个PHP5不错的实例
CODE: [Copy to clipboard] --------------------------------------------------------------------------------
<? /* 发送一个头部,以便让浏览器知道该文件所包含的内容类型*/ header("Content-type: image/jpg"); /* 建立保存新图像高度和宽度的变量*/ $newWidth = 35; $newHeight = 35; /* 建立给定高度和宽度的新的空白图像*/ $newImg = ImageCreate($newWidth,$newHeight); /* 从原来较大的图像中得到数据*/ $origImg = ImageCreateFromJPEG("test.jpg"); /*拷贝调整大小后的图像,使用ImageSX()、ImageSY()得到原来的图像在X、Y方面上的大小 */ ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg)); /*创建希望得到的图像,释放内存 */ $red=ImageColorAllocate($im,255,0,0); ImageString($newImg,5,10,10," HI !HELLO!",$red); ImageJPEG($newImg); ImageDestroy($newImg); ?>
error_reporting(E_ERROR | E_WARNING | E_PARSE); header("Content-Language: charset=zh-cn"); header("Content-type: text/html; charset=GB2312");
//+----------------------------------------------------------------------------- //定义常量 define( "SITE_NAME", "NetX商务在线5.0---PHP5开发测试版!"); define( "CLASS_PATH" , "classes/"); define( "SESSION_PATH" , "session/");
//+------------------------------------------------------------------------------ //打开缓冲区 ob_start();
//+------------------------------------------------------------------------------ //设置session保存目录 session_save_path(SESSION_PATH); if(function_exists(session_cache_limiter)) { session_cache_limiter("nocache"); } session_start();
//+----------------------------------------------------------------------------- //定义自动加载类
function __autoload($className){ require_once(CLASS_PATH.$className.".php"); }
//+----------------------------------------------------------------------------- //定义一个接口 interface index {
public function application(); public function module_var(); public function parse_incoming(); public function safeChars($safestr);
}
//定义一个类 class MainClass implements index{ //+---------------------------------------------------------------------------------- //属性 public $site_name; public $netx; public $temp; public $mod = ""; public $module_class; public $subtemplate; public $NetXHeader; public $module; public $NetXFooter; public $input;
//+--------------------------------------------------------------------------------------- //以下是定义类中的方法 //+---------------------------------------------------------------------------------------
//+---------------------------------------------------------------------------- //构造函数 function __construct(){
//+---------------------------------------------------------------------------- //加载MYSQL驱动! $this->netx = new db_driver;
//+---------------------------------------------------------------------------- //加载模板驱动! $this->temp = new Template("header"); $this->NetXHeader = new getHeader($this->temp,$this->netx);
$this->temp = new Template("footer"); $this->NetXFooter = new getFooter($this->temp);
//+------------------------------------------------------------------------------- //解析输入 $this->input = $this->parse_incoming();
//+------------------------------------------------------------------------------- //模块调用,自动加载$module_var类! $module_var = $this->module_var(); $this->module = new $module_var($this->netx,$this->input,$module_var); } //End Function
//+---------------------------------------------------------------------------- //析构函数 function __destruct(){ $this->netx->db_close(); } //End Function
function __call($function_name, $args){ $this->halt("使用".get_class()."类未定义的".$function_name."()方法!"); exit; }
function __set($property_name, $value){ $this->halt("使用".get_class()."类未定义的属性".$property_name); exit; } //+--------------------------------------------------------------------------- //入口函数 public function application(){
$this->NetXHeader->application(); $this->module->application(); $this->NetXFooter ->application(); } //End Function
//+--------------------------------------------------------------------------- //设置模板变量! public function module_var(){
@$this->mod = isset($_GET["mod"]) ? $_GET["mod"] : $_POST["mod"]; if(isset($this->mod)){ if (!file_exists(CLASS_PATH.$this->mod.".php")){ $this->mod = "main"; } return $this->mod; }else{ $this->mod = "main"; return $this->mod; } } //End Function
//+-------------------------------------------------------------------------------------- //输入解析函数 public function parse_incoming(){ //+-------------------------------------------------------------------------------- //接收GET变量! $return = array(); if(is_array($_GET)){ foreach($_GET as $k=>$v){ if(is_array( $_GET[$k])){ foreach($_GET[$k] as $k2=>$v2){ $return[$k][$k2] = $this->safeChars($v2); } }else{ $return[$k] = $this->safeChars($v); } } }
//+----------------------------------------------------------------------------- //用POST变量覆写GET变量 if(is_array($_POST)){ foreach($_POST as $k=>$v){ if(is_array($_POST[$k])){ foreach($_POST[$k] as $k2 => $v2){ $return[$k][$k2] = $this->safeChars($v2); } }else{ $return[$k] = $this->safeChars($v); } } } return $return; }
//+------------------------------------------------------------------------------------ //数据安全性检查 public function safeChars($safestr){
$safestr = trim($safestr); $safestr = str_replace("*", "*", $safestr); $safestr = str_replace("|", "|", $safestr); $safestr = str_replace("&", "&", $safestr); // 正常显示符号 & $safestr = str_replace(">", ">", $safestr); $safestr = str_replace("<", "<", $safestr); $safestr = str_replace(""", """, $safestr); $safestr = str_replace(""", """, $safestr); $safestr = str_replace(" ", "<br />", $safestr); // 兼容 XHTML,改用 <br /> $safestr = str_replace(" ", "",$safestr); // 建议保留 $safestr = str_replace(" ","",$safestr);
return $safestr; } //End Function
//+------------------------------------------------------------------------------------ //发送警告信息 function halt($msg){ echo "<script language="javascript">"; echo "alert("$msg");"; echo "window.close();"; echo "</script>"; } //End Function } //End Class
//+----------------------------------------------------------------------------- //创建一个对象,并执行该对象的动作 $app = new MainClass; $app->application(); //NetX商务在线程序入口 //+----------------------------------------------------------------------------
//+-------------------------------------------------------------------------------- ?>
计算两个日期相差几天 <?php $date1 = "2004-7-1"; $date2 = "2005-8-9"; $d = (maketime($date2) - maketime($date1)) / (3600*24); echo "相差 $d 天";
function maketime($date) { list($year,$month,$day) = explode("-",$date); return mktime(0,0,0,$month,$day,$year); } ?>
|