当前位置:  编程技术>php
本页文章导读:
    ▪PHP记录搜索引擎来路、关键字的示例代码      本节内容: PHP记录搜索引擎来路、关键字 例子:   代码示例: <?php /*  * @ 记录搜索引擎来路、关键字  * @ edit: www.  /* function keywords($url){    $spier=array('baidu.'=>'百度','google.'=>'.........
    ▪php socket使用smtp发送带附件的邮件      本节内容: php socket smtp发送带附件的邮件 例子:   代码示例: <?php /** * php socket smtp发送邮件 * edit: www. */ //define("SOL", "\n"); define("EOL", "\r\n"); define("SMTP_HOST", "smtp.163.com");//SMTP服务器 d.........
    ▪php计算指定文件夹信息(文件夹数,文件数,文件夹大小)      本节内容: 统计文件夹信息的php代码。 此代码,实现如下功能: 计算文件夹的大小,包括子文件夹,格式化输出文件夹大小、文件数、子文件夹数信息。 例子:   代码示例: <? //格式化.........

[1]PHP记录搜索引擎来路、关键字的示例代码
    来源: 互联网  发布时间: 2013-12-24

本节内容:
PHP记录搜索引擎来路、关键字

例子:
 

代码示例:

<?php
/*
 * @ 记录搜索引擎来路、关键字
 * @ edit: www.
 /*
function keywords($url){
   $spier=array('baidu.'=>'百度','google.'=>'谷歌','soso.'=>'搜搜','sogou.'=>'搜狗','www.so.com'=>'360');
   $q=array('百度'=>'/wd=([^&]*)/i','谷歌'=>'/q=([^&]*)/i','360'=>'/q=(.*)/i','搜狗'=>'/query=([^&]*)/i','搜搜'=>'/w=([^&]*)/i');
   foreach($spier as $k=>$v){
  if(strpos($url,$k)){
            preg_match("{$q[$v]}",$url,$b);
        if($v=='搜搜'||$v=='搜狗'){
      $keywords=iconv('GBK','UTF-8',urldecode($b[1]));
    }else{
   $keywords=urldecode($b[1]);
  }
 
  echo "来自{$v}的关键字:".$keywords;
  }
 } 
}

//调用示例
$url=$_SERVER['HTTP_REFERER']; //放到网站的头部来获取来访地址
keywords($url);


    
[2]php socket使用smtp发送带附件的邮件
    来源: 互联网  发布时间: 2013-12-24

本节内容:
php socket smtp发送带附件的邮件

例子:
 

代码示例:

<?php
/**
* php socket smtp发送邮件
* edit: www.
*/

//define("SOL", "\n");
define("EOL", "\r\n");
define("SMTP_HOST", "smtp.163.com");//SMTP服务器
define("SMTP_PORT", "25");//SMTP服务器端口
define("SMTP_USER", "");//SMTP服务器的用户帐号
define("SMTP_PASS", "");//SMTP服务器的用户密码

$from = "";//SMTP服务器的用户邮箱
$to = "";//发送给谁 可用逗号隔开多个邮箱
$cc = "";
$bcc = "";

$subject="这是一个由PHP发送的带附件的邮件";//邮件主题 很多客户端会有乱码,所以转一下码
$body = "这个是一个带附件的邮件发送程序<hr>看到没,这里显示了HTM标签哦;<a href='http://www./'>请点开链接</a><input type='button' id='aaa' name=\"aaa\"> ".date('Y-m-d H:i:s');//邮件内容
$smtp = new smtp(SMTP_HOST,SMTP_PORT,SMTP_USER,SMTP_PASS, true);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->addAttachment("mail.zip");
$smtp->sendmail($to, $from, $subject, $body, $cc, $bcc);

class smtp {

 /* Public Variables */
 public $attachments = array();
 /* Private Variables */
 private $smtp_host;
 private $smtp_port;
 private $time_out;
 private $host_name;
 private $auth;
 private $user;
 private $pass;
 private $sock;

