php数据库备份程序

 更新时间:2016年11月25日 16:05  点击:1424

function backup($bakfile = null, $tables = array())
 {
  if (empty($bakfile)) {
   $bakfile = $this->dbname . date("Ymdhis") . '.sql';
  } elseif (is_dir($bakfile)) {
   if (preg_match('//$/', $bakfile)) {
    $bakfile = $bakfile . $this->dbname . date("Ymdhis") . '.sql';
   } else {
    $bakfile = $bakfile . '/' . $this->dbname . date("Ymdhis") . '.sql';
   }
  }
  if (!$tables) {
   $this->query("SHOW TABLES");
   while ($row = mysql_fetch_row($this->queryID)) {
    $tables[] = $row[0];
   }
  } else {
   foreach ($tables as $k => $v) {
    $tables[$k] = $this->tablePrefix . $v;
   }
  }
 
  if ($fp = fopen($bakfile, 'wb')) {
   if ($this->dbcharset) {
    fwrite($fp, "SET NAMES " . $this->dbcharset . ";nn");
   }
   foreach ($tables as $table) {
    $this->dumpTable($table, $fp);
    fwrite($fp, "n");
   }//foreach
   fclose($fp);
   return true;
  } else {
   return false;
  }//if
 }
 
 //私有方法 导出表格
 function dumpTable($fullTableName, $fp)
 {
  //备份表结构 
  fwrite($fp, "-- n-- {$fullTableName}n-- n");
  $row = $this->findBySql("SHOW CREATE TABLE `{$fullTableName}`");
  fwrite($fp, $row['Create Table'] . ";nn" );
  //备份表库数据
  $this->query("SELECT * FROM `{$fullTableName}`");
  while ($row = mysql_fetch_assoc($this->queryID)) {
   foreach ($row as $k=>$v) {
    $row[$k] = "'" . $this->qstr($v) . "'" ;
   }
   $sql = "INSERT INTO `$fullTableName` VALUES (" . join(",", $row) . ");n";
   fwrite($fp, $sql);
  }
  mysql_free_result($this->queryID);
  fwrite($fp, "n");
 }
 
 //恢复数据库文件
 function restore($bakfile)
 {
  if ($fp = fopen($bakfile, 'r')) {
   $sql = '';
   while (!feof($fp)) {
    $line = fgets($fp);
    if (strpos($line,'--')!==0)
    {
     $sql .= $line;
     //pp($sql);
    }
    if (preg_match('/;s*$/', $sql)) {
     $this->query($sql);
     $sql = '';
    }
   }
   fclose($fp);
   return true;
  } else {
   return false;
  }
 }
