服务器 频道

PHP日常实用小Tips

  【IT168 服务器学院】1.简易判断ip地址合法性
  if(!strcmp(long2ip(sprintf("%u",ip2long($ip))),$ip)) echo "is ipn";

  2.email的正则判断
  eregi("^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z_-]+.)+[a-zA-Z]$", $email);

  3.检测ip地址和mask是否合法的例子
  $ip = ''192.168.0.84'';
  $mask = ''255.255.255.0'';
  $network = ''192.168.0'';

  $ip = ip2long($ip);
  $mask = ip2long($mask);
  $network = ip2long($network);

  if( ($ip & $mask) == $network) echo "valid ip and maskn";
  ?>

  4.关于表单刷新
  问:为什么我在点击浏览器的后退按钮后,所有字段的信息都被清空了?

  答:这是由于你在你的表单提交页面中使用了 session_start 函数。该函数会强制当前页面不被缓存。解决办法为,在你的 Session_start 函数后加入 header("Cache-control: private"); 注意在本行之前你的PHP程序不能有任何输出。

  补充:还有基于session的解决方法,在session_start前加上
  session_cache_limiter(''nocache'');// 清空表单
  session_cache_limiter(''private''); //不清空表单,只在session生效期间
  session_cache_limiter(''public''); //不清空表单,如同没使用session一般

  可以在session_start();前加上session_cache_limiter("private,max-age=10800");

  5.快速搞定文件下载头部输出

  header("Content-type: application/x-download");
  header("Content-Disposition: attachment; filename=$file_download_name;");
  header("Accept-Ranges: bytes");
  header("Content-Length: $download_size");
  echo ''xxx''

  6.用header输出ftp下载方式,并且支持断点续传
  一个例子:
  header(''Pragma: public'');
  header(''Cache-Control: private'');
  header(''Cache-Control: no-cache, must-revalidate'');
  header(''Accept-Ranges: bytes'');
  header(''Connection: close'');
  header("Content-Type: audio/mpeg");

  header("Location:ftp://download:1bk3l4s3k9s2@218.30.116.103/1001/咖哩辣椒/咖喱辣椒.rmvb");

  7.正则匹配中文
  ereg("^[".chr(0xa1)."-".chr(0xff)."]+$", $str);

  8.批量替换文本里面的超级链接
  function urlParse($str = '''')
  {
      if ('''' == $str) return $str;

  $types = array("http", "ftp", "https");

  $replace = <<  ''''.htmlentities(''1'').htmlentities(''2'').''''
  EOPHP;

  $ret = $str;

  while(list(,$type) = each($types))
  {
    $ret = preg_replace("|($type://)([^s]*)|ie ", $replace, $ret);
  }

  return $ret;
  }
  ?>

   

0
相关文章