当前位置:  编程技术>php
本页文章导读:
    ▪HTTP_HOST和SERVER_NAME的区别      在开发站群软件时,用到了根据访问域名判断子站点的相关问题。 PHP获取当前域名有两个变量 HTTP_HOST 和 SERVER_NAME,想知道两者的区别以及哪个更加可靠。 本人经过亲自验证 + 查阅官方文档 .........
    ▪关于iconv('gbk', 'utf-8',$str)丢失字符的解决方法      问题:处理一个文本文件,发现内容有一个’  ‘(看上去是全角空格),然后用iconv('gbk', 'utf-8',$str)转换格式,只能得到这个空格之前的内容,之后的内容会丢失。 解决方法:首先ord找出这.........
    ▪php发送邮件(mail)的类      php发送邮件(mail)的类   代码如下: <?php class smtp { /* Public Variables */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; var $user; var $pass; /* Private Variables */.........

[1]HTTP_HOST和SERVER_NAME的区别
    来源: 互联网  发布时间: 2013-12-24

在开发站群软件时,用到了根据访问域名判断子站点的相关问题。
PHP获取当前域名有两个变量 HTTP_HOST 和 SERVER_NAME,想知道两者的区别以及哪个更加可靠。

本人经过亲自验证 + 查阅官方文档 + 官方BUG列表 + 官方邮件列表 + sitepoint + webmasterworld + google的总结:

相同点:
当满足以下三个条件时,两者会输出相同信息。
1. 服务器为80端口
2. apache的conf中ServerName设置正确
3. HTTP/1.1协议规范

不同点:
1. 通常情况:
_SERVER["HTTP_HOST"] 在HTTP/1.1协议规范下,会根据客户端的HTTP请求输出信息。
_SERVER["SERVER_NAME"] 默认情况下直接输出apache的配置文件httpd.conf中的ServerName值。

2. 当服务器为非80端口时:
_SERVER["HTTP_HOST"] 会输出端口号,例如:mimiz.cn:8080
_SERVER["SERVER_NAME"] 会直接输出ServerName值
因此在这种情况下,可以理解为:HTTP_HOST = SERVER_NAME : SERVER_PORT

3. 当配置文件httpd.conf中的ServerName与HTTP/1.0请求的域名不一致时:
httpd.conf配置如下:
 

代码如下:
<virtualhost *>
ServerName mimiz.cn
ServerAlias www.mimiz.cn
</virtualhost>
 

客户端访问域名www.mimiz.cn
_SERVER["HTTP_HOST"] 输出 www.mimiz.cn
_SERVER["SERVER_NAME"] 输出 mimiz.cn

因此,在实际程序中,应尽量使用_SERVER["HTTP_HOST"] 。


    
[2]关于iconv('gbk', 'utf-8',$str)丢失字符的解决方法
    来源: 互联网  发布时间: 2013-12-24

问题:处理一个文本文件,发现内容有一个’  ‘(看上去是全角空格),然后用iconv('gbk', 'utf-8',$str)转换格式,只能得到这个空格之前的内容,之后的内容会丢失。

解决方法:首先ord找出这个字符的值(163和117),再用str_replace()(chr(163). chr(117), ' ', $str)将这个特殊符号转为空格,就能解决问题。


    
[3]php发送邮件(mail)的类
    来源: 互联网  发布时间: 2013-12-24

php发送邮件(mail)的类
 

代码如下:

<?php
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;

/* Private Variables */
var $sock;

/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
#
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
#
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";

$this->sock = FALSE;
}

