PHP 实现的验证码 晴

jed , 2007-3-19 09:38 , 代码编程 , 评论(0) , 阅读(6253) , Via 本站原创
  验证码就是将一串随机产生的数字或符号,生成一幅图片,图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。

  方法如下,代码一:



  <?php

    /* * Filename: authpage.php * Author: hutuworm

    * Date: 2003-04-28

    * @Copyleft hutuworm.org

    */ srand((double)microtime()*1000000);

    //验证用户输入是否和验证码一致

    if(isset($HTTP_POST_VARS['authinput'])) {       if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0) echo "验证成功!";

    else

      echo "验证失败!";

    }

    //生成新的四位整数验证码

    while(($authnum=rand()%10000)<1000);

   ?>

请输入验证码: > >

  代码二:


  <?php

    /* * Filename: authimg.php

    * Author: hutuworm * Date: 2003-04-28

    * @Copyleft hutuworm.org

    */

    //生成验证码图片

    Header("Content-type: image/PNG");

    srand((double)microtime()*1000000);

    $im = imagecreate(58,28); $black = ImageColorAllocate($im, 0,0,0);

    $white = ImageColorAllocate($im, 255,255,255);

    $gray = ImageColorAllocate($im, 200,200,200);

    imagefill($im,68,30,$gray);

    //将四位整数验证码绘入图片

    imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black);

    for($i=0;$i<50;$i++) //加入干扰象素 {

      imagesetpixel($im, rand()%70 , rand()%30 , $black);

    }

    ImagePNG($im);

    ImageDestroy($im);

  ?>

  这段程序已经基本上实现了验证码的生成和校验功能,但是文章作者不知道为什么却将验证码的内容显示在表单里了,这样的话,只是限制了用户必须输入验证码,对恶意程序却没有任何防范作用。可以说是在难为人,而不是防范攻击。

  不过还好根据原作者的思路,我们可以将验证串保存在 session 里,这样的话,才具有一定的安全性。 代码如下:


  <?php

     //file:authform.php 请输入验证码:

    /* * Filename:authimg.php

    */

    Header("Content-type:image/PNG");

    session_start();

    $auth_num="";

    session_register('auth_num');

    $im=imagecreate(63,20);

    srand((double)microtime()*1000000);

    $auth_num_k=md5(rand(0,9999));

    $auth_num=substr($auth_num_k,17,5);

    $black=ImageColorAllocate($im,0,0,0);

    $white=ImageColorAllocate($im,255,255,255);

    $gray=ImageColorAllocate($im,200,200,200);

    //ImageFill($im,63,20,$black);

    //这行不知道为什么在我公司的服务器上出错误,换个空间ok

    imagestring($im,5,10,3,$auth_num,$gray);

    for($i=0;$i<200;$i++) {

      $randcolor=ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

      imagesetpixel($im,rand()%70,rand()%30,$randcolor);

    }

    ImagePNG($im);

    ImageDestroy($im);

  ?>



  <?php

    /* * Filename:authpage.php */

    session_start();

    $num=trim($num);

    if($auth_num==$num && $num<>""){

      echo "验证成功";

    }

    else{

      echo "验证失败";

    }

  ?>


Tags:
创建日期filectime($filepath)
最后修改filemtime($filepath)
文件大小number_format(filesize($filepath)/1024, 3)K
文件属性substr(base_convert(fileperms($filepath),10,8),-4)
表单输入限制(小结)

只能输入汉字:


<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">


只能输入数字:


<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">


只能输入英文和数字:


<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">


今天整理商贸程序,其中得需要计算一个字符串中某个字符出现的次数,就上网搜,好不容易找到一个函数substr_count(),

<?php
$text = 'This is a test' ;
echo strlen ( $text ); // 14

echo substr_count ( $text , 'is' ); // 2

// the string is reduced to 's is a test', so it prints 1
echo substr_count ( $text , 'is' , 3 );

// the text is reduced to 's i', so it prints 0
echo substr_count ( $text , 'is' , 3 , 3 );

// generates a warning because 5+10 > 14
echo substr_count ( $text , 'is' , 5 , 10 );


// prints only 1, because it doesn't count overlapped subtrings
$text2 = 'gcdgcdgcd' ;
echo substr_count ( $text2 , 'gcdgcd' );
?>
Tags: ,

php2html php生成静态页函数 晴

jed , 2006-12-23 17:27 , 代码编程 , 评论(0) , 阅读(6275) , Via 本站原创

<?php
/**
------------------------
  Function: php2html($in_Url, $out_htmlFile, $out_logFile)
------------------------
@ Description: 生成静态函数
@ Author 眼睛5462    
@ QQ: 332458700 [E-mail:yanjing5462@my55.com.cn]
@ Copyright: Copyright (c) 2006 - 2011 MY55.com.cn
@ Create: 2006-08-01
@ Modify: 2006-10-27
@ 提示:这里要用到的路径为服务器绝对路径; 若给定的路径目录不存在则自动创建
=======================================================================================
@ Example:php2html("http://www.baidu.com", "/www/html/index.html", "/www/log/log.txt");
*/
           
// {{{     contents

   function php2html($in_Url, $out_htmlFile, $out_logFile)
   {            
       $htmlContent = file_get_contents($in_Url); //将文件读入 $htmlContent 变量

       /**
        * @检查要生成的文件是否存在
        */
       if (is_file($out_htmlFile))
       {
               @unlink($out_htmlFile);//若文件已存在,则删除
       }
       
       /**
        * @ 创建目录 网页部分
        */
       $dir_array = explode("/", dirname($out_htmlFile));

       chdir("/"); //改变目录到根

       for($i=1;$i<count($dir_array);$i++)
       {
           if(is_dir($dir_array[$i]))
           {
               chdir($dir_array[$i]);            
           }
           else
           {
               mkdir($dir_array[$i]);
               chdir($dir_array[$i]);
           }
       }

       /**
        * @ 创建目录 日志部分
        */
       $dir_array = explode("/", dirname($out_logFile));

       chdir("/"); //改变目录到根

       for($i=1;$i<count($dir_array);$i++)
       {
           if(is_dir($dir_array[$i]))
           {
               chdir($dir_array[$i]);            
           }
           else
           {
               mkdir($dir_array[$i], 0777);
               chdir($dir_array[$i]);
           }
       }

       $handle = fopen($out_htmlFile, "w"); //打开文件指针,创建文件
       $logHandle    = fopen ($out_logFile, "a+"); //打开日志文件

       /**
        * @检查目录是否可写
        */
       if (!is_writable($out_htmlFile))
       {
           echo "文件:".$out_htmlFile."不可写,请检查目录属性后重试";
           exit();
       }
       if (!is_writable($out_logFile))
       {
           echo "文件:".$out_logFile."不可写,请检查目录属性后重试";
           exit();
       }

       /**
        * @写入文件
        */
       if (!fwrite ($handle, $htmlContent))
       {
           $logMsg = "写入文件" . $out_htmlFile . "失败";
       }
       else
       {
           $logMsg = "创建文件" . $out_htmlFile . "成功";
       }

       /**
        * @记录日志
        */
       $logMsg    .= "(".date("Y-m-d H:i:s") .")\r\n";
       fwrite ($logHandle, $logMsg);
       fclose($logHandle); //关闭日志指针

       fclose ($handle); //关闭指针
   }

// }}}

php2html("http://www.baidu.com", dirname(__FILE__)."/yanjing_html/index.html", dirname(__FILE__)."/yanjing_log/log.txt");
echo "成功";
?>


ajax乱码问题的整理 晴

jed , 2006-12-20 16:13 , 代码编程 , 评论(0) , 阅读(3484) , Via 本站原创
产生原因
主要有2个原因
1 xtmlhttp 返回的数据默认的字符编码是utf-8,如果前台页面是gb2312或者其它编码数据就会产生乱码
2 post方法提交数据默认的字符编码是utf-8,如果后台是gb2312或其他编码数据就会产生乱码

解决的办法就是在送出的流里面加一个HEADER,指明送出的是什么编码流,这样XMLHTTP就不会乱搞了。


PHP:header('Content-Type:text/html;charset=GB2312');
ASP:Response.Charset("GB2312")
JSP:response.setHeader("Charset","GB2312");

用php实现发信功能 晴

jed , 2006-12-19 15:42 , 代码编程 , 评论(0) , 阅读(5991) , Via 本站原创
1. 使用 mail() 函数

没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发。这个按照各个系统不同而定。使用参考手册。

2. 使用管道的形式

有网友曾经测试成功,使用本地的qmail来发送邮件。
Tags:

按钮代码:



<button onclick="copyToClipBoard()">复制本贴地址发送给好朋友</button>



调用函数



<script language="javascript">
function copyToClipBoard(){
var clipBoardContent="";
clipBoardContent+=document.URL
window.clipboardData.setData("Text",clipBoardContent);
alert("复制成功");
}
</script>  



用php5实现MD5加密取16位 晴

jed , 2006-12-16 09:23 , 代码编程 , 评论(0) , 阅读(4059) , Via 本站原创
php之md5加密2006-08-19 13:03php5之取16位md5值


用php5实现

PHP代码:--------------------------------------------------------------------------------



<?php
/****
php5 get md5 value
****/

$value = md5("just a test", true);
echo $value;

?>


--------------------------------------------------------------------------------

用php4实现(不能直接取16位的,要么自己写个,要么截取)

PHP代码:--------------------------------------------------------------------------------



<?php
/****
php4 get md5 value
****/

$value = hex2bin(md5("just a test"));
echo $value;

/*
+--------------------------------------------------
|   函数名: hex2bin($data)
|   作用: 将16进转换为2进
|   参数: $data
|    
|
|   返回值: 二进bit流
+--------------------------------------------------
*/
function hex2bin($data)
{
   $len = strlen($data);
   $newdata = '';

   for($i=0;$i<$len;$i+=2)
   {
       $newdata .= pack("C",hexdec(substr($data,$i,2)));
   }

   return $newdata;
}


Tags:

返回上一页代码 晴

jed , 2006-12-12 09:44 , 代码编程 , 评论(0) , 阅读(4370) , Via 本站原创
动态返回上一页代码:



Response.Redirect(Request.ServerVariables("HTTP_HOST"))



静态返回上一页代码:



javascript:history.back()


-----------------------------------------------------------------


<a href="javascript:history.back(-1)">返回上一页</a>

         
分页: 17/26 第一页 上页 12 13 14 15 16 17 18 19 20 21 下页 最后页 [ 显示模式: 摘要 | 列表 ]