php 批量过滤非法字符

 更新时间:2016年11月25日 15:58  点击:1899

function testAddslashes($array) {
 if(!get_magic_quotes_gpc()) {
  if(is_array($array)) {
   foreach($array as $key => $val) {
    $array[$key] = testAddslashes($val);
   }
  } else {   
   $array = addslashes($array);
  }
  $array=str_replace("&#x","& # x",$array); //过滤一些不安全

字符s
  $array=str_replace("<","&lt;",$array); //过滤<
 }
 return $array;
}

if( $_POST)
{
 print_r( $_POST );
 echo '过滤前<hr /> ';
 $_POST = testAddslashes($_POST);
 echo '<hr />过滤后<br />';
 echo $_POST['textfield'];
 
}
?>
<!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=gb2312" />
<title>无标题文档</title>
</head>

<body>

<form action="" method="post" enctype="multipart/form-data" name="form1"

id="form1">
  <label>
  <input type="text" name="textfield" />
  </label>
  <p>
    <label>
    <input type="text" name="textfield2" />
    </label>
    <label></label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="提交" />
    </label>
  </p>
</form>
</body>
</html>

/*
 将日间日期转换成时间时间戳
 strtotime(time,now)参数 描述
 time 规定要解析的时间字符串。
 now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。
 
 <?php
 echo strtotime("now"), " ";
 echo strtotime("10 September 2000"), " ";
 echo strtotime("+1 day"), " ";
 echo strtotime("+1 week"), " ";
 echo strtotime("+1 week 2 days 4 hours 2 seconds"), " ";
 echo strtotime("next Thursday"), " ";
 echo strtotime("last Monday"), " ";
 ?>
 
 <?php
 $str = 'Not Good';
 
 // previous to PHP 5.1.0 you would compare with -1, instead of false
 if (($timestamp = strtotime($str)) === false) {
  echo "The string ($str) is bogus";
 } else {
  echo "$str == " . date('l dS o F Y h:i:s A', $timestamp);
 }
 ?>
 
 再看strtotime实例
*/
 echo strtotime('2010-2-14'),"<br />";
 echo date('Y-m-d',strtotime('2010-2-14'));
 
 //输出值
 
 1266076800
 2010-02-14
 
 //你应该在strtotime(),你决定什么不能做。例如
 <?php

# on 2/8/2010
date('m/d/y', strtotime('first day')); # 02/01/10
date('m/d/y', strtotime('last day')); # 02/28/10
date('m/d/y', strtotime('last day next month')); # 03/31/10
date('m/d/y', strtotime('last day last month')); # 01/31/10
date('m/d/y', strtotime('2009-12 last day')); # 12/31/09 - this doesn't work if you reverse the order of the year and month
date('m/d/y', strtotime('2009-03 last day')); # 03/31/09
date('m/d/y', strtotime('2009-03')); # 03/01/09
date('m/d/y', strtotime('last day of march 2009')); # 03/31/09
date('m/d/y', strtotime('last day of march')); # 03/31/10
?>

//用php curl_init函数
$ch = curl_init();
  $timeout = 5;
  curl_setopt ($ch, CURLOPT_URL, "$url");
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0

(compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $contents = curl_exec($ch);
  curl_close($ch);
  
//方法用php fsockopen

function ReadPR($link)
{
 $fp = fsockopen ("www.google.com", 80, $errno, $errstr, 30);
 if (!$fp)
 {
  echo "$errstr ($errno) ";
  exit(1);
 }
 else
 {
  $out = "GET $link HTTP/1.0 ";
  $out .= "Host: toolbarqueries.google.com ";
  $out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar

2.0.114.9-big; Linux 2.6) ";
  $out .= "Connection: Close ";
  fwrite($fp, $out);
  do{
  $line = fgets($fp, 128);
  }while ($line !== " ");
  $data = fread($fp,8192);
  fclose ($fp);
  return $data;
 }
}

$now_time = time(); //当前时间
$for_time = mktime(0,0,0,2,24,2010);//设置一个日期
$n_time = $now_time-$for_time;
if( $n_time > 2592000 )

 $echo = round($n_time/2592000).'月前';
}
elseif( $n_time > 6048000 )
{
 $echo = round($n_time/604800).'周前';
}
elseif( $n_time > 86400 )
{
 $echo = round($n_time/86400).'天前';
}
elseif( $n_time > 3600 )
{
 $echo = round($n_time/3600).'小时前';
}
elseif( $n_time > 60 )
{
 $echo = round($n_time/60).'分钟前';
}
else
{
 $echo = $n_time.'秒前';
}
echo $echo;

如何创建一个CSV文件

方法1 - 使用HTTP头

至于在Word和Excel,您需要添加头信息到PHP脚本的例子。

下面的代码片断创建一个指定的表包括其列名CSV文件。然后会提示用户下载此文件。

<?php
$table = 'table_name';
$outstr = NULL;

header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=cars-models.csv");

$conn = mysql教程_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("db",$conn);

// Query database to get column names 
$result = mysql_query("show columns from $table",$conn);
// Write column names
while($row = mysql_fetch_array($result)){
    $outstr.= $row['Field'].',';

$outstr = substr($outstr, 0, -1)." ";

// Query database to get data
$result = mysql_query("select * from $table",$conn);
// Write data rows
while ($row = mysql_fetch_assoc($result)) {
    $outstr.= join(',', $row)." ";
}

echo $outstr;
mysql_close($conn);
?>

方法2 - 使用fputcsv()

在fputcsv()函数格式作为CSV行并将其写入一个打开的文件。欲了解更多信息,看

一看http://php.net/manual/en/function.fputcsv.php看看。

下面的代码片断创建一个指定表的列名,包括CSV文件,并将其发送到浏览器。

<?php
$table = 'table_name';
$filename = tempnam(sys_get_temp_dir(), "csv");

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("db",$conn);

$file = fopen($filename,"w");

// Write column names
$result = mysql_query("show columns from $table",$conn);
for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $colArray[$i] = mysql_fetch_assoc($result);
    $fieldArray[$i] = $colArray[$i]['Field'];
}
fputcsv($file,$fieldArray);

// Write data rows
$result = mysql_query("select * from $table",$conn);
for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $dataArray[$i] = mysql_fetch_assoc($result);
}
foreach ($dataArray as $line) {
    fputcsv($file,$line);
}

fclose($file);

header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=cars-models.csv");

// send file to browser
readfile($filename);
unlink($filename);
?>

[!--infotagslink--]

相关文章