PHP实现.cn多域名释放可注册监测并邮件提醒小功能

PHP实现.cn多域名释放监测并邮件提醒小功能
使用:宝塔或护卫神或虚拟主机环境(php5.5-7.3)使用
定期激发:宝塔护卫神定期访问网址任务,每天4.04-6.06,访问网址:本页网址?x=cha访问
场景:自己中意的.cn域名,一旦发现过期就邮件反馈(QQ邮箱+微信/QQ邮箱提醒功能)

index.php

<?php
//.cn域名定期监测是可否注册功能脚步
//使用:宝塔或护卫神或虚拟主机环境(php5.5-7.3)使用
//定期激发:宝塔护卫神定期访问网址任务,每天4.04-6.06,访问网址:本页网址?x=cha访问
//场景:自己中意的.cn域名,一旦发现过期就邮件反馈(QQ邮箱+微信/QQ邮箱提醒功能)
//作者:yujianyue 15058593138@.com 可接小功能定制
//修改以下自己关注的域名,仅支持.cn/com.cn结尾域名 一行一个
$tips = "
malide.cn 码(源码|二维码)立得,源码下载|二维码生成
echafen.cn E查分
11170.cn 
aiyaha.com
mabida.com 七年老站
saolide.cn 扫(二维码/防伪码)立得
11184.cn
lidehui.cn
96448.cn 10年老站
soulide.cn 搜立得
63636.cn
71315.cn
pccx.cn
yidati.cn 易答题
";
//接受邮件
$mailx = "********@qq.com";
//配置发信邮件
$JmailUser="admin@*****.net";		//JmailUser
$JmailPass="**************";		//JmailPassword
$JmailAdds="admin@*****.net";		//JmailAddress
$Jmailsmtp="smtp.qq.com";		//Jmailsmtp
$Jmailport="465";			//mailport 推荐465

//没有把握:不建议修改以下代码
$geshu = "50"; //批量查询限制个数:建议10别超20
$biao = date("YmdHis");
$whose = array("cn","com.cn");
function QSe($domain) { //cn域名whois查询
$wsr = "whois.cnnic.net.cn";
$fp = @fsockopen($wsr, 43, $eno, $str, 1) or die("Socket Error".$eno."@".$str);
 fputs($fp, $domain . "\r\n");
 $out = "";
 while(!feof($fp)){$out .= fgets($fp);}
 if(Trim($out) == "") { return "No results!";	}
 fclose($fp);
 return strtolower($out);
}

if($_GET["x"] == "cha"){ //文件对应网址+?x=cha访问
$dmz=join("|",$whose);
preg_match_all("/\w+\.($dmz)/", $tips, $madm);
$doli = array_unique($madm[0]); //去重再查
if(count($doli)<1){ exit("<h2>没有识别到($dmz)域名</h2>"); }
if(count($doli)>$geshu){ exit("<h2>只提供($geshu)以内的批量查询</h2>"); }
$ii = 0; $it = "";
foreach($doli as $getal){
echo "<br>$getal:";
$dnsc = dns_get_record($getal, DNS_NS);
if(count($dnsc)>0){ //只查没有NDS记录的降低之查whois频率
echo "有解析已注册".count($dnsc);
}else{
$CACHE = QSe($getal);
if(stristr($CACHE, "No match")){
//未注册:发邮件
echo "Nomatch未注册"; $ii++; $it = "<br>$getal 未注册";
}else{
//忽略:有结果即已注册
echo "有Whois已注册";
}
 sleep(1); //请务必确保查询间隔
}
}
if($ii>0){
//有未注册的 发送提醒
if($JmailUser == $mail || !file_exists("mail.php") ){
 echo "<h1>未发送</h1>邮件支持文件不存在或收发邮件相同!";
}else{
 $title = "提醒:{$ii}域名可注册!";
 $descs = "<h3>发送给自己的即时提醒邮件:请勿拦截!</h3>$it !";
 $descs .= "<p>查立得 - $biao </p>"; 
 echo "<h1>$title</h1>".$descs;
 include 'mail.php'; //邮件发送函数文件
 $mail = new MySendMail();
 $mail->setServer($Jmailsmtp, $JmailUser, $JmailPass, $Jmailport, true);
 $mail->setFrom($JmailUser); //设置发件人
 $mail->setReceiver($mailx); //设置收件人,多个收件人,调用多次
 $mail->setMail($title, $descs); //设置邮件主题、内容
 $mailoka = $mail->sendMail(); //发送
 echo "<h1>发 $mailoka 封</h1>$mailx";
}
}
}
?>

mail.php

