php 生成验证程序
<?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;
}
}
?>
jquery与 ajax 简单例子
基于JQuery框架的AJAX
PS:本人这篇始发于PHPCHINA,发现被很多人转了,但却未注明出处,想了一下,还是自己转到这里来。
前几天发了个贴,分享了prototype框架关于AJAX方面的学习过程。然后有人说jquery框架更方便。
正好项目中准备使用thickbox,于是干脆抛弃prototype.js,看起jquery.js了。JQuery确实不错,体积比Prototype小了许多,而且使用起来更方便更灵活。有人说Prototype像JAVA,正统;而JQuery像Ruby,灵活,更趋于OOP。
小试了下AJAX,感觉比prototype简洁多了,在JQuery中,AJAX有三种实现方式:$.ajax(),$.post,$.get()。
XHTML(主要):
代码: 复制内容到剪贴板
<div id="result" style="backgroundrange;border:1px solid red;width:300px;height:400px;"></div>
<form id="formtest" action="" method="post">
<p><span>输入姓名:</span><input type="text" name="username" id="input1" /></p>
<p><span>输入年龄:</span><input type="text" name="age" id="input2" /></p>
<p><span>输入性别:</span><input type="text" name="sex" id="input3" /></p>
<p><span>输入工作:</span><input type="text" name="job" id="input4" /></p>
</form>
<button id="send_ajax">提交</button>
<button id="test_post">OST提交</button>
<button id="test_get">GET提交</button>
JS:
1、引入jquery框架:
代码: 复制内容到剪贴板
<script type="text/javascript" src="../js/jquery.js"></script>
2、构建AJAX,JQUERY的好处是不需要在XHTML中使用JS代码来触发事件了,可以直接封装在JS文件中:
代码: 复制内容到剪贴板
<script type="text/javascript">
//$.ajax()方式
$(document).ready(function (){
$('#send_ajax').click(function (){ //直接把onclick事件写在了JS中,而不需要混在XHTML中了
var params=$('input').serialize(); //序列化表单的值,与prototype中的form.serialize()相同
$.ajax({
url :'ajax_test.php', //后台处理程序
type:'post', //数据发送方式
dataType:'json', //接受数据格式
data:params, //要传递的数据
success:update_page //回传函数(这里是函数名)
});
});
});
function update_page (json) { //回传函数实体,参数为XMLhttpRequest.responseText
var str="姓名:"+json.username+"";
str+="年龄:"+json.age+"";
str+="性别:"+json.sex+"";
str+="工作:"+json.job;
$("#result").html(str);
}
//$.post()方式:
$(function (){//$(document).ready(function (){ 的简写
$('#test_post').click(function (){
$.post('ajax_test.php',
{username('#input1').val(),age('#input2').val(),sex('#input3').val(),job('#input4').val()},
function (data){ //回传函数
var myjson='';
eval('myjson='+data+';');
$('#result').html("姓名:"+myjson.username+"
工作:"+myjson['job']);
});
});
});
$.get()方式:
$(function (){
$('#test_get').click(function (){
$.get('ajax_test.php',
{username("#input1").val(),age("#input2").val(),sex("#input3").val(),job("#input4").val()},
function (data) {
var myjson='';
eval("myjson="+data+";");
$("#result").html(myjson.job);
});
});
});
</script>
PHP代码:
代码: 复制内容到剪贴板
<?php
$arr=$_POST; //若以$.get()方式发送数据,则要改成$_GET.或者干脆_REQUEST
$myjson=json_encode($arr);
echo $myjson;
?>
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
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='是否置顶|radio|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
相关文章
- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
- 本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
- 大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
- 这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
- 这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
Django def clean()函数对表单中的数据进行验证操作
这篇文章主要介绍了Django def clean()函数对表单中的数据进行验证操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-09将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文
时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25- 本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
- 这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
- 本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
- 这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
- 这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
- 这篇文章主要为大家详细介绍了JavaScript实现密码框输入验证,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-01
- 这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
- 关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
- 这篇文章主要介绍了Nest.js 授权验证的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-22
- 这篇文章主要介绍了el-table树形表格表单验证(列表生成序号),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-01