/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\\.)", "\\1.\\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($mailtype=="HTML"){
$header .= "Content-Type:text/html\r\n";
}
$header .= "To: ".$to."\r\n";
if ($cc != "") {
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: $from<".$from.">;\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explode()(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">;\r\n";
$TO = explode(",", $this->strip_comment($to));

if ($cc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}

if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}

$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("E-mail has been sent to <".$rcpt_to.">;\n");
} else {
$this->log_write("Error: Cannot send email to <".$rcpt_to.">;\n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
return $sent;
}

/* Private Functions */

        function smtp_send($helo, $from, $to, $header, $body = "")
        {
                if (!$this->smtp_putcmd("HELO", $helo)) {
                        return $this->smtp_error("sending HELO command");
                }
                #auth
                if($this->auth){
                        if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
                                return $this->smtp_error("sending HELO command");
                        }
              
                        if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
                                return $this->smtp_error("sending HELO command");
                        }
                }
                #
                if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">;")) {
                        return $this->smtp_error("sending MAIL FROM command");
                }
              
                if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">;")) {
                        return $this->smtp_error("sending RCPT TO command");
                }
              
                if (!$this->smtp_putcmd("DATA")) {
                        return $this->smtp_error("sending DATA command");
                }
              
                if (!$this->smtp_message($header, $body)) {
                        return $this->smtp_error("sending message");
                }
              
                if (!$this->smtp_eom()) {
                        return $this->smtp_error("sending <CR>;<LF>;.<CR>;<LF>; [EOM]");
                }
              
                if (!$this->smtp_putcmd("QUIT")) {
                        return $this->smtp_error("sending QUIT command");
                }
              
                return TRUE;
        }

        function smtp_sockopen($address)
        {
                if ($this->relay_host == "") {
                        return $this->smtp_sockopen_mx($address);
                } else {
                        return $this->smtp_sockopen_relay();
                }
        }
      
        function smtp_sockopen_relay()
        {
                $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
                $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
                if (!($this->sock && $this->smtp_ok())) {
                        $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
                        $this->log_write("Error: ".$errstr." (".$errno.")\n");
                        return FALSE;
                }
                $this->log_write("Connected to relay host ".$this->relay_host."\n");
                return TRUE;
        }

        function smtp_sockopen_mx($address)
        {
                $domain = ereg_replace("^.+@([^@]+)$", "\\1", $address);
                if (!@getmxrr($domain, $MXHOSTS)) {
                        $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
                        return FALSE;
                }
                foreach ($MXHOSTS as $host) {
                        $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
                        $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
                        if (!($this->sock && $this->smtp_ok())) {
                                $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
                                $this->log_write("Error: ".$errstr." (".$errno.")\n");
                                continue;
                        }
                        $this->log_write("Connected to mx host ".$host."\n");
                        return TRUE;
                }
                $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
                return FALSE;
        }

        function smtp_message($header, $body)
        {
                fputs($this->sock, $header."\r\n".$body);
                $this->smtp_debug(">; ".str_replace()("\r\n", "\n".">; ", $header."\n>; ".$body."\n>; "));
              
                return TRUE;
        }

        function smtp_eom()
        {
                fputs($this->sock, "\r\n.\r\n");
                $this->smtp_debug(". [EOM]\n");
              
                return $this->smtp_ok();
        }

        function smtp_ok()
        {
                $response = str_replace("\r\n", "", fgets($this->sock, 512));
                $this->smtp_debug($response."\n");
              
                if (!ereg("^[23]", $response)) {
                        fputs($this->sock, "QUIT\r\n");
                        fgets($this->sock, 512);
                        $this->log_write("Error: Remote host returned \"".$response."\"\n");
                        return FALSE;
                }
                return TRUE;
        }

        function smtp_putcmd($cmd, $arg = "")
        {
                if ($arg != "") {
                        if($cmd=="") $cmd = $arg;
                        else $cmd = $cmd." ".$arg;
                }
      
                fputs($this->sock, $cmd."\r\n");
                $this->smtp_debug(">; ".$cmd."\n");
      
                return $this->smtp_ok();
        }

        function smtp_error($string)
        {
                $this->log_write("Error: Error occurred while ".$string.".\n");
                return FALSE;
        }

        function log_write($message)
        {
                $this->smtp_debug($message);
      
                if ($this->log_file == "") {
                        return TRUE;
                }
      
                $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
                if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
                        $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
                        return FALSE;
                }
                flock($fp, LOCK_EX);
                fputs($fp, $message);
                fclose($fp);
              
                return TRUE;
        }

        function strip_comment($address)
        {
                $comment = "\\([^()]*\\)";
                while (ereg($comment, $address)) {
                        $address = ereg_replace($comment, "", $address);
                }
              
                return $address;
        }

        function get_address($address)
        {
                $address = ereg_replace("([ \t\r\n])+", "", $address);
                $address = ereg_replace("^.*<(.+)>;.*$", "\\1", $address);
              
                return $address;
        }
      
        function smtp_debug($message)
        {
                if ($this->debug) {
                        echo $message;
                }
        }
}

function sendmail($smtpserver,$smtpuser,$smtppass,$smtpemailto,$smtpusermail, $mailsubject, $mailbody){
        $smtp = new smtp($smtpserver,25,true,$smtpuser,$smtppass);
        //$smtp->debug = TRUE;
        $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, "HTML");      
}
//such as
//sendmail("smtp.126.com","test@126.com","password","1034555083@qq.com","test@126.com","title","body");
?>


    
最新技术文章:
▪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