<?php
/**
* 邮件发送类
* 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的ssl连接
* 需要的php扩展:sockets、Fileinfo和openssl。
* 编码格式是UTF-8,传输编码格式是base64
* @example
* $mail = new MySendMail();
* $mail->setServer("smtp@126.com", "XXXXX@126.com", "XXXXX"); //设置smtp服务器,普通连接方式
* $mail->setServer("smtp.gmail.com", "XXXXX@gmail.com", "XXXXX", 465, true); //设置smtp服务器,到服务器的SSL连接
* $mail->setFrom("XXXXX"); //设置发件人
* $mail->setReceiver("XXXXX"); //设置收件人,多个收件人,调用多次
* $mail->setCc("XXXX"); //设置抄送,多个抄送,调用多次
* $mail->setBcc("XXXXX"); //设置秘密抄送,多个秘密抄送,调用多次
* $mail->addAttachment("XXXX"); //添加附件,多个附件,调用多次
* $mail->setMail("test", "<b>test</b>"); //设置邮件主题、内容
* $mail->sendMail(); //发送
*/

class MySendMail {
    protected $_userName;
    protected $_password;
    protected $_sendServer;
    protected $_port;
    protected $_from;
    protected $_to = array();
    protected $_cc = array();
    protected $_bcc = array();
    protected $_subject;
    protected $_body;
    protected $_attachment = array();
    protected $_socket;
    protected $_isSecurity;
    protected $_errorMessage;
    public function setServer($server, $username="", $password="", $port=25, $isSecurity=false) {
        $this->_sendServer = $server;
        $this->_port = $port;
        $this->_isSecurity = $isSecurity;
        $this->_userName = empty($username) ? "" : base64_encode($username);
        $this->_password = empty($password) ? "" : base64_encode($password);
        return true;
    }
 
