php自动生成新密码的两种方法,方法一采用的是随机种子,方法是乱序数组,异曲同工吧。
有需要的朋友可以参考下。
方法1:
/*
* php自动生成新密码自定义函数
适用环境: PHP5.2.x / mysql 5.0.x
代码作者: xujiajay
* */
function genPassword($min = 5, $max = 8)
{
$validchars="abcdefghijklmnopqrstuvwxyz123456789";
$max_char=strlen($validchars)-1;
$length=mt_rand($min,$max);
$password = "";
for($i=0;$i<$length;$i )
{
$password.=$validchars[mt_rand(0,$max_char)];
}
return $password;
}
echo "新密码:".genPassword()."<br>";
echo "新密码:".genPassword(5,10)."<br>";
?>
方法2:
<?php
/**
php 生成随机密码
*/
function generatePassword($length=8)
{
$chars = array_merge(range(0,9),
range('a','z'),
range('A','Z'),
array('!','@','$','%','^','&','*'));
shuffle($chars);
$password = '';
for($i=0; $i<8; $i++) {
$password .= $chars[$i];
}
return $password;
}
echo "新密码:" . generatePassword(16);
?>
/**
php自动生成月历
link:www. 2013-3-7
*/
function calendar($MM,$YYYY){
if($MM=="") $MM = date("m");
if($YYYY=="") $YYYY = date("Y");
if(checkdate($MM,1,$YYYY)){
$stringDate = strftime("%d %b %Y",mktime (0,0,0,$MM,1,$YYYY));
$days = strftime("%d",mktime (0,0,0,$MM+1,0,$YYYY));
$firstDay = strftime("%w",mktime (0,0,0,$MM,1,$YYYY));
$lastDay = strftime("%w",mktime (0,0,0,$MM,$days,$YYYY));
$PRintDays = $days;
$preMonth = strftime("%m",mktime (0,0,0,$MM-1,1,$YYYY));
$preYear = strftime("%Y",mktime (0,0,0,$MM-1,1,$YYYY));
$nextMonth = strftime("%m",mktime (0,0,0,$MM+1,1,$YYYY));
$nextYear = strftime("%Y",mktime (0,0,0,$MM+1,1,$YYYY));
print("<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\">");
print("<tr><th valign=\"top\"><a href=/index.html"".$_SERVER['PHP_SELF']."?NB=".$_GET["NB"]."&MM=".$preMonth."&YY=".$preYear."\">P</a></th>");
print("<th colspan=\"5\" valign=\"top\">".strftime("%b %Y",mktime (0,0,0,$MM,1,$YYYY))."</th>");
print("<th valign=\"top\"><a href=/index.html"".$_SERVER['PHP_SELF']."?NB=".$_GET["NB"]."&MM=".$nextMonth."&YY=".$nextYear."\">N</a></th></tr>");
print("<tr font-family: Verdana; font-size:x-small\">");
print("<th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>");
$currentDays = 1;
for($a=1;$a<=5;$a++){
print("<tr align=\"left\" valign=\"top\" font-family: Verdana; font-size:x-small\">");
$diffDays = $firstDay-$lastDay;
if($firstDay>$lastDay && $currentDays ==1 && ($diffDays<>1)){
for($x=$lastDay;$x>=0;$x--){
$printDays = $days-$x;
print("<td>$printDays</td>");
}
for($z=1;$z<$firstDay-$lastDay;$z++){
print("<td> </td>");
}
for($y=$firstDay;$y<7;$y++){
print("<td>$currentDays</td>");
$currentDays++;
}
} elseif($firstDay!=0 && $currentDays==1){
for($z=1;$z<=$firstDay;$z++){
print("<td> </td>");
}
for($y=$firstDay;$y<7;$y++){
print("<td>$currentDays</td>");
$currentDays++;
}
} else {
for($u=1;$u<=7 && $currentDays<=$days;$u++){
print("<td>$currentDays</td>");
$currentDays++;
}
}
print("</tr>");
}
print("</table>");
}
}
?>
php自动生成TXT文件的代码,当客户端即访问PHP时可以生成TXT并自动下载。
有需要的朋友可以参考下。
1、处理中文文件名:
<?php
$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = "中文文件名.txt";
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace()("+", "%20", $encoded_filename);
$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = "中文文件名.txt";
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
?>
以上代码可支持下载中文文件名。
2、生成TXT文件
<?php
header("Content-Type: application/octet-stream");
if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {
header('Content-Disposition: attachment; filename*="utf8' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
header("Content-Type: application/octet-stream");
if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {
header('Content-Disposition: attachment; filename*="utf8' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}//兼容各种浏览器。
?>
3、输出内容
这一步就简单多了,直接用echo输出,“\r\n”用以换行。
输出的内容即为txt里的内容。
此功能也可以生成doc文件,只要把后缀改成.doc即可,不过按这种方法只能生成简单的doc文件,如果有图片或链接什么的,则只能另寻他途了。
,祝您学习愉快。