<?php session_start(); ?>
<?php
{
$authcode = new AuthCode();
if ($authcode->GetUriFileName() == "authcode.php")
{
  $authcode->OutputImg();
}
}
function ChkAuthcode($Authcode)
{
if ($_SESSION['AuthCode'] == $Authcode)
{
  $rtn = true;
}
else
{
  $rtn = false;
}
$_SESSION['AuthCode'] = rand(0, 999999);
return $rtn;
}
class AuthCode
{
/* Public Variables */
/* Private Variables */
var $image;
#
var $sBgcolor;
var $nWidth;
var $nHeight;
var $nLen;
var $bNoise;
var $nNoise;
var $bBorder;
var $aFontlist;
/* Constractor */
function AuthCode()
{
  $this->sBgcolor = "#FFCC00";
  $this->nWidth = 70;
  $this->nHeight = 25;
  $this->nLeftMargin = 5;
  $this->nRightMargin = 5;
  $this->nTopMargin = 3;
  $this->nBottomMargin = 2;
  $this->nLen = 4;
  $this->bNoise = true;
  $this->nNoisePoint = 50;
  $this->nNoiseLine = 5;
  $this->bBorder = true;
 
  $this->aFontlist[0] = "./fonts/arial.ttf";
  $this->aFontlist[1] = "./fonts/comic.ttf";
  $this->aFontlist[2] = "./fonts/raavi.ttf";
  $this->aFontlist[3] = "./fonts/verdanai.ttf";
  $this->aFontlist[4] = "./fonts/tahoma.ttf";
  $this->aFontlist[5] = "./fonts/shruti.ttf";
  $this->aFontlist[6] = "./fonts/BKANT.TTF";
  $this->aFontlist[7] = "./fonts/comicbd.ttf";
  $this->aFontlist[8] = "./fonts/courbi.ttf";
  $this->aFontlist[9] = "./fonts/times.ttf";
}

function OutputImg()
{
  $this->image = "";
  $this->image = imagecreate($this->nWidth, $this->nHeight);
  $back = $this->getcolor($this->sBgcolor);
  imagefilledrectangle($this->image, 0, 0, $this->nWidth, $this->nHeight, $back);
  $size = ($this->nWidth - $this->nLeftMargin - $this->nRightMargin)/$this->nLen;
  if($size>($this->nHeight - $this->nTopMargin - $this->nBottomMargin))
   $size=$this->nHeight - $this->nTopMargin - $this->nBottomMargin;
 
  $left = ($this->nWidth-$this->nLen*($size+$size/10))/2 + $this->nLeftMargin;
  $code = "";
  for ($i=0; $i<$this->nLen; $i++)
  {
   $randtext = rand(0, 9);
   $code .= $randtext;
   $textColor = imagecolorallocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
   $font = $this->aFontlist[rand(0,9)];//rand(1,4).".ttf";
   $randsize = rand($size-$size/10, $size+$size/10);
   $location = $left+($i*$size+$size/10);
   imagettftext($this->image, $randsize, rand(-18,18), $location, rand($size, $size+$size/5) + $this->nTopMargin, $textColor, $font, $randtext);
  }
  if($this->bNoise == true) $this->setnoise();
  $_SESSION['AuthCode'] = $code;
  $bordercolor = $this->getcolor("      ");
  if($border==true) imagerectangle($this->image, 0, 0, $this->nWidth-1, $this->nHeight-1, $bordercolor);
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // date in the past
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Cache-Control: private");
  header("Pragma: no-cache"); // HTTP/1.0
  header("Content-type: image/png");
  imagepng($this->image);
  imagedestroy($this->image);
 
  return $sAuthcode;
}
function ChkAuthcode($Authcode)
{
  if ($this->GetAuthcode() == $Authcode)
  {
   $rtn = true;
  }
  else
  {
   $rtn = false;
  }
  $_SESSION['AuthCode'] = rand(0, 999999);
  return $rtn;
}
function GetAuthcode()
{
  $x_AuthCode = $_SESSION['AuthCode'];
  $_SESSION['AuthCode'] = rand(0, 999999);
  return $x_AuthCode;
}
 
/* Private Functions */
function GetUriFileName()
{
  return substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], '/') + 1 , strlen($_SERVER['SCRIPT_NAME']) - strrpos($_SERVER['SCRIPT_NAME'], '/'));
}
function setnoise()
{
  for ($i=0; $i<$this->nNoiseLine; $i++){
   $randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageline($this->image, rand(0, $this->nWidth), rand(0, $this->nHeight), rand(0, $this->nWidth), rand(0, $this->nHeight), $randColor);
  }
 
  for ($i=0; $i<$this->nNoisePoint; $i++){
   $randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)); 
   imagesetpixel($this->image, rand(0, $this->nWidth), rand(0, $this->nHeight), $randColor);
  }
}
function getcolor($color)
{
   $color = eregi_replace ("^#","",$color);
   $r = $color[0].$color[1];
   $r = hexdec ($r);
   $b = $color[2].$color[3];
   $b = hexdec ($b);
   $g = $color[4].$color[5];
   $g = hexdec ($g);
   $color = imagecolorallocate ($this->image, $r, $b, $g);
   return $color;
}
}
?>
<?php
if (isset($_GET['action']) && $_GET['action']=='update')
{
    //update notice data
    //上传文件
    require_once("upload_class.php");
    $f = new Upload( $savepath, $fileFormat, $maxsize, $overwrite);
    if (!$f->run('img'))
    {//下面的img是Form中上传文件的input的名字
        echo $f->errmsg();  //这里只能传递最后一个出错的信息,详细的信息在$f->getInfo()中可以得到。
        print_r($f->returnArray);
    }
    echo "
    <script src="".__FILENAME__."/js/jquery.js" type="text/javascript"></script>
    <script>
    var ptext='';
    ptext="OK";
    //alert($('#img_view').html());
    parent.$('#img_view').html(ptext);
</script>";
}
?>
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="{{$tempurl}}/admin.css" rel="stylesheet" type="text/css" />
<link href="{{$baseurl}}/js/calendar/default/datePicker.css" rel="stylesheet" type="text/css" />
<script src="{{$baseurl}}/js/calendar/WdatePicker.js" type="text/javascript"></script>
<script src="{{$baseurl}}/js/jquery.js" type="text/javascript"></script>
<script src="{{$baseurl}}/js/Forms.js" type="text/javascript"></script>
<script src="{{$baseurl}}/js/FormValid.js" type="text/javascript"></script>
<script type="text/javascript">
      FormValid.succeed = function () {
      $.post("./article.php?action=update",$.getForms("update"),function (msg){
      alert(msg);
      if(msg=="更新公告成功")
      {
        location.href("./article.php");
      }
      //location.href("{{$baseurl}}/index.php");
  });
    return false; 
    }
