当前位置: 编程技术>php
本页文章导读:
▪php文章管理模块实例代码 代码如下,实现了文章管理的基本功能,大家注意看注释,知识点还是不少的。
<?php
/**
*文章管理
* www.
* */
include "isLogin.php";
include "../conn/config.inc.php";
.........
▪php判断是否手机访问的代码 代码如下:
<?php
//php判断客户端是否为手机
$agent = $_SERVER['HTTP_USER_AGENT'];
if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($age.........
▪php 图片处理类(简单易用) 代码如下:
<?php
class Image {
private $path;
//构造方法用来对图片所在位置进行初使化
function __construct($path="./"){
$this->path=rtrim($path, "/")."/";
}
/* 对图片.........
[1]php文章管理模块实例代码
来源: 互联网 发布时间: 2013-12-24
代码如下,实现了文章管理的基本功能,大家注意看注释,知识点还是不少的。
<?php /** *文章管理 * www. * */ include "isLogin.php"; include "../conn/config.inc.php"; include_once("../FCKeditor/fckeditor.php"); $oFCKeditor = new FCKeditor("content") ; // 创建FCKeditor实例,可创建多个实例 $oFCKeditor->BasePath = '../FCKeditor/'; // 设置FCKeditor目录地址 /*action操作初始化*/ if(emptyempty($_REQUEST['action'])){ $_REQUEST['action']='list'; }else { $_REQUEST['action']=trim($_REQUEST['action']); } if($_REQUEST['action']=='add'){ $oFCKeditor = new FCKeditor("content") ; // 创建FCKeditor实例,可创建多个实例 $oFCKeditor->BasePath = '../FCKeditor/'; // 设置FCKeditor目录地址 $tpl->assign('fck',$oFCKeditor->Create()); $tpl->assign('tmess','添加文章'); $tpl->assign('act','insert'); $tpl->assign('submitButton','添加'); $tpl->display('admin/addArticle.tpl'); } /*添加文章*/ elseif ($_REQUEST['action']=='insert'){ $title=$_POST['title']; $zhaiyao=$_POST['zhaiyao']; $author=$_POST['author']; $laiyuan=$_POST['laiyuan']; $keyword=$_POST['keyword']; $is_tuijian=$_POST['is_tuijian']; $content=$_POST['content']; $sql="insert into article (title,zhaiyao,author,laiyuan,keyword,is_tuijian,content,addtime) values ('$title','$zhaiyao','$author','$laiyuan','$keyword','$is_tuijian','$content',now())"; $rs=$db->query($sql); if($rs){ echo header("location:/admin/article.php?action=list"); }else { echo "失败!"; } } /*文章列表*/ elseif ($_REQUEST['action']=='list'){ $total=getAllArticle(); $curpage=isset()($_GET['page'])?$_GET['page']:1; $pageObj=new Page($total,$curpage); $pageInfo=$pageObj->getPageInfo(); $arts=getArticlePage($pageInfo['row_offset'],$pageInfo['row_num']); $tpl->assign("tmess","文章列表"); $tpl->assign("arts",$arts); $tpl->assign("pageInfo",$pageInfo); $tpl->display("admin/editArticle.tpl"); } /*修改文章*/ elseif ($_REQUEST['action']=='edit'){ $id=$_GET['id']; $post=editArticle($id); $tpl->assign("tmess","修改文章"); $tpl->assign('post',$post); $oFCKeditor->Value=$post["content"]; //编辑时显示文章内容 if($post['is_tuijian']==1||$_POST["recommend"]==1){ //判断是否推荐 $tpl->assign('recommend','checked'); }else { $tpl->assign('no_recommend','checked'); } $tpl->assign('fck',$oFCKeditor->Create()); $tpl->assign("act","update"); $tpl->assign('submitButton','修改'); $tpl->display('admin/addArticle.tpl'); }elseif ($_REQUEST['action']=='update'){ $id=$_POST['id']; $title=$_POST['title']; $zhaiyao=$_POST['zhaiyao']; $author=$_POST['author']; $laiyuan=$_POST['laiyuan']; $keyword=$_POST['keyword']; $is_tuijian=$_POST['is_tuijian']; $content=$_POST['content']; $sql="update article set title='$title',zhaiyao='$zhaiyao',author='$author',laiyuan='$laiyuan',keyword='$keyword',is_tuijian='$is_tuijian', content='$content' where id ='$id'"; $rs=$db->query($sql); if($rs){ echo header("location:/admin/article.php?action=list"); }else { echo "修改失败!"; } } /*删除*/ elseif ($_REQUEST['action']=='del'){ $id=$_GET['id']; del($id); } /*审核文章*/ elseif ($_REQUEST['action']=='shenhe'){ $id=$_GET['id']; shenhe($id); } /*锁定文章*/ elseif ($_REQUEST['action']=='lock'){ $id=$_GET['id']; lock($id); } /*批量删除*/ /*批量审核*/ elseif (isset($_REQUEST['shenhe_x'])||isset($_REQUEST['shenhe_y'])){ $del=$_POST['del']; shenhe($del); } /*批量锁定*/ elseif (isset($_REQUEST['lock_x'])||isset($_REQUEST['lock_y'])){ $del=$_POST['del']; lock($del); } /*==--文章模块相关函数===*/ /** * 获取所有的文章进行分页用 * */ function getAllArticle(){ global $db; $sql="select * from article"; $rs=$db->query($sql); $total=$db->num_rows($rs); return $total; } /*文章分页相关函数*/ function getArticlePage($offset,$num){ global $db; $sql="select*from article order by id desc limit $offset,$num"; $rs=$db->query($sql); while ($row=$db->fetch_array($rs)){ $data[]=$row; } return $data; } /*文章修改*/ function editArticle($id){ global $db; $sql="select * from article where id = '$id'"; $rs=$db->query($sql); return $row=$db->fetch_array($rs,MYSQL_ASSOC); } /*删除文章*/ function del($id){ global $db; $sql="delete from article where id"; if(is_array($id)){ $sql.="IN (".implode(",","$id").")"; }else { $sql.="=$id"; } $rs=$db->query($sql); if($rs){ echo header("location:/admin/article.php?action=list"); }else { echo "删除失败!"; } } function shenhe($id){ global $db; $sql="update article set status='1' where id"; if(is_array($id)){ $sql.="IN (".join(",",$id).")"; //如果$id是一个数组,用join函数把$id分割成用,连接的字符串 join是implode的别名 }else { $sql.="=$id"; } $rs=$db->query($sql); if($rs){ echo header("location:/admin/article.php?action=list"); }else { echo "审核失败!"; } } function lock($id){ global $db; $sql="update article set status='0' where id "; if(is_array($id)){ $sql.="IN (".implode(",","$id").")"; }else { $sql.="=$id"; } $rs=$db->query($sql); if($rs){ echo header("location:/admin/article.php?action=list"); }else { echo "锁定失败!"; } } ?>
[2]php判断是否手机访问的代码
来源: 互联网 发布时间: 2013-12-24
代码如下:
<?php //php判断客户端是否为手机 $agent = $_SERVER['HTTP_USER_AGENT']; if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) header("Location:xxxxxxxx"); ?>
您还可以参考:判断是否手机访问的php js代码
[3]php 图片处理类(简单易用)
来源: 互联网 发布时间: 2013-12-24
代码如下:
<?php class Image { private $path; //构造方法用来对图片所在位置进行初使化 function __construct($path="./"){ $this->path=rtrim($path, "/")."/"; } /* 对图片进行缩放 * * 参数$name: 是需要处理的图片名称 * 参数$width:是缩放后的宽度 * 参数$height:是缩放后的高度 * 参数$qz: 是新图片的名称前缀 * 返回值:就是缩放后的图片名称,失败则返回false * */ function thumb($name, $width, $height, $qz="th_"){ //获取图片信息 $imgInfo=$this->getInfo($name); //图片的宽度,高度,类型 //获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png $srcImg=$this->getImg($name, $imgInfo); //获取计算图片等比例之后的大小, $size["width"], $size["height"] $size=$this->getNewSize($name, $width, $height, $imgInfo); //获取新的图片资源, 处理一下gif透明背景 $newImg=$this->kidOfImage($srcImg, $size, $imgInfo); //另存为一个新的图片,返回新的缩放后的图片名称 return $this->createNewImage($newImg, $qz.$name, $imgInfo); } private function createNewImage($newImg, $newName, $imgInfo){ switch($imgInfo["type"]){ case 1://gif $result=imageGif($newImg, $this->path.$newName); break; case 2://jpg $result=imageJPEG($newImg, $this->path.$newName); break; case 3://png $return=imagepng($newImg, $this->path.$newName); break; } imagedestroy($newImg); return $newName; } private function kidOfImage($srcImg, $size, $imgInfo){ $newImg=imagecreatetruecolor($size["width"], $size["height"]); $otsc=imagecolortransparent($srcImg); if($otsc >=0 && $otsc <= imagecolorstotal($srcImg)){ $tran=imagecolorsforindex($srcImg, $otsc); $newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]); imagefill($newImg, 0, 0, $newt); imagecolortransparent($newImg, $newt); } imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]); imagedestroy($srcImg); return $newImg; } private function getNewSize($name, $width, $height, $imgInfo){ $size["width"]=$imgInfo["width"]; $size["height"]=$imgInfo["height"]; //缩放的宽度如果比原图小才重新设置宽度 if($width < $imgInfo["width"]){ $size["width"]=$width; } //缩放的高度如果比原图小才重新设置高度 if($height < $imgInfo["height"]){ $size["height"]=$height; } //图片等比例缩放的算法 if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){ $size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]); }else{ $size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]); } return $size; } private function getInfo($name){ $data=getImageSize($this->path.$name); $imageInfo["width"]=$data[0]; $imageInfo["height"]=$data[1]; $imageInfo["type"]=$data[2]; return $imageInfo; } private function getImg($name, $imgInfo){ $srcPic=$this->path.$name; switch($imgInfo["type"]){ case 1: //gif $img=imagecreatefromgif($srcPic); break; case 2: //jpg $img=imageCreatefromjpeg($srcPic); break; case 3: //png $img=imageCreatefrompng($srcPic); break; default: return false; } return $img; } /* 功能:为图片加水印图片 * 参数$groundName: 背景图片,即需要加水印的图片 * 参数$waterName: 水钱图片 * 参数#aterPost:水印位置, 10种状态, * 0为随机位置 * * 1. 为顶端居左 2. 为顶端居中 3 为顶端居右 * 4 为中部居左 5. 为中部居中 6 为中部居右 * 7 . 为底端居左 8. 为底端居中, 9. 为底端居右 * * 参数$qz : 是加水印后的图片名称前缀 * 返回值:就是处理后图片的名称 * */ function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){ if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){ $groundInfo=$this->getInfo($groundName); $waterInfo=$this->getInfo($waterName); //水印的位置 if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){ echo "水印不应该比背景图片小!"; return; } $groundImg=$this->getImg($groundName, $groundInfo); $waterImg=$this->getImg($waterName, $waterInfo); $groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo); return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo); }else{ echo "图片或水印图片不存在"; return false; } } private function copyImage($groundImg, $waterImg, $pos, $waterInfo){ imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]); imagedestroy($waterImg); return $groundImg; } private function position($groundInfo, $waterInfo, $waterPos){ //需要背景比水印图片大 if(($groundInfo["width"]< $waterInfo["width"]) ||($groundInfo["height"] < $waterInfo["height"])){ return false; } switch($waterPos){ case 1: $posX=0; $posY=0; break; case 2: $posX=($groundInfo["width"]-$waterInfo["width"])/2; $posY=0; break; case 3: $posX=$groundInfo["width"]-$waterInfo["width"]; $posY=0; break; case 4: $posX=0; $posY=($groundInfo["height"]-$waterInfo["height"]) /2; break; case 5: $posX=($groundInfo["width"]-$waterInfo["width"])/2; $posY=($groundInfo["height"]-$waterInfo["height"]) /2; break; case 6: $posX=$groundInfo["width"]-$waterInfo["width"]; $posY=($groundInfo["height"]-$waterInfo["height"]) /2; break; case 7: $posX=0; $posY=$groundInfo["height"]-$waterInfo["height"]; break; case 8: $posX=($groundInfo["width"]-$waterInfo["width"])/2; $posY=$groundInfo["height"]-$waterInfo["height"]; break; case 9: $posX=$groundInfo["width"]-$waterInfo["width"]; $posY=$groundInfo["height"]-$waterInfo["height"]; break; case 0: default: $posX=rand(0, ($groundInfo["width"]-$waterInfo["width"])); $posY=rand(0, ($groundInfo["height"]-$waterInfo["height"])); break; } return array("posX"=>$posX, "posY"=>$posY); } }
最新技术文章: