php需要一定的时间格式才能转换成时间戳(表示从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数),这就要用到php正则判断,以下是代码:
//匹配时间格式为2012-02-16或2012-02-16 23:59:59前面为0时可以不写
$time = "2012-02-16 23:59:59";
$patten = "/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])(\s+(0?[0-9]|1[0-9]|2[0-3])\:(0?[0-9]|[1-5][0-9])\:(0?[0-9]|[1-5][0-9]))?$/";
if (preg_match ( $patten, $time )) {
echo $timestro = strtotime ( $time );
} else {
echo "error";
}
?>
对该正则的分析:
/是表达式开始符 最后面的/是表达式的结束符
^字符串的开始,即以***开始
$表示字符串的结尾,即表示已***结尾
|表示或者
()表示一个单元
d{4}匹配一个4位数,
(\-)匹配一个“-”号,
(0?[1-9]|1[012])匹配月份,
(0?[1-9]|[12][0-9]|3[01])匹配天,
(\s+(0?[0-9]|1[0-9]|2[0-3])\:(0?[0-9]|[1-5][0-9])\:(0?[0-9]|[1-5][0-9]))?整个匹配时分秒部分,?表示有或者没有。
最后一步是对匹配成功的部分,进行时间戳转换。
当前转换结果为:1329407999
您可能感兴趣的文章:
php匹配图片地址的代码一例
php匹配任何网址的正则表达式
php正则匹配重写html图片img路径的代码一例
PHP正则匹配获取URL中域名的代码
使用 preg_replace 函数 匹配图片并加上链接的方法
php用preg_match_all匹配文章中的图片
php正则表达式匹配URL中的域名
在php编程中,对于一些访问没有明显错误提示的php页面,可以通过error_log来做进一步的判定。
但出于种种原因,有些服务器并没有开启PHP的error_log功能。
测试或其它需要时,可以打开一下,方法如下。
编辑php.ini,将log_errors设置为on:
然后,重启apache即可。
如成功开启,就可以跟踪到对应的错误提示:
[Mon Sep 24 16:57:01 2012] [error] [client 218.5.80.210] PHP Warning: fsockopen() has been disabled for security reasons in /home/bccgi-bin/fsockopen.php on line 2
[Mon Sep 24 16:57:02 2012] [error] [client 218.5.80.210] PHP Warning: fsockopen() has been disabled for security reasons in /home/bccgi-bin/fsockopen.php on line 2
[Mon Sep 24 16:57:03 2012] [error] [client 218.5.80.210] PHP Warning: fsockopen() has been disabled for security reasons in /home/bccgi-bin/fsockopen.php on line 2
[Mon Sep 24 16:57:04 2012] [error] [client 218.5.80.210] PHP Warning: fsockopen() has been disabled for security reasons in /home/bccgi-bin/fsockopen.php on line
另外,注意在Windows环境下,除了将log_errors设置为on外,还需要定义error_log的路径及文件名:
(此目录需要授予php标识用户的修改权限,否则日志文件无法生成)
IIS没有error_log的概念,所以需要另外定义。
无论是php编程,还是基于系统的运维等,日志文件都相当重要的,养成收集与分析日志的习惯吧,有时会在解决问题时带来大的帮助。
1,生成验证码程序ttt.php。
通过random生成随机数,然后保存在session里:
<?php
/**
* 验证码程序
* edit www.
*/
session_start();
function random($length, $numeric = 0) {
mt_srand((double)microtime() * 1000000);
if($numeric) {
$hash = sprintf()('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}
$seccode = random(4, 1);
$_SESSION['seccode'] = $seccode;
$seccode = sprintf('%04d', $seccode);
if(!$nocacheheaders) {
@header("Expires: -1");
@header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
@header("Pragma: no-cache");
}
if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagesetpixel') && function_exists('imagechar') && function_exists('imagecreatefromgif') && function_exists('imagepng')) {
$im = imagecreate(62, 25);
$backgroundcolor = imagecolorallocate ($im, 255, 255, 255);
$numorder = array(1, 2, 3, 4);
shuffle($numorder);
$numorder = array_flip($numorder);
for($i = 1; $i <= 4; $i++) {
$imcodefile = 'seccode/'.$seccode[$numorder[$i]].'.gif';
$x = $numorder[$i] * 13 + mt_rand(0, 4) - 2;
$y = mt_rand(0, 3);
if(file_exists($imcodefile)) {
$imcode = imagecreatefromgif($imcodefile);
$data = getimagesize($imcodefile);
imagecolorset($imcode, 0 ,mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255));
imagecopyresized($im, $imcode, $x, $y, 0, 0, $data[0] + mt_rand(0, 6) - 3, $data[1] + mt_rand(0, 6) - 3, $data[0], $data[1]);
} else {
$text_color = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255));
imagechar($im, 5, $x + 5, $y + 3, $seccode[$numorder[$i]], $text_color);
}
}
$linenums = mt_rand(10, 32);
for($i=0; $i <= $linenums; $i++) {
$linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
$linex = mt_rand(0, 62);
$liney = mt_rand(0, 25);
imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
}
for($i=0; $i <= 64; $i++) {
$pointcolor = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 255), mt_rand(50, 255));
imagesetpixel($im, mt_rand(0, 62), mt_rand(0, 25), $pointcolor);
}
$bordercolor = imagecolorallocate($im , 150, 150, 150);
imagerectangle($im, 0, 0, 61, 24, $bordercolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
} else {
$numbers = array
(
0 => array('3c','66','66','66','66','66','66','66','66','3c'),
1 => array('1c','0c','0c','0c','0c','0c','0c','0c','1c','0c'),
2 => array('7e','60','60','30','18','0c','06','06','66','3c'),
3 => array('3c','66','06','06','06','1c','06','06','66','3c'),
4 => array('1e','0c','7e','4c','2c','2c','1c','1c','0c','0c'),
5 => array('3c','66','06','06','06','7c','60','60','60','7e'),
6 => array('3c','66','66','66','66','7c','60','60','30','1c'),
7 => array('30','30','18','18','0c','0c','06','06','66','7e'),
8 => array('3c','66','66','66','66','3c','66','66','66','3c'),
9 => array('38','0c','06','06','3e','66','66','66','66','3c')
);
for($i = 0; $i < 10; $i++) {
for($j = 0; $j < 6; $j++) {
$a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);
$a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1);
mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);
mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);
}
}
$bitmap = array();
for($i = 0; $i < 20; $i++) {
for($j = 0; $j < 4; $j++) {
$n = substr($seccode, $j, 1);
$bytes = $numbers[$n][$i];
$a = mt_rand(0, 14);
switch($a) {
case 1: str_replace()('9', '8', $bytes); break;
case 3: str_replace('c', 'e', $bytes); break;
case 6: str_replace('3', 'b', $bytes); break;
case 8: str_replace('8', '9', $bytes); break;
case 0: str_replace('e', 'f', $bytes); break;
}
array_push($bitmap, $bytes);
}
}
for($i = 0; $i < 8; $i++) {
$a = substr('012', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);
array_unshift($bitmap, $a);
array_push($bitmap, $a);
}
$image = pack('H*', '424d9e000000000000003e000000280000002000000018000000010001000000'.
'0000600000000000000000000000000000000000000000000000FFFFFF00'.implode('', $bitmap));
header('Content-Type: image/bmp');
echo $image;
}
?>
2,登录界面login.html:
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head>
<title>login登录界页-www.</title>
<style type="text/css">
<!--
.textbox {
height: 18px;
width: 100px;
}
.text {
font-size: 14px;
vertical-align: bottom;
color: #0000FF;
}
.style1 {
font-size: 18px;
color: #0000FF;
font-family: "幼圆";
}
-->
</style>
</head>
<body>
<table width="200">
<tr><td align="center" valign="bottom" bgcolor="#C7D3F9">请输入验证码</td>
</tr>
</table>
<form method="post" action="/blog_article/login.html">
<table width="200" border="0" bgcolor="C7D3F9">
<tr>
<td >验证码:</td>
<td align="right" valign="bottom"><input type="text" name="auth" ></td>
</tr>
</table>
<img src="/blog_article/ttt/act/yes.html" align="middle">
<table width="200"><tr><td align="right"><input type="button" value="看不清楚验证码" onclick="window.location.reload();"><input name="submit" type="submit" value="Submit"></td></tr></table>
</form>
</body>
</html>
3,认证程序login.php:
session_start();
$auth = $_POST['auth'];
if($_SESSION['seccode'] == $auth)
{
print '
<script language=javascript>
alert("登录成功!");
</script>';
}else
{
print '
<script language=javascript>
alert("登录失败,请重新登录!");
self.window.location="login.html";
</script>';
}
?>
您可能感兴趣的文章:
php随机生成4位数字验证码
php验证码简单函数代码(附效果图)
分享一个php 验证码类及调用示例
php验证码的三个实例代码分享
一个php验证码的封装类
php自定义大小验证码的实例代码
php仿QQ验证码的实现代码
php验证码函数使用的例子
php5验证码类(简易实用型)
php验证码(GD库生成验证码)的例子
php图片验证码的例子
php彩色验证码的简单例子
php验证码刷新与局部刷新的实现方法
php GD库生成验证码的实例
php生成验证码的例子
php 验证码类 php 验证码
php随机验证码 php生成随机验证码(图文)
一个比较稳定的php登陆系统验证码
用php生成带有雪花背景的验证码
php生成动态图片验证码的一段代码