function file_addupload(type)
{
  //负责增加图片输入框
  var ptext='';
  ptext=$("#"+type+"_more").html()+"<br /><input type='file' name='"+type+"[]' class='input' />";
  $("#"+type+"_more").html(ptext);
}
</script>
<title>发布文章--编辑页面</title>
</head>
<body >
<table width="99%" border="0" cellpadding="0" cellspacing="1" class="install">
  <tr>
    <td colspan="2" class="title">编辑文章内容</td>
  </tr>
  {{section name=article loop=$article}}
  <form action="./article.php?action=update" method="post" name="update" enctype="multipart/form-data" target="upload_iframe">
  <input type="hidden" name="article_id" value="{{$id}}">
  <tr>
    <td class="left">文章标题:</td>
    <td class="right"><input type="text" id="title" name="article_title" value="{{$article[article].title}}" class="input" size="35" valid="required" errmsg="文章标题不能为空" /></td>
  </tr>
  <tr>
    <td class="left">发布IP:</td>
    <td class="right"><input type="text" id="ip" value="{{$article[article].ip}}" name="ip" class="input" readonly /></td>
  </tr>
  <tr>
    <td class="left">作者:</td>
    <td class="right"><input type="text" id="author" name="article_validtime" value="{{$article[article].author}}"  valid="required" errmsg="请输入文章作者"  /></td>
  </tr>
  <tr>
    <td class="left">分类:</td>
    <td class="right"><select name='sort'>
{{html_options options=$sort_options selected=$article[article].sort }}
</select></td>
  </tr>
  <tr>
    <td class="left">来源:</td>
    <td class="right"><input type="text" id="comes" name="comes" value="{{$article[article].comes}}" class="input"/></td>
  </tr>
  <tr>
    <td class="left">是否置顶:</td>
    <td class="right">{{html_radios name='is_top' options=$is_top_radios checked=$article[article].is_top separator=' ' _note='是否置顶&#124;radio&#124;1=>是,0=>否' }}</td>
  </tr>
  <tr>
    <td class="left">内容:</td>
    <td class="right">{{$editor}}</td>
  </tr>
    <tr>
    <td class="left">图片上传:</td>
    <td class="right">
    <span id="img_view">
    <input type="file" name="img[]" class="input" />
      <a href="#" onclick="file_addupload('img')">++</a>
    <span id="img_more"></span>
    </span>
  </tr>
  <tr>
    <td class="left">文件上传:</td>
    <td class="right"><input type="file" name="file[]" class="input" />
      <a href="#" onclick="file_addupload('file')">++</a>
    <span id="file_more"></span>
