今天在做项目想用局域网的IP连接本地MySQL的时候,总是提示以下的信息:
ERROR 1045 (28000): Access denied for user 'root'@'fujiayl-PC' (using password:NO)
搞了半天,终于找到解决方法:用phpMyAdmin添加多以下一条记录就可以了
2012年5月29日星期二
2012年5月26日星期六
PHP XML 的 DOMDocument 读取功能
1、XML常识知识补充
XML(eXtensible Markup Language)即可扩展标记语言,它与HTML一样,都是属于SGML标准通用标记语言。XML是Internet环境中跨平台的,依赖于内容的技术,是独立于软件和硬件的信息传输工具。不同的应用程序都能够访问您的数据,不仅仅在 HTML 页中,也可以从 XML 数据源中进行访问。
XML与HTML的设计区别是:XML 被设计为传输和存储数据,其焦点是数据的内容, 旨在传输信息 。HTML 被设计用来显示数据,其焦点是数据的外观,旨在显示信息。
更多关于XML的资源访问: http://www.w3school.com.cn/x.asp
2、什么是DOM、什么是PHP DOM XML解析
DOM (Document Object Model,文档对象模型)定义了访问和操作文档的标准方法。使用DOM,开发人员可以创建XML或HTML文档,操作它们的结果,增加、修改和删除文档 元素及内容。可以从任何编程语言访问DOM,本文使用PHP 5 DOM扩展,它是PHP核心的一部分,因此除了PHP外,不需要安装其它软件。
根据 DOM,XML 文档中的每个成分都是一个节点。
DOM 是这样规定的:
DOM 是这样规定的:
- 整个文档是一个文档节点
- 每个 XML 标签是一个元素节点
- 包含在 XML 元素中的文本是文本节点
- 每一个 XML 属性是一个属性节点
- 注释属于注释节点
DOM树节点在PHP中的实现方法如下所示:
- Document节点 -- 表示DOMDocument接口
- Element节点 -- 表示DOMElement接口
- Attribute节点 -- 表示DOMAttr接口
- Comment节点 -- 表示DOMComment接口
- Text节点 -- 表示DOMText接口
3、使用PHP DOM 读取一个XML
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book>
<title>PHP100中文网</title>
</book>
</bookstore>
============php==============
$doc = new DOMDocument();
$doc->load('book.xml');$books = $doc->getElementsByTagName( "book" );
$title = $doc->getElementsByTagName( “title" );
echo $title >item(0)->nodeValue;
PHP通过Socket发送邮件
本文主要探讨的是发送邮件底层的技术原理,然后通过PHP的Socket来发送邮件。
首先你有必要先了解以下相关协议
在PHP中发送邮件是很简单的,可以通过系统自带的mail()函数直接发送,但是用mail()函数需要类似sendmail这样的组件支持,即系统要在php.ini配置SMTP服务器的相关信息才能发送成功,设置起来有点麻烦,移植性比较差。
发送邮件主要是通过SMTP协议来传输的,SMTP是 TCP/IP 协议的一种实现,负责把邮件发送到另一台计算机。
我们知道,计算机之间的通信都是基于TCP/IP协议的。我们平时用浏览器浏览网页时,也是使用 TCP/IP 来连接因特网的。 浏览器使用 TCP/IP 来访问因特网服务器,服务器使用 TCP/IP 向浏览器传回 HTML。同样,电子邮件程序必须使用 TCP/IP 来连接因特网,这样才能收发邮件。
即PHP只是充当一个客户端的角色,你用mail()函数发送邮件,只是相当于向SMTP服务器发送了一个命令,具体的发送过程还是要靠SMTP服务器来实现。
如果你没有自己的SMTP服务器,要想发送邮件,就必须用第三方的SMTP服务器,这时,mail()函数根本就没有用了,我们必须通过socket方式来发送邮件。
resource fsockopen ( string target [, int port [, int &errno [, string &errstr [, float timeout]]]] )
这里由于要使用SMTP协议,所以端口号为25。在打开连接成功后,会返回一个socket句柄,使用它就可以象使用文件句柄一样的。可使用的操作有fputs(),fgets(),feof(),fclose()。
在socket连接过程中,会返回以下相关信息。
220 服务就绪(在socket连接成功时,会返回此信息)
221 正在处理
250 请求邮件动作正确,
354 开始发送数据,结束以 .
500 语法错误,命令不能识别
550 命令不能执行,邮箱无效
552 中断处理:用户超出文件空间
还好网上已经有大牛把这样的程序给写出来了。我们直接调用就行。以下是一个邮件类文件,功能很强悍,移植性好又易用。把它命名为 email.class.php
<?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" );
}
echo "<br>";
echo $header;
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 . "<br>";
}
}
function get_attach_type($image_tag) { //
$filedata = array ();
$img_file_con = fopen ( $image_tag, "r" );
unset ( $image_data );
while ( $tem_buffer = AddSlashes ( fread ( $img_file_con, filesize ( $image_tag ) ) ) )
$image_data .= $tem_buffer;
fclose ( $img_file_con );
$filedata ['context'] = $image_data;
$filedata ['filename'] = basename ( $image_tag );
$extension = substr ( $image_tag, strrpos ( $image_tag, "." ), strlen ( $image_tag ) - strrpos ( $image_tag, "." ) );
switch ($extension) {
case ".gif" :
$filedata ['type'] = "image/gif";
break;
case ".gz" :
$filedata ['type'] = "application/x-gzip";
break;
case ".htm" :
$filedata ['type'] = "text/html";
break;
case ".html" :
$filedata ['type'] = "text/html";
break;
case ".jpg" :
$filedata ['type'] = "image/jpeg";
break;
case ".tar" :
$filedata ['type'] = "application/x-tar";
break;
case ".txt" :
$filedata ['type'] = "text/plain";
break;
case ".zip" :
$filedata ['type'] = "application/zip";
break;
default :
$filedata ['type'] = "application/octet-stream";
break;
}
return $filedata;
}
}
?>
例子说明
你可以到163申请一个邮箱,把下面的相关配置改一下即可运行。
<?php
require_once ('email.class.php');
$smtpserver = "smtp.163.com";//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = "";//SMTP服务器的用户邮箱
$smtpemailto = "";//发送给谁
$smtpuser = "";//SMTP服务器的用户帐号
$smtppass = "";//SMTP服务器的用户密码
$mailsubject = "PHP100测试邮件系统";//邮件主题
$mailbody = "<h1> 这是一个测试程序 </h1>";//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
?>
首先你有必要先了解以下相关协议
在PHP中发送邮件是很简单的,可以通过系统自带的mail()函数直接发送,但是用mail()函数需要类似sendmail这样的组件支持,即系统要在php.ini配置SMTP服务器的相关信息才能发送成功,设置起来有点麻烦,移植性比较差。
php.ini中SMTP的配置 |
发送邮件主要是通过SMTP协议来传输的,SMTP是 TCP/IP 协议的一种实现,负责把邮件发送到另一台计算机。
我们知道,计算机之间的通信都是基于TCP/IP协议的。我们平时用浏览器浏览网页时,也是使用 TCP/IP 来连接因特网的。 浏览器使用 TCP/IP 来访问因特网服务器,服务器使用 TCP/IP 向浏览器传回 HTML。同样,电子邮件程序必须使用 TCP/IP 来连接因特网,这样才能收发邮件。
即PHP只是充当一个客户端的角色,你用mail()函数发送邮件,只是相当于向SMTP服务器发送了一个命令,具体的发送过程还是要靠SMTP服务器来实现。
如果你没有自己的SMTP服务器,要想发送邮件,就必须用第三方的SMTP服务器,这时,mail()函数根本就没有用了,我们必须通过socket方式来发送邮件。
通过socket方式发送原理
使用fsockopen函数打开一个Internet连接
在socket连接过程中,会返回以下相关信息。
221 正在处理
250 请求邮件动作正确,
354 开始发送数据,结束以 .
500 语法错误,命令不能识别
550 命令不能执行,邮箱无效
552 中断处理:用户超出文件空间
<?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" );
}
echo "<br>";
echo $header;
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 . "<br>";
}
}
function get_attach_type($image_tag) { //
$filedata = array ();
$img_file_con = fopen ( $image_tag, "r" );
unset ( $image_data );
while ( $tem_buffer = AddSlashes ( fread ( $img_file_con, filesize ( $image_tag ) ) ) )
$image_data .= $tem_buffer;
fclose ( $img_file_con );
$filedata ['context'] = $image_data;
$filedata ['filename'] = basename ( $image_tag );
$extension = substr ( $image_tag, strrpos ( $image_tag, "." ), strlen ( $image_tag ) - strrpos ( $image_tag, "." ) );
switch ($extension) {
case ".gif" :
$filedata ['type'] = "image/gif";
break;
case ".gz" :
$filedata ['type'] = "application/x-gzip";
break;
case ".htm" :
$filedata ['type'] = "text/html";
break;
case ".html" :
$filedata ['type'] = "text/html";
break;
case ".jpg" :
$filedata ['type'] = "image/jpeg";
break;
case ".tar" :
$filedata ['type'] = "application/x-tar";
break;
case ".txt" :
$filedata ['type'] = "text/plain";
break;
case ".zip" :
$filedata ['type'] = "application/zip";
break;
default :
$filedata ['type'] = "application/octet-stream";
break;
}
return $filedata;
}
}
?>
你可以到163申请一个邮箱,把下面的相关配置改一下即可运行。
<?php
require_once ('email.class.php');
$smtpserver = "smtp.163.com";//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = "";//SMTP服务器的用户邮箱
$smtpemailto = "";//发送给谁
$smtpuser = "";//SMTP服务器的用户帐号
$smtppass = "";//SMTP服务器的用户密码
$mailsubject = "PHP100测试邮件系统";//邮件主题
$mailbody = "<h1> 这是一个测试程序 </h1>";//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
?>
Apache Rewrite 实现URL伪静态
一、简述
Rewirte主要的功能就是实现URL的跳转。之所以要把URL伪静态化,是因为有利于SEO(搜索引擎的优化),有利于搜索引擎的抓取,提交网站的排名。
Apache Rewrite的实现有2种方式,一种是基于服务器级的(httpd.conf),另外一种是基于目录级的(.htaccess)。
二、配置
无论基于哪种方式,要想实现URL重写,都必须开启Apache的mod_rewrite.so模块。
打开Apache目录下的配置文件 httpd.conf,找到下面这一行
#LoadModule rewrite_module modules/mod_rewrite.so
把前面的#号去掉。
1.基于httpd.conf
如果你设置了虚拟主机,可以按照下面的例子添加相应的 IfModule 片段,当然URL重写规则你要自己写。最后记得重启Apache。
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.hleclerc-PC.ingenidev
DocumentRoot "D:/wamp/bin/apache/Apache2.2.17/htdocs/blogger"
ErrorLog "logs/blogger.com-error.log"
CustomLog "logs/blogger.com-access.log" common
ServerName blogger.com
<Directory "D:/wamp/bin/apache/Apache2.2.17/htdocs/blogger">
Options Indexes FollowSymLinks
AllowOverride all
Order deny,allow
allow from all
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine On
# Blog
RewriteRule ^/blog_([0-9]+)_home\.html$ /blog/index.php?blogid=$1 [L]
RewriteRule ^/blog_([0-9]+)_list\.html$ /blog/blog.php?blogid=$1 [L]
RewriteRule ^/blog_([0-9]+)_([0-9]+)_list\.html$ /blog/blog.php?blogid=$1&page=$2 [L]
RewriteRule ^/blog_show_([0-9]+)\.html$ /blog/blog_show.php?id=$1 [L]
RewriteRule ^/blog_([0-9]+)_photo\.html$ /blog/photo.php?blogid=$1 [L]
RewriteRule ^/blog_([0-9]+)_([0-9]+)_photo\.html$ /blog/photo.php?blogid=$1&page=$2 [L]
RewriteRule ^/blog_albums_([0-9]+)\.html$ /blog/photo_albums_show.php?id=$1 [L]
RewriteRule ^/blog_p_([0-9]+)\.html$ /blog/photo_show.php?id=$1 [L]
RewriteRule ^/blog_([0-9]+)_myfriends\.html$ /blog/myfriends.php?blogid=$1 [L]
RewriteRule ^/blog_([0-9]+)_([0-9]+)_myfriends\.html$ /blog/myfriends.php?blogid=$1&page=$2 [L]
RewriteRule ^/blog_([0-9]+)_messages\.html$ /blog/messages.php?blogid=$1 [L]
RewriteRule ^/blog_([0-9]+)_([0-9]+)_messages\.html$ /blog/messages.php?blogid=$1&page=$2 [L]
RewriteRule ^/blog_([0-9]+)_b_([0-9]+)\.html$ /blog/messages.php?blogid=$1 [L]
</IfModule>
</VirtualHost>
2.基于.htaccess
(1)首先要在你网站的根目录下,建立一个 .htaccess 命名的文件。
(2)确认你Apache的配置是否已经支持了 .htaccess 的重写
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.hleclerc-PC.ingenidev
DocumentRoot "D:/wamp/bin/apache/Apache2.2.17/htdocs/blogger"
ErrorLog "logs/
blogger.com-error.log"
CustomLog "logs/
blogger.com-access.log" common
ServerName
blogger.com
<Directory "D:/wamp/bin/apache/Apache2.2.17/htdocs/blogger">
Options Indexes FollowSymLinks #下面4行代码就是是否启用.htaccess
AllowOverride all
Order deny,allow
allow from all
</Directory>
</VirtualHost>
(3)最后记得重启Apache
(4).htaccess 文件里面的内容跟 httpd.conf 文件里的内容稍微有点不同,以上面的代码为例子,主要就是每个 URL 前面都少了一个 / 。 .htaccess 方式有一个好处就是每次修改的 URL 规则如果要生效,不必重启 Apache。
(4).htaccess 文件里面的内容跟 httpd.conf 文件里的内容稍微有点不同,以上面的代码为例子,主要就是每个 URL 前面都少了一个 / 。 .htaccess 方式有一个好处就是每次修改的 URL 规则如果要生效,不必重启 Apache。
RewriteEngine On
# Blog
RewriteRule ^blog_([0-9]+)_home\.html$ blog/index.php?blogid=$1 [L]
RewriteRule ^blog_([0-9]+)_list\.html$ blog/blog.php?blogid=$1 [L]
RewriteRule ^blog_([0-9]+)_([0-9]+)_list\.html$ blog/blog.php?blogid=$1&page=$2 [L]
RewriteRule ^blog_show_([0-9]+)\.html$ blog/blog_show.php?id=$1 [L]
三、有用的资源
关于上面 Rewirte 的各种语法你可以参考下面的几个链接,都有详细的介绍,通俗易懂,这里不做讲解。
PHP服务端伪Ajax请求
有时候,你可能有这样的需求,我们的A程序在处理客户端的请求时,想要以用户的身份给我们的B程序发送一个请求,但是又不想等待它的请求结果,有什么样的方法来解决这样的情景呢?
这时候,我们必须以socket的方式来建立一个链接,然后把我们的请求数据以Get或者Post的方式传递过去,而无须等待它的响应结果。这个类似于Ajax请求,不会阻塞当前A程序的执行。
下面就以Post方式的例子来讲解一下。
<?php
$tmp = '';
$cookie = '';
$postfield = array ('title' => $title, 'type' => 'help', 'select_circle_id' => $item->circle_id, 'act' => 'addshare', 'artcle_comment' => $content );
$tmp = http_build_query($postfield);
foreach ( $_COOKIE as $key => $value ) {
//如果想以用户的身份给B程序发送请求,必须传递Cookie过去,这样才能获取Session
$cookie .= "$key=".urlencode($value)."; ";
}
//去掉末尾的;号
$cookie = substr($cookie,0,-1);
//有一点必须明白,如果当前连接没有建立成功,程序会在这里阻塞的
$socket = fsockopen ( $_SERVER ['SERVER_NAME'], 80, $errno, $errstr, 30 );
//我们的B程序的URL地址
$request = "POST /personal/personal_center.php HTTP/1.1\r\n";
$request .= "Cookie: $cookie\r\n";//回车换行符必须在双引号里面
$request .= 'Host: ' . $_SERVER ['SERVER_NAME'] . "\r\n";
$request .= "Content-type: application/x-www-form-urlencoded\r\n";
$request .= "Content-length: " . strlen($tmp) . "\r\n";
$request .= "Accept: */*\r\n";
$request .= "Connection: Keep-Alive\r\n\r\n";//注意这里是2个回车换行符,标志着请求头的结束
$request .= "$tmp\r\n\r\n";//Post数据后也要有2个回车换行符
fwrite ( $socket, $request );
fclose ( $socket );
//OK,我们已经向B程序成功发送了一个请求,B程序会执行给定的任务。但我们不必理会,继续执行当前A程序
//A程序的任务……
?>
jQuery图像裁切插件
今天介绍一款jQuery
图像裁切插件。这款插件的应用很广泛,如在新浪微博或者QQ校友中,我们上传头像时,会有对图像进行裁切这个功能。
http://odyniec.net/projects/imgareaselect/
现在的难点是我们怎么在后台生成缩略图呢?
首先我们必须把上图的各个参数都传递过去,关于各个参数的意义就不用我多说了吧,一目了然。
下面我用PHP的程序来解释一下后台是怎么做处理的
<?php
//我们假设用户上传的头像是下面的文件路径
$image = $_SERVER ['DOCUMENT_ROOT'] . '/images/test.jpg';
$x1 = $_POST['x1'];//代表上图的x1坐标
$y1 = $_POST['y1'];//代表上图的y1坐标
$width = $_POST['width'];//代表上图的Width
$height = $_POST['height'];//代表上图的Height
//得到图片的相关信息
$img_info = getimagesize ( $image );
$image_type = $img_info ['mime'];
switch ($image_type) {
case 'image/gif' :
//从 GIF 文件或 URL 新建一图像,返回一图像标识符,代表了从给定的文件名取得的图像。
$source = imagecreatefromgif ( $image );
break;
case 'image/jpeg' :
$source = imagecreatefromjpeg ( $image );
break;
case 'image/png' :
$source = imagecreatefrompng ( $image );
break;
}
//等比缩放的基准,其中100是可以改变的,这里我们假设要保存的图像为100×100的像素
$scale = 100/$width;
//对新建的图像等比缩放
$newImageWidth = ceil ( $width * $scale );
$newImageHeight = ceil ( $height * $scale );
//新建一个真彩色图像,代表了一幅大小为 $newImageWidth 和 $newImageHeight 的黑色图像
$newImage = imagecreatetruecolor ( $newImageWidth, $newImageHeight );
//重采样拷贝部分图像并调整大小
imagecopyresampled ( $newImage, $source, 0, 0, $x1, $y1, $newImageWidth, $newImageHeight, $width, $height );
//设置保存新图片的路径
$img_path_info = pathinfo($image);
$thumbnail_path = $img_path_info['dirname'].'/'.time().'.'.$img_path_info['extension'];
switch ($image_type) {
case 'image/gif' :
//以 GIF 格式将图像输出到浏览器或文件
imagegif ( $newImage, $thumbnail_path );
break;
case 'image/jpeg' :
imagejpeg ( $newImage, $thumbnail_path, 90 );
break;
case 'image/png' :
imagepng ( $newImage, $thumbnail_path );
break;
}
?>
这样子,在原来的目录里会再生成一幅新的图像
为什么要裁切呢?主要是因为用户上传的头像图片可能会很大,每次打开有头像的页面加载会很慢,所以有必要对用户上传的图片进行裁切,在后台生成容量较小的缩略图。
jQuery imgareaselect 是一款图像裁切专用的选区插件,它本身并没有图像裁切功能,只是生成一个类似选区的虚线框,拖动四角可以调整选区大小,可以拖动整个选区移动,高亮显示被选择的图像区域,用法有很多种。你可以到一下官方网站看演示或者下载下来,到自己的电脑上运行。
http://odyniec.net/projects/imgareaselect/
现在的难点是我们怎么在后台生成缩略图呢?
首先我们必须把上图的各个参数都传递过去,关于各个参数的意义就不用我多说了吧,一目了然。
下面我用PHP的程序来解释一下后台是怎么做处理的
<?php
//我们假设用户上传的头像是下面的文件路径
$image = $_SERVER ['DOCUMENT_ROOT'] . '/images/test.jpg';
$x1 = $_POST['x1'];//代表上图的x1坐标
$y1 = $_POST['y1'];//代表上图的y1坐标
$width = $_POST['width'];//代表上图的Width
$height = $_POST['height'];//代表上图的Height
//得到图片的相关信息
$img_info = getimagesize ( $image );
$image_type = $img_info ['mime'];
switch ($image_type) {
case 'image/gif' :
//从 GIF 文件或 URL 新建一图像,返回一图像标识符,代表了从给定的文件名取得的图像。
$source = imagecreatefromgif ( $image );
break;
case 'image/jpeg' :
$source = imagecreatefromjpeg ( $image );
break;
case 'image/png' :
$source = imagecreatefrompng ( $image );
break;
}
//等比缩放的基准,其中100是可以改变的,这里我们假设要保存的图像为100×100的像素
$scale = 100/$width;
//对新建的图像等比缩放
$newImageWidth = ceil ( $width * $scale );
$newImageHeight = ceil ( $height * $scale );
//新建一个真彩色图像,代表了一幅大小为 $newImageWidth 和 $newImageHeight 的黑色图像
$newImage = imagecreatetruecolor ( $newImageWidth, $newImageHeight );
//重采样拷贝部分图像并调整大小
imagecopyresampled ( $newImage, $source, 0, 0, $x1, $y1, $newImageWidth, $newImageHeight, $width, $height );
//设置保存新图片的路径
$img_path_info = pathinfo($image);
$thumbnail_path = $img_path_info['dirname'].'/'.time().'.'.$img_path_info['extension'];
switch ($image_type) {
case 'image/gif' :
//以 GIF 格式将图像输出到浏览器或文件
imagegif ( $newImage, $thumbnail_path );
break;
case 'image/jpeg' :
imagejpeg ( $newImage, $thumbnail_path, 90 );
break;
case 'image/png' :
imagepng ( $newImage, $thumbnail_path );
break;
}
?>
PHP超强分页类
分页这个功能一直在WEB应用中是经常遇到的,在服务器端处理不是什么大问题,麻烦的是在页面中处理是一件麻烦的事。有什么便捷的方法没有呢?今天就分享一个很强悍的分页类。
点击这里下载 page.class.php 源代码
用法:
<?php
require_once 'page.class.php';
$page = new page(array('total'=>1000,'perpage'=>20));//total 表示中的记录条数;perpage每一页的数目
echo $page->show();
?>
上面的例子将会产生一个类似图下面的页码,当然,样式需要你自己调一下。默认的url是当前页面地址,如:http://localhost/test.php?page=2 。page 参数是页数,当然 url 的地址是可以改变的,只需在上面的构造函数传给 url 参数进去就可以了。
共有4中分页模式
echo 'mode:1<br>'.$page->show();
echo '<hr>mode:2<br>'.$page->show(2);
echo '<hr>mode:3<br>'.$page->show(3);
echo '<hr>mode:4<br>'.$page->show(4);
第一种模式相关的CSS
.showpage{float:right; line-height:25px; height:25px; text-align:center; color:#999999; padding:5px 0;}
.showpage a{ cursor:pointer;}
.showpage span.first_page{ float:left; width:30px; margin-right:5px; border:1px solid #ccc;}
.showpage a.first_page{ float:left; width:30px; margin-right:5px; border:1px solid #ccc;}
.showpage a.first_page:hover{ text-decoration:none;}
.showpage span.pre_page{ float:left; width:50px; margin-right:5px; border:1px solid #ccc;}
.showpage a.pre_page{ float:left; width:50px; margin-right:5px; border:1px solid #ccc;}
.showpage a.pre_page:hover{ float:left; width:50px; margin-right:5px; border:1px solid #ccc;}
.showpage .go_page{float:left; width:20px; text-align:center; border:1px solid #ccc; margin-right:5px;}
.showpage a.go_page:hover{ background:#ccc; text-decoration:none;}
.showpage span.go_page{ background:#ddd; color:#FF0000;}
.showpage .next_page{float:left; width:50px; margin-right:5px; border:1px solid #ccc;}
.showpage a.next_page:hover{ text-decoration:none;}
.showpage .last_page{float:left; width:30px; margin-right:5px; border:1px solid #ccc;}
.showpage a.last_page:hover{ text-decoration:none;}
.showpage .page_select{float:left; width:35px; margin-top:3px; height:20px;}
.showpage .page_ajax{float:left; width:35px; margin-top:3px; height:20px;}
.showpage .page_select_left{float:left; width:27px; line-height:27px;}
.showpage .page_select_right{float:left; width:27px; line-height:27px;}
支持ajax
<?php
require_once 'page.class.php';
require_once 'page.class.php';
$page = new page ( array ('total' => 8, 'perpage' => 6, 'ajax' => 'get_circle_on_disease' ) );
$page_html = $page->show ();
订阅:
博文 (Atom)