 /* Constractor */
 public function smtp($smtp_host = null, $smtp_port = null, $user = null, $pass = null, $auth = true) {
  $this->smtp_host = (!empty($smtp_host)) ? $smtp_host : SMTP_HOST;
  $this->smtp_port = (!empty($smtp_port)) ? $smtp_port : SMTP_PORT;
  $this->user = (!empty($user)) ? $user : SMTP_PORT;
  $this->pass = (!empty($pass)) ? $pass : SMTP_PORT;
  $this->auth = $auth;
  $this->time_out = 15;
  #
  $this->host_name = "localhost";
  $this->sock = FALSE;
 }

 /* Main Function */
 public function sendmail($to, $from, $subject = "", $body = "", $cc = "", $bcc = "") {
  $bndp = md5(uniqid("")) . rand(1000, 9999);
  $bnd  = md5(uniqid("")) . rand(1000, 9999);
  list ($msec, $sec) = explode()(" ", microtime());

  $mail_from = $this->strip_line_breaks($from);
  $mail_to = explode(",", $to);
  $body = preg_replace("/(^|(\r\n))(\\.)/", "", $body);
  if ($cc != "") $mail_to = array_merge($mail_to, explode(",", $cc));
  if ($bcc != "") $mail_to = array_merge($mail_to, explode(",", $bcc));

  $headers  = "MIME-Version:1.0" . EOL;
  $headers .= "To: " . $to . EOL;
  if ($cc != "") {
  $headers .= "Cc: " . $cc . EOL;
  }
  $headers .= "From: $from<" . $from . ">" . EOL;
  $headers .= "Subject: " . $subject . EOL;
  $headers .= "Date: " . date("r") . EOL;
  $headers .= "X-Mailer: Webmail ver 1.0 (PHP Version/" . phpversion() . ")" . EOL;
  $headers .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $from . ">" . EOL;
  if (count($this->attachments) > 0) {
   $headers .= "Content-Type: multipart/mixed;" . EOL . chr(9) . " boundary=\"" . $bndp . "\"" . EOL . EOL;
   $headers .= '--'.$bndp . EOL;
   $headers .= 'Content-Type : multipart/alternative; boundary="' . $bnd . '"' . EOL . EOL;
   $headers .= '--' . $bnd . EOL;
   $headers .= 'Content-Type: text/plain; charset=utf-8' . EOL;
   $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
   $headers .= $body . EOL;
   $headers .= '--' . $bnd . EOL;
   $headers .= 'Content-type: text/html; charset=utf-8' . EOL;
   $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
   $headers .= $body . EOL;
   $headers .= '--' . $bnd . '--' . EOL;

   foreach ($this->attachments as $att) {
    $headers .= "--" . $bndp . EOL . $att;
   }
   $headers .= '--' . $bndp . '--' . EOL;
   $this->clear_attachments();
  } else {
   $headers .= 'Content-Type : multipart/alternative;boundary="'.$bnd.'"' . EOL . EOL;
   $headers .= '--'.$bnd . EOL;
   $headers .= 'Content-Type: text/plain; charset=utf-8' . EOL;
   $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
   $headers .= $body . EOL;
   $headers .= '--'.$bnd . EOL;
   $headers .= 'Content-type: text/html; charset=utf-8' . EOL;
   $headers .= "Content-Transfer-Encoding: 8bit" . EOL . EOL;
   $headers .= $body . EOL;
   $headers .= '--'.$bnd.'--' . EOL;
  }

  $sent = TRUE;
  foreach ($mail_to as $rcpt_to) {
   $rcpt_to = $this->strip_line_breaks($rcpt_to);
   if (!$this->smtp_sockopen($rcpt_to)) {
    $this->log_write("Error: Cannot send email to " . $rcpt_to);
    $sent = FALSE;
    continue;
   }
   if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $headers, $body)) {
    $this->log_write("E-mail has been sent to <" . $rcpt_to . ">");
   } else {
    $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">");
    $sent = FALSE;
   }
   fclose($this->sock);
  }
  $this->log_write("{$mail_to} send over;");
  return $sent;
 }

 public function addAttachment($file, $dispo = "attachment") {
  $file_data = (file_exists($file)) ? file_get_contents($file) : "";
  if ($file_data != "") {
   $filename = basename($file);
   $ext = pathinfo($filename, PATHINFO_EXTENSION);
   $chunks = chunk_split()(base64_encode($file_data));
   $parts  = "Content-Type: application/$ext; name=\"" . $filename . "\"" . EOL;
   $parts .= "Content-Transfer-Encoding: base64" . EOL;
   $parts .= "Content-Disposition: " . $dispo . "; filename=\"" . $filename . "\"" . EOL . EOL;
   $parts .= $chunks . EOL . EOL;
   $this->attachments[] = $parts;
  }
 }

 private function clear_attachments() {
  unset($this->attachments);
  $this->attachments = array();
 }

 /* Private Functions */
 private function smtp_send($helo, $from, $to, $header, $body = "") {
  if (!$this->smtp_putcmd("HELO", $helo)) {
   //$this->log_write("Error: Error occurred while sending HELO command.");
   return FALSE;
  }
  #auth
  if ($this->auth) {
   if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
    //$this->log_write("Error: Error occurred while sending HELO command.");
    return FALSE;
   }
   if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
    //$this->log_write("Error: Error occurred while sending HELO command.");
    return FALSE;
   }
  }
  if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) {
   //$this->log_write("Error: Error occurred while sending MAIL FROM command.");
   return FALSE;
  }
  if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) {
   //$this->log_write("Error: Error occurred while sending RCPT TO command.");
   return FALSE;
  }
  if (!$this->smtp_putcmd("DATA")) {
   //$this->log_write("Error: Error occurred while sending DATA command.");
   return FALSE;
  }
  if (!$this->smtp_message($header, $body)) {
   //$this->log_write("Error: Error occurred while sending message.");
   return FALSE;
  }
  if (!$this->smtp_eom()) {
   //$this->log_write("Error: Error occurred while sending <CR><LF>.<CR><LF> [EOM].");
   return FALSE;
  }
  if (!$this->smtp_putcmd("QUIT")) {
   //$this->log_write("Error: Error occurred while sending QUIT command.");
   return FALSE;
  }
  return TRUE;
 }
 private function smtp_sockopen($address) {
  if ($this->smtp_host == "") {
   return $this->smtp_sockopen_mx($address);
  } else { // www.
   return $this->smtp_sockopen_relay();
  }
 }
 private function smtp_sockopen_relay() {
  $this->log_write("Trying to Connect " . $this->smtp_host . ":" . $this->smtp_port . "...");
  $this->sock = @fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  if (!($this->sock && $this->smtp_ok())) {
   $this->log_write("Error: connenct error" . $errstr . " (" . $errno . ")");
   return FALSE;
  }
  $this->log_write("Connected Ok");
  return TRUE;
 }
 private function smtp_sockopen_mx($address) {
  $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address);
  if (!@getmxrr($domain, $MXHOSTS)) {
   $this->log_write("Error: Cannot resolve MX \"" . $domain . "\"");
   return FALSE;
  }
  foreach ($MXHOSTS as $host) {
   $this->log_write("Trying to " . $host . ":" . $this->smtp_port);
   $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
   if (!($this->sock && $this->smtp_ok())) {
    $this->log_write("Connect Error ," . $errstr . " (" . $errno . ")");
    continue;
   }
   $this->log_write("Connected to mx host " . $host);
   return TRUE;
  }
  $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")");
  return FALSE;
 }
 private function smtp_message($header, $body) {
  fputs($this->sock, $header . "\r\n" . $body);
  return TRUE;
 }
 private function smtp_eom() {
  fputs($this->sock, "\r\n.\r\n");
  return $this->smtp_ok();
 }
 private function smtp_ok() {
  $response = str_replace()("\r\n", "", fgets($this->sock, 512));
  if (!preg_match("/^[23]/", $response)) {
   fputs($this->sock, "QUIT\r\n");
   fgets($this->sock, 512);
   $this->log_write("Error: Remote host returned \"" . $response . "\"");
   return FALSE;
  }
  return TRUE;
 }
 private function smtp_putcmd($cmd, $arg = "") {
  if ($arg != "") $cmd = ($cmd == "") ? $arg : ($cmd . " " . $arg);
  fputs($this->sock, $cmd . "\r\n");
  return $this->smtp_ok();
 }
 private function strip_line_breaks($address) {
  $address = preg_replace("/([\t\r\n])+/", "", $address);
  $address = preg_replace("/^.*<(.+)>.*$/", "", $address);
  return $address;
 }
 public function log_write($message) {
  $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message;
  file_put_contents(dirname(__FILE__) . '/mail.log', $message . PHP_EOL, FILE_APPEND | LOCK_EX);
 }
}


    
[3]php计算指定文件夹信息(文件夹数,文件数,文件夹大小)
    来源: 互联网  发布时间: 2013-12-24