    public function setFrom($from) {
        $this->_from = $from;
        return true;
    }
    public function setReceiver($to) {
        $this->_to[] = $to;
        return true;
    }
    public function setCc($cc) {
        $this->_cc[] = $cc;
        return true;
    }
    public function setBcc($bcc) {
        $this->_bcc[] = $bcc;
        return true;
    }
    public function addAttachment($file) {
        if(!file_exists($file)) {
            $this->_errorMessage = "file " . $file . " does not exist.";
            return false;
        }
        $this->_attachment[] = $file;
        return true;
    }
    public function setMail($subject, $body) {
        $this->_subject = base64_encode($subject);
        $this->_body = base64_encode($body);
        return true;
    }
    public function sendMail() {
        $command = $this->getCommand();
 
        $this->_isSecurity ? $this->socketSecurity() : $this->socket();
 
        foreach ($command as $value) {
            $result = $this->_isSecurity ? $this->sendCommandSecurity($value[0], $value[1]) : $this->sendCommand($value[0], $value[1]);
            if($result) {
                continue;
            }
            else{
                return false;
            }
        }
        $this->_isSecurity ? $this->closeSecutity() : $this->close();
        return true;
    }
    public function error(){
        if(!isset($this->_errorMessage)) {
            $this->_errorMessage = "";
        }
        return $this->_errorMessage;
    }
    protected function getCommand() {
        $separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符
 
        $command = array(
                array("HELO sendmail\r\n", 250)
            );
        if(!empty($this->_userName)){
            $command[] = array("AUTH LOGIN\r\n", 334);
            $command[] = array($this->_userName . "\r\n", 334);
            $command[] = array($this->_password . "\r\n", 235);
        }
 
        //设置发件人
        $command[] = array("MAIL FROM: <" . $this->_from . ">\r\n", 250);
        $header = "FROM: <" . $this->_from . ">\r\n";
 
        //设置收件人
        if(!empty($this->_to)) {
            $count = count($this->_to);
            if($count == 1){
                $command[] = array("RCPT TO: <" . $this->_to[0] . ">\r\n", 250);
                $header .= "TO: <" . $this->_to[0] .">\r\n";
            }
            else{
                for($i=0; $i<$count; $i++){
                    $command[] = array("RCPT TO: <" . $this->_to[$i] . ">\r\n", 250);
                    if($i == 0){
                        $header .= "TO: <" . $this->_to[$i] .">";
                    }
                    elseif($i + 1 == $count){
                        $header .= ",<" . $this->_to[$i] .">\r\n";
                    }
                    else{
                        $header .= ",<" . $this->_to[$i] .">";
                    }
                }
            }
        }
 
        //设置抄送
        if(!empty($this->_cc)) {
            $count = count($this->_cc);
            if($count == 1){
                $command[] = array("RCPT TO: <" . $this->_cc[0] . ">\r\n", 250);
                $header .= "CC: <" . $this->_cc[0] .">\r\n";
            }
            else{
                for($i=0; $i<$count; $i++){
                    $command[] = array("RCPT TO: <" . $this->_cc[$i] . ">\r\n", 250);
                    if($i == 0){
                    $header .= "CC: <" . $this->_cc[$i] .">";
                    }
                    elseif($i + 1 == $count){
                        $header .= ",<" . $this->_cc[$i] .">\r\n";
                    }
                    else{
                        $header .= ",<" . $this->_cc[$i] .">";
                    }
                }
            }
        }
 
        //设置秘密抄送
        if(!empty($this->_bcc)) {
            $count = count($this->_bcc);
            if($count == 1) {
                $command[] = array("RCPT TO: <" . $this->_bcc[0] . ">\r\n", 250);
                $header .= "BCC: <" . $this->_bcc[0] .">\r\n";
            }
            else{
                for($i=0; $i<$count; $i++){
                    $command[] = array("RCPT TO: <" . $this->_bcc[$i] . ">\r\n", 250);
                    if($i == 0){
                    $header .= "BCC: <" . $this->_bcc[$i] .">";
                    }
                    elseif($i + 1 == $count){
                        $header .= ",<" . $this->_bcc[$i] .">\r\n";
                    }
                    else{
                        $header .= ",<" . $this->_bcc[$i] .">";
                    }
                }
            }
        }
 
        //主题
        $header .= "Subject: =?UTF-8?B?" . $this->_subject ."?=\r\n";
        if(isset($this->_attachment)) {
            //含有附件的邮件头需要声明成这个
            $header .= "Content-Type: multipart/mixed;\r\n";
        }
        elseif(false){
            //邮件体含有图片资源的,且包含的图片在邮件内部时声明成这个,如果是引用的远程图片,就不需要了
            $header .= "Content-Type: multipart/related;\r\n";
        }
        else{
            //html或者纯文本的邮件声明成这个
            $header .= "Content-Type: multipart/alternative;\r\n";
        }
 
        //邮件头分隔符
        $header .= "\t" . 'boundary="' . $separator . '"';
 
        $header .= "\r\nMIME-Version: 1.0\r\n";
 
        //这里开始是邮件的body部分,body部分分成几段发送
        $header .= "\r\n--" . $separator . "\r\n";
        $header .= "Content-Type:text/html; charset=utf-8\r\n";
        $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $header .= $this->_body . "\r\n";
        $header .= "--" . $separator . "\r\n";
 
        //加入附件
        if(!empty($this->_attachment)){
            $count = count($this->_attachment);
            for($i=0; $i<$count; $i++){
                $header .= "\r\n--" . $separator . "\r\n";
                $header .= "Content-Type: " . $this->getMIMEType($this->_attachment[$i]) . '; name="=?UTF-8?B?' . base64_encode( basename($this->_attachment[$i]) ) . '?="' . "\r\n";
                $header .= "Content-Transfer-Encoding: base64\r\n";
                $header .= 'Content-Disposition: attachment; filename="=?UTF-8?B?' . base64_encode( basename($this->_attachment[$i]) ) . '?="' . "\r\n";
                $header .= "\r\n";
                $header .= $this->readFile($this->_attachment[$i]);
                $header .= "\r\n--" . $separator . "\r\n";
            }
        }
 
        //结束邮件数据发送
        $header .= "\r\n.\r\n";
 
        $command[] = array("DATA\r\n", 354);
        $command[] = array($header, 250);
        $command[] = array("QUIT\r\n", 221);
 
        return $command;
    }
    protected function sendCommand($command, $code) {
        //echo 'Send command:' . $command . ',expected code:' . $code . '<br />';
        //发送命令给服务器
        try{
            if(socket_write($this->_socket, $command, strlen($command))){
                if(empty($code))  {
                    return true;
                }
                $data = trim(socket_read($this->_socket, 1024));
                //echo 'response:' . $data . '<br /><br />';
 
                if($data) {
                    $pattern = "/^".$code."+?/";
                    if(preg_match($pattern, $data)) {
                        return true;
                    }
                    else{
                        $this->_errorMessage = "Error:" . $data . "|**| command:";
                        return false;
                    }
                }
                else{
                    $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
                    return false;
                }
            }
            else{
                $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
                return false;
            }
        }catch(Exception $e) {
            $this->_errorMessage = "Error:" . $e->getMessage();
        }
    }
 