</td>
  </tr>
  <tr>
    <td colspan="2" class="page" style="text-align:center;"><input type="submit" name="submit" class="button" value="保存" />    <input type="button" name="article_return" value="返回"  class="button" onclick="javascript:location.href('./article.php');" /></td>
  </tr>
  </form>
  {{/section}}
</table>
<iframe name="upload_iframe" style="width: 400px; height: 100px; display: none;">
</iframe>
</body>
</html>

php简单 在线投票系统 源码下载,上次要和你一家公合作他们说要我写一个简单的投票系统,但是每一个IP只能投一次,好了下面就是我写的一个最最简单的投票系统了,是用php实现的.

先来创建数据库.有两个一个记录投票次数与相关信息一个是记录IP.

CREATE TABLE IF NOT EXISTS `lj_vote` (
  `id` int(8) NOT NULL auto_increment,
  `v_type` int(4) default '1',
  `v_caption` varchar(500) default NULL COMMENT '此处可以放图片地址也可以是文章说明',
  `v_hits` int(8) NOT NULL default '0' COMMENT '投票次数',
  `v_ip` text COMMENT '投票IP为唯一,永远只有一次机会',
  `v_name` varchar(50) default NULL COMMENT '作品的作者',
  `v_id` int(4) default NULL COMMENT '唯一编号',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=21 ;

记录IP的数据库.

CREATE TABLE IF NOT EXISTS `lj_ip` (
  `id` int(8) NOT NULL auto_increment,
  `v_ip` varchar(50) default NULL,
  `v_time` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=4 ;

嗯.好了数据都准备好了我们就来看php  是怎么实现投票的吧.

<?php
  $value =$_SERVER['HTTP_HOST']; 
 $vote1 =isset($_POST['a'])?$_POST['a']:'';
 $vote2 =isset($_POST['b'])?$_POST['b']:'';
 $ip =get_real_ip();
 if( empty($vote1) || empty($vote2) ){
  exit("<script>alert('你还有未选择项目');history.back();</script>");
 }else{
  $re =mysql_query("select v_ip from lj_ip where v_ip='$ip'") or die('error');
  if( mysql_num_rows($re) ){
   exit("对不起,你己经投票了,<a href=# onclick=\"history.back();\">点击返回</a>");
  }else{
   mysql_query("update lj_vote set v_hits=v_hits+1 where v_name='$vote1'") or die('a');
   mysql_query("update lj_vote set v_hits=v_hits+1 where v_name='$vote2'") or die('b');
   mysql_query("insert into lj_ip(v_ip,v_time) value('$ip','".date("Y-m-d h:i:s")."')") or die('ip');
   exit("<script>alert('恭喜你,己成功投票');history.back();</script>");
  }
 }
  
 function show_hits($value){
  $result =mysql_query("select * from lj_vote where v_name='$value'") or die('error');
  return @mysql_num_rows($result);
 }
 
 function get_real_ip(){
    $ip=false;
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
     $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
     $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
     if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
     for ($i = 0; $i < count($ips); $i++) {
      if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
       $ip= $ips[$i];
       break;
      }
     }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  }

好了就OK了,投票系统不完成了,投票的htm页面我就没写了自己写一下吧.
?>

申明:本站原创转载请注明:  www.111cn.net

php 广告点击统计代码,昨天晚上有几个IDC网想与本站合作放些广告,但是我想看看广告效果后想了就写了一个简单的广告统计代码了,这里只是等的统计不能IP限制或是恶心点击等等了.

先来创建数据库.

CREATE TABLE IF NOT EXISTS `ad_count` (
  `ad_id` int(8) NOT NULL auto_increment,
  `ad_hit` int(8) NOT NULL default '0',
  `ad_name` varchar(200) character set gb2312 default NULL,
  `ad_datetime` datetime default NULL,
  `ad_qq` varchar(15) character set gb2312 default NULL,
  `ad_url` varchar(900) character set gb2312 NOT NULL,
  `ad_price` int(8) NOT NULL,
  PRIMARY KEY  (`ad_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

数据创建好了我们就开始写程序了.数据连接我不不写了,

<?php
 require_once("......省了");
 $ad_id =get_value('ad_id','');这里下面有讲
 if(is_numeric( $ad_id ) ){
 
   $ad_select="select ad_id,ad_url from ad_count where ad_id='$ad_id' limit 1";
   
   $ad_update="Update ad_count set  ad_hit=ad_hit+1 where ad_id='$ad_id' limit 1";
   
   $ad_result=mysql_query($ad_select) or die('id error');
   
     if( mysql_num_rows( $ad_result) ){
     
       mysql_query($ad_update);
       
       $ad_rs=mysql_fetch_array($ad_result);
       
       header("location:".$ad_rs['ad_url']);
       
       mysql_free_result($ad_rs);
              
     }
 }

function htmltohtml($Str){
  $temp =str_replace(chr(13),'<br/>',$Str);
  $temp =str_replace('<','&lt;',$Str);
  $temp =str_replace('>','&gt;',$Str);
  $temp =addslashes($Str);
  return $temp;
 }
 function get_value($value,$para){ 
  return htmltohtml(empty($para)?(isset($_GET[$value])?$_GET[$value]:''):(isset($_POST[$value])?$_POST[$value]:''));
 }

然后我们再在要显示广告的地方如下写.

'<a href=/cp/ad_count.php?ad_id=2 target=_blank><img src=/ad_a/ad2.gif border=0 /></a>

就完了了,本站原创,转载请注明来原:  www.111cn.net
?>

[!--infotagslink--]

相关文章

  • PHP 数据库缓存Memcache操作类

    操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Intellij IDEA连接Navicat数据库的方法

    这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
  • 在数据库里将毫秒转换成date格式的方法

    在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 如何解决局域网内mysql数据库连接慢

    通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
  • MySQL快速复制数据库数据表的方法

    某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
  • mysqldump命令导入导出数据库方法与实例汇总

    mysqldump命令的用法1、导出所有库系统命令行mysqldump -uusername -ppassword --all-databases > all.sql 2、导入所有库mysql命令行mysql>source all.sql; 3、导出某些库系统命令行mysqldump -uusername -ppassword...2015-10-21
  • Mysql数据库错误代码中文详细说明

    1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除数据库失败1011:删除数据库...2013-09-23
  • Linux下MySQL数据库的备份与还原

    深入解析Linux下MySQL数据库的备份与还原...2013-09-11
  • c#异步读取数据库与异步更新ui的代码实现

    这篇文章主要介绍了c#从数据库里取得数据并异步更新ui的方法,大家参考使用吧...2020-06-25
  • Yii2.0高级框架数据库增删改查的一些操作

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2使用中的一些基本的增删改查操作。 User::find()->all(); //返回所有用户数据; User::findOne($id); //返回 主键...2015-11-24
  • MYSQL数据库使用UTF-8中文编码乱码的解决办法

    1.用phpmyadmin创建数据库和数据表 创建数据库的时候,请将“整理”设置为:“utf8_general_ci” 或执行语句: 复制代码 代码如下:CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 创...2015-10-21
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • Linux 下使用shell脚本定时维护数据库的案例

    这篇文章主要介绍了Linux 下使用shell脚本定时维护数据库,本文通过案例分析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-11
  • MySQL数据库备份还原方法

    MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:/Program Files/MySQL/MySQL Server 4.1/bin (或者直接将windows的环境变量path中添加该目录) ...2013-09-26
  • PHP连接公司内部服务器的MYSQL数据库的简单实例

    “主机,用户名,密码”得到连接、“数据库,sql,连接”得到结果,最后是结果的处理显示。当然,数据库连接是扩展库为我们完成的,我们能做的仅仅是处理结果而已。...2013-09-29
  • node.js如何操作MySQL数据库

    这篇文章主要介绍了node.js如何操作MySQL数据库,帮助大家更好的进行web开发,感兴趣的朋友可以了解下...2020-10-29
  • C#连接加密的Sqlite数据库的方法

    对数据加密分两种,一种是对数据库本身进行加密,另一种是对数据表中的数据进行加密,下面通过本文给大家介绍C#连接加密的Sqlite数据库的方法,感兴趣的朋友一起看看吧...2020-06-25