本节内容:
统计文件夹信息的php代码。

此代码,实现如下功能:
计算文件夹的大小,包括子文件夹,格式化输出文件夹大小、文件数、子文件夹数信息。

例子:
 

代码示例:
<?
//格式化输出目录大小 单位:Bytes,KB,MB,GB
//也可以用于统计目录数
//site www.
function getDirectorySize($path)
{
  $totalsize = 0;
  $totalcount = 0;
  $dircount = 0;
  if ($handle = opendir ($path))
  {
    while (false !== ($file = readdir($handle)))
    {
      $nextpath = $path . '/' . $file;
      if ($file != '.' && $file != '..' && !is_link ($nextpath))
      {
        if (is_dir ($nextpath))
        {
          $dircount++;
          $result = getDirectorySize($nextpath);
          $totalsize += $result['size'];
          $totalcount += $result['count'];
          $dircount += $result['dircount'];
        }
        elseif (is_file ($nextpath))
        {
          $totalsize += filesize ($nextpath);
          $totalcount++;
        }
      }
    }
  }
  closedir ($handle);
  $total['size'] = $totalsize;
  $total['count'] = $totalcount;
  $total['dircount'] = $dircount;
  return $total;
}
 
function sizeFormat($size)
{
    $sizeStr='';
    if($size<1024)
    {
        return $size." bytes";
    }
    else if($size<(1024*1024))
    {
        $size=round($size/1024,1);
        return $size." KB";
    }
    else if($size<(1024*1024*1024))
    {
        $size=round($size/(1024*1024),1);
        return $size." MB";
    }
    else
    {
        $size=round($size/(1024*1024*1024),1);
        return $size." GB";
    }
 
}
 