    protected function sendCommandSecurity($command, $code) {
        //echo 'Send command:' . $command . ',expected code:' . $code . '<br />';
        try {
            if(fwrite($this->_socket, $command)){
                //当邮件内容分多次发送时,没有$code,服务器没有返回
                if(empty($code))  {
                    return true;
                }
                //读取服务器返回
                $data = trim(fread($this->_socket, 1024));
                //echo 'response:' . $data . '<br /><br />';
 
                if($data) {
                    $pattern = "/^".$code."+?/";
                    if(preg_match($pattern, $data)) {
                        return true;
                    }
                    else{
                        $this->_errorMessage = "Error:" . $data . "|**| command:";
                        return false;
                    }
                }
                else{
                    return false;
                }
            }
            else{
                $this->_errorMessage = "Error: " . $command . " send failed";
                return false;
            }
        }catch(Exception $e) {
            $this->_errorMessage = "Error:" . $e->getMessage();
        }
    } 
    protected function readFile($file) {
        if(file_exists($file)) {
            $file_obj = file_get_contents($file);
            return base64_encode($file_obj);
        }
        else {
            $this->_errorMessage = "file " . $file . " dose not exist";
            return false;
        }
    }
    protected function getMIMEType($file) {
        if(file_exists($file)) {
            if(stristr($file."@", ".zip@")){
                return "application/zip";
            }
            return mime_content_type($file);
        }else {
            return false; //false
        }
    }
    protected function socket() {
        //创建socket资源
        $this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
 
        if(!$this->_socket) {
            $this->_errorMessage = socket_strerror(socket_last_error());
            return false;
        }
 
        socket_set_block($this->_socket);//设置阻塞模式
 
        //连接服务器
        if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {
            $this->_errorMessage = socket_strerror(socket_last_error());
            return false;
        }
        $str = socket_read($this->_socket, 1024);
        if(!preg_match("/220+?/", $str)){
            $this->_errorMessage = $str;
            return false;
        }
 
        return true;
    }
    protected function socketSecurity() {
        $remoteAddr = "tcp://" . $this->_sendServer . ":" . $this->_port;
        $this->_socket = stream_socket_client($remoteAddr, $errno, $errstr, 30);
        if(!$this->_socket){
            $this->_errorMessage = $errstr;
            return false;
        }
        stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
 
        stream_set_blocking($this->_socket, 1); //设置阻塞模式
        $str = fread($this->_socket, 1024);
        if(!preg_match("/220+?/", $str)){
            $this->_errorMessage = $str;
            return false;
        }
 
        return true;
    }
 
    /**
    * 关闭socket
    * @access protected
    * @return boolean
    */
    protected function close() {
        if(isset($this->_socket) && is_object($this->_socket)) {
            $this->_socket->close();
            return true;
        }
        $this->_errorMessage = "No resource can to be close";
        return false;
    }
    protected function closeSecutity() {
        if(isset($this->_socket) && is_object($this->_socket)) {
            stream_socket_shutdown($this->_socket, STREAM_SHUT_WR);
            return true;
        }
        $this->_errorMessage = "No resource can to be close";
        return false;
    }
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的示例代码,演示如何使用PHP操作MySQL数据库实现增删改查,并在微信小程序上实现登录与注册功能。 首先,我们需要在MySQL数据库中创建一个名为`users`的表,用于存储用户信息。该表包含以下字段: - `id`:用户ID,自增长整数。 - `username`:用户名,字符串类型。 - `password`:密码,字符串类型。 接下来,我们可以使用以下PHP代码实现增删改查功能: ```php <?php // 配置数据库连接信息 $db_host = 'localhost'; // 数据库主机名 $db_user = 'root'; // 数据库用户名 $db_password = 'root'; // 数据库密码 $db_name = 'test'; // 数据库名 // 连接数据库 $conn = mysqli_connect($db_host, $db_user, $db_password, $db_name); // 查询用户信息 function query_user($username, $password) { global $conn; $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $sql); return mysqli_fetch_assoc($result); } // 添加用户信息 function add_user($username, $password) { global $conn; $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; return mysqli_query($conn, $sql); } // 修改用户信息 function update_user($id, $username, $password) { global $conn; $sql = "UPDATE users SET username='$username', password='$password' WHERE id=$id"; return mysqli_query($conn, $sql); } // 删除用户信息 function delete_user($id) { global $conn; $sql = "DELETE FROM users WHERE id=$id"; return mysqli_query($conn, $sql); } ?> ``` 接下来,我们可以使用以下代码在微信小程序上实现登录与注册功能: ```javascript // 登录 wx.request({ url: 'http://example.com/login.php', method: 'POST', data: { username: 'username', password: 'password' }, success: function(res) { if (res.data.code == 0) { // 登录成功 } else { // 登录失败 } } }); // 注册 wx.request({ url: 'http://example.com/register.php', method: 'POST', data: { username: 'username', password: 'password' }, success: function(res) { if (res.data.code == 0) { // 注册成功 } else { // 注册失败 } } }); ``` 在以上示例代码中,`http://example.com`应替换为您的服务器地址。同时,您需要编写`login.php`和`register.php`文件,用于处理登录和注册请求。其中,登录请求应查询数据库并返回结果,注册请求应添加用户信息到数据库中。具体实现细节请根据您的需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YUJIANYUE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值