$path="/home/www/htdocs";
$ar=getDirectorySize($path);
 
echo "<h4>路径 : $path</h4>";
echo "目录大小 : ".sizeFormat($ar['size'])."<br>";
echo "文件数 : ".$ar['count']."<br>";
echo "目录术 : ".$ar['dircount']."<br>";
 
//print_r($ar);
?>

    
最新技术文章:
▪PHP函数microtime()时间戳的定义与用法
▪PHP单一入口之apache配置内容
▪PHP数组排序方法总结(收藏)
▪php数组排序方法大全(脚本学堂整理奉献)
▪php数组排序的几个函数(附实例)
▪php二维数组排序(实例)
▪php根据键值对二维数组排序的小例子
▪php验证码(附截图)
▪php数组长度的获取方法(三个实例)
▪php获取数组长度的方法举例
▪判断php数组维度(php数组长度)的方法
▪php获取图片的exif信息的示例代码
▪PHP 数组key长度对性能的影响实例分析
▪php函数指定默认值的方法示例
▪php提交表单到当前页面、提交表单后页面重定...
▪php四舍五入的三种实现方法
▪php获得数组长度(元素个数)的方法
▪php日期函数的简单示例代码
▪php数学函数的简单示例代码
▪php字符串函数的简单示例代码
▪php文件下载代码(多浏览器兼容、支持中文文...
▪php实现文件下载、支持中文文件名的示例代码...
▪php文件下载(防止中文文件名乱码)的示例代码
▪解决PHP文件下载时中文文件名乱码的问题
▪php数组去重(一维、二维数组去重)的简单示例
▪php小数点后取两位的三种实现方法
▪php Redis 队列服务的简单示例
▪PHP导出excel时数字变为科学计数的解决方法
▪PHP数组根据值获取Key的简单示例
▪php数组去重的函数代码示例
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3