用于管理PayPal即时付款通知的一些简单的PHP类

分享于 

31分钟阅读

Web开发

  繁體 雙語

更新

介绍

这是一个快速写作,共享一种方法来管理一种方法来管理through通知通知( IPN )。 看看网络上的几个例子,我被所看到的所有这些例子都碰到了。 而且,它们大多是整体的,而且很难理解所有必要的步骤。 但是多亏了他们,我才能够找到它,本文中的代码就是结果。 希望通过共享,它的他人会省去一些难度。

类 below 也不是全面的,因为它们只处理我对单项购买的需求。 还可以能缺少另外一些特性,但它们为我工作,并为那些希望进一步进行的人提供一个好开始。

通过将进程分解为负责逻辑子任务的对象来处理 IPNs。 让人们更容易理解那些熟悉对象方向的人。 ( 如果你不是,我希望这有助于你看到好处),并且更精确地在方法中。 它将为你节省大量的工作。

CodeProject上还有几个其它互连付款方式,你可能还想查看。 我找到的是 C# 和 ASP.NET。 在 2013年07月 中,stine文章的AllanPolichbecker666和Mat似乎与这个任务最为相关。 请随意指出我错过的任何其他。 如果你需要额外支付选项,( DaSpors链接了一个令人印象深刻的PHP/JavaScript/SQL 购物车解决方案,它可以处理多个 PayPal IPNs

解决办法是什么呢

为了不给出重要的详细信息,我将复制/粘贴我的代码 below 并更改相应的信息。 这将使一篇长篇文章成为一个代码转储,但我还会在这里放置一些注释来更容易理解。 我相信代码是自我编写的,可以能是一些补偿,但是我仍然抱歉。 如果将来有足够的调用,我将花时间从修改后的代码中创建文件。

解决方案将进程分解为五个对象:

  • payPalController - 这里类处理贝壳进程的非互连部分的逻辑。 它检查PayPal发送的重复交易,并验证IPN事务中的产品是否实际上是我的站点中的一个。 如果是,它开始向数据库添加购买,并向客户发送电子邮件。
  • payPalIpn - 这是IPN处理发生的地方。 非常重要 !
  • rmwStoreDB - 这里单元处理向数据库中的数据库添加事务。
  • myMailClass - 一个基本的寄存器,可以大大扩展以处理HTML电子邮件,但它提供了基本功能。 它依赖于的Mailer,让事情变得容易。
  • myLoggerClass - 记录错误,如果在设置中指定了这里类操作,则可以发送电子邮件。

此外,还有两个非类文件,其中包含:

  • 数据库设置信息,以及
  • 电子邮件帐户信息

要使它的正常工作,你必须执行三步。 首先,你必须建立一个数据库,我不会在这里覆盖。 在线足够了,如果你还没有找到这个过程的话。 然后你必须将五个类文件和两个密码文件复制到外部可见的子目录下面的子目录中,并将它们修改到你的( 位置'上面的规范。 和 finally,你为omg指定的网页将通知发送给( 如果你走到button-by-button的基础上) 必须如下所示:

<!doctypehtmlpublic'-//W3C//DTDHTML4.01//EN''http://www.w3.org/TR/html4/strict.dtd'><html><head><title>RandomMonkeyWorks</title><metahttp-equiv="Content-Type"content="text/html; charset=iso-8859-1"><metaname="Generator"content="Notepad++"><metaname="Description"content="PayPal IPN Processor"><metaname="Keywords"content="Nothing"></head><body><!-- The code to deal with the PalPal logic: --><?php
 //The following is for the directory below publicly visible,//where the processing stuff is:$base = dirname(dirname(dirname(__FILE__)));
 define('BASE_PATH', $base == DIRECTORY_SEPARATOR? $base : $base. DIRECTORY_SEPARATOR);
 require_once (BASE_PATH. "phpPayPalController.php");
 $ppc = new payPalController();
 $ppc->setTesting(false);
 $ppc->setLogging(false);
 $ppc->processPayPalIpnPayment();
 ?><!-- THINGS TO DO WHEN GOING FROM TESTING TO PRODUCTION: --><!-- Change 'testing' to false via the '$ppc->setTesting' line 上面 --><!-- Make certain buttons are coded correctly on site to access this page --><!-- Make certain the phpPayPalIpnClass opens socket to proper secured server --></body></html>

可以看到,payPalController 是页面需要知道的唯一单元。 控制器将设置其他类本身:

<?php//This should be placed in the subdirectory beneath the user-accessible//one. On localhost this can't be done, so remember to do it manually//when copying to the site.require_once(BASE_PATH. "phpPayPalIpnClass.php");
require_once(BASE_PATH. "phpLoggerClass.php");
require_once(BASE_PATH. "phpRmwStoreDbActions.php");
require_once(BASE_PATH. "phpMyMailClass.php");class payPalController {
 private $db;
 private $logger;
 private $testing;
 function __construct() {
 $this->logger = new myLogger();
 $this->logger->setLogFile("log.txt");
 $this->logger->setLogging(false);
 $this->db = new rmwStoreDB();
 $this->db->setLogger($this->logger);
 $this->testing = false;
 }
 function setTesting($state) {
 $this->testing = $state;
 $this->logger->setLogging($state);
 }
 function setLogging($state) { $this->logger->setLogging($state); }
 function processPayPalIpnPayment() {
 $processor = new payPalIpn();
 $processor->setTesting($this->testing);
 $processor->setLogger($this->logger);
 $this->logger->log("Processing a payment.". PHP_EOL);
 if (!$this->validatePostData()) return;
 if (!$processor->processPost()) return;
 if ($this->duplicateTransaction()) return;
 if (!$this->verify()) return;
 if (!$this->addOrderToDatabase()) return;
 $this->sendProduct();
 }
 function validatePostData() {
 $step = 0;
 $ret = true;
 if(!isset($_POST['txn_id'])) $step = 1;
 if(!isset($_POST['shipping'])) $step = 2;
 if(!isset($_POST['quantity'])) $step = 3;
 if(!isset($_POST['mc_gross'])) $step = 4;
 if(!isset($_POST['mc_gross'])) $step = 5;
 if(!isset($_POST['last_name'])) $step = 6;
 if(!isset($_POST['first_name'])) $step = 7;
 if(!isset($_POST['address_zip'])) $step = 8;
 if(!isset($_POST['mc_currency'])) $step = 9;
 if(!isset($_POST['item_number'])) $step = 10;
 if(!isset($_POST['payer_email'])) $step = 11;
 if(!isset($_POST['address_city'])) $step = 12;
 if(!isset($_POST['address_name'])) $step = 13;
 if(!isset($_POST['address_state'])) $step = 14;
 if(!isset($_POST['address_street'])) $step = 15;
 if(!isset($_POST['receiver_email'])) $step = 16;
 if(!isset($_POST['payment_status'])) $step = 17;
 if(!isset($_POST['address_country'])) $step = 18;
 //if(!isset($_POST['option_selection1'])) $step = 19;if ($step!= 0) $ret = false;
 if ($ret == false) {
 $this->logger->log("POST DATA not set: $step". PHP_EOL);
 $response = "";
 foreach ($_POST as$key => $value) {
 $numPosts += 1;
 if($magicQuotesFuncExists == true && get_magic_quotes_gpc() == 1) {
 $value = urlencode(stripslashes($value));
 }
 else {
 $value = urlencode($value);
 }
 $response. = "&$key=$value". PHP_EOL;
 }
 $this->logger->log($response);
 }
 return$ret;
 }
 private function duplicateTransaction() {
 $ret = false;
 if ($this->db->itemExists("orders", "payPalTransId", $_POST['txn_id'])) {
 $this->logger->log("Transaction:". $_POST['txn_id']. " exists". PHP_EOL);
 $ret = true;
 }
 else {
 $this->logger->log("Transaction:". $_POST['txn_id']. 
 " does not exist". PHP_EOL);
 }
 return$ret;
 }
 private function verify() {
 $nl = PHP_EOL;
 //First, check for valid item number:if (!$this->db->itemExists("products", "id", $_POST ['item_number'])) {
 $this->logger->log("Item number:". $_POST['item_number']. 
 " doesn't exist in database$nl");
 return false;
 }
 else {
 $this->logger->log("Item number:". $_POST['item_number']. 
 " exists in database$nl");
 }
 //Check that we received the proper amount, or more, in the case of Texas taxes: $this->dbPrice = $this->db->getCellValue("price", "products", "id",
 $_POST['item_number']);
 if ($_POST['mc_gross'] <$this->dbPrice) {
 $this->logger->log("Payment received (". $_POST ['mc_gross']. 
 ") less than item price. (". $this->dbPrice. PHP_EOL);
 return false;
 }
 else {
 $this->logger->log("Adequate payment received (". $_POST ['mc_gross']. 
 ").$nl");
 }
 if ($_POST['mc_currency']!= "USD") {
 $this->logger->log("Paid in non-US funds - need to investigate.$nl");
 return false;
 }
 else {
 $this->logger->log("US Currency received - OK.$nl");
 }
 if ($_POST['receiver_email']!= "someone@somewhere.com" && $_POST['receiver_email']!= "someone@somewhere.com") {
 $this->logger->log("Incorrect receiver email received (". 
 $_POST['receiver_email']. ")$nl");
 return false;
 }
 else {
 $this->logger->log("Correct email received (
". $_POST['receiver_email']. ")$nl");
 }
 //And the most important one:if ($_POST['payment_status']!= "Completed") {
 $this->logger->log("Payment incomplete from PayPal$nl");
 return false;
 }
 return true;
 }
 private function addOrderToDatabase() {
 //Everything will revolve around email address as of primary importance; if there is//one in the database the record will be updated to reflect any changes to the//account. If one doesn't exist, one will be created. $this->logger->log("Updating database.". PHP_EOL);
 $this->db->addOrUpdateUser();
 $this->db->addOrder();
 return true;
 }
 private function sendProduct() {
 $nl = PHP_EOL;
 $mailHandler = new myMailer();
 $mailHandler->setLogger($this->logger);
 if ($this->testing) {
 $mailTo = 'someone@somewhere.com';
 }
 else {
 $mailTo = $_POST['payer_email'];
 }
 if ($_POST['item_number'] == "something") {
 doSomething(); //You get the idea... }
 }
 }?>

在 上面 代码 block 中,为了更漂亮的格式,我包装了一些可以能需要在移植到你的网站时打开。 它们可以能是正确的,但是以防万一,这值得注意。 另外,请记住更改本文中使用的电子邮件地址。

这就是 payPalIpn 类。 如前所述,这处理网络处理,并将正确的响应发送回 bayes。

<?phprequire_once(BASE_PATH. "phpLoggerClass.php");class payPalIpn {
 private $logger; //This will actually be a reference; see 'setLogger'//for mechanism which accomplishes this. private $ipnVerifiedC;
 private $testingC;
 function __construct() {
 $this->ipnVerifiedC = false;
 $this->testingC = false;
 }
 function ipnVerified() { return $this->ipnVerifiedC; }
 function setTesting($state) { $this->testingC = $state; }
 function setLogger(myLogger &$logFile) { $this->logger = $logFile; }
 function processPost() {
 // Send an empty HTTP 200 OK response to acknowledge receipt of the notification //header('HTTP/1.1 200 OK'); $nl = PHP_EOL;
 //Log the contents if the logger is set to do so: $this->logger->log("RECEIVED:$nl". var_export($_POST, true). PHP_EOL);
 //Everything below here is originally from//http://designertuts.com/wp-content/uploads/2007/10/paypalipnphp.txt, which was said//to be a copy of the example code specified on the Paypal site. Of course, I modified//formatting and necessary logic. Quite a lot, actually.//Paypal POSTs HTML FORM variables to this page. We must return all the variables//back to PayPal unchanged and add an extra parameter 'cmd 'with value//'_notify-validate'$response = 'cmd=_notify-validate';
 //Check for magic quotes (this comes from a PayPal pdf on IPN usage - don't remember//the URL):$magicQuotesFuncExists = false;
 if(function_exists('get_magic_quotes_gpc')) {
 $magicQuotesFuncExists = true;
 }
 //The following variable is used for a quick, dummy check to keep from sending a//response unless the POST was long, meaning it probably came from PayPal:$numPosts = 0;
 // go through each of the POSTed vars and add them to the variableforeach ($_POST as$key => $value) {
 $numPosts += 1;
 if($magicQuotesFuncExists == true && get_magic_quotes_gpc() == 1) {
 $value = urlencode(stripslashes($value));
 }
 else {
 $value = urlencode($value);
 }
 $response. = "&$key=$value";
 }
 $this->logger->log("AFTER MAGIC QUOTES:$nl".var_export($_POST, true).PHP_EOL);
 // post back to PayPal system to validate$header = "POST/cgi-bin/webscr HTTP/1.1$nl";
 $header. = "Host: www.sandbox.paypal.com$nl";
 $header. = "Content-Type: application/x-www-form-urlencoded$nl";
 $header. = "Content-Length:". strlen($response). PHP_EOL. PHP_EOL;
 //In a live application send it back to www.paypal.com, but during development use//the PayPal sandbox. Paypal Sandbox only seems to accept using ssl connections,//and on port 443.if ($this->testingC) {
 $socket = fsockopen ('ssl://www.sandbox.paypal.com', 443, $socketErrNum,
 $socketErrStr, 30);
 }
 else {
 $socket = fsockopen ('ssl://www.paypal.com', 443, $socketErrNum,
 $socketErrStr, 30);
 }
 //Oldie: $socket = fsockopen ('www.paypal.com', 80, $socketErrNum,// $socketErrStr, 30);if ($socket) $this->logger->log("Socket successful$nl");
 else $this->logger->log("Socket failed!$nl");
 if (!$socket) {
 // HTTP ERROR Failed to connect. Send me an email notification:$mail_Body = "Error from fsockopen:$nl". $socketErrStr. 
 "$nl$nlOriginal PayPal Post Data (COULD BE BOGUS!)$nl$nl";
 foreach ($_POST as$key => $value) {
 $value = urlencode(stripslashes($value));
 $mail_Body. = "&$key=$value". PHP_EOL;
 }
 mail($myEmail, "IPN Error Noficiation: Failed to connect to PayPal", $mail_Body,
 "someone@somewhere.com");
 // The original code used"fwrite($fh, $socketErrStr)" for the following: $this->logger->log("Socket error:". $socketErrStr);
 return;
 }
 //Now respond to PayPal's posting. The previous 'if' statement returned//and terminated this if there was a socket error, so we can concentrate on what we//need. First, send our response to the POST, and check for verification:$receivedVerification = false;
 $this->logger->log("Number of posts: $numPosts$nl");
 //Only send a response if it comes from PayPal.//The quick and dirty way to do so is see how many posts were in the original message:if ($numPosts> 3) {
 //Actually send the response: $this->logger->log("Sending the post back:$nl");
 fputs ($socket, $header. $response);
 $this->logger->log("SENT:$nl$nl$header$response$nl$nl");
 //And get their response. First lines will be transaction data, finally followed by//'VERIFIED' or 'UNVERIFIED' (or something). It took a//little work to find out that SSL had to be used for the Sandbox, and to get it//working correctly. But the previous '$socket' initialization finally//succeeded.$receivedVerification = false;
 $endOfStreamReached = false;
 while (!feof($socket) &&!$endOfStreamReached &&!$receivedVerification) {
 $result = fgets ($socket, 1024); //Get a line of response $this->logger->log("RECEIVED: $result");
 if (strncmp ($result, "VERIFIED", 8) == 0) {
 $receivedVerification = true;
 //$this->logger->log("VERIFIED! VERIFIED! VERIFIED!$nl"); }
 //else {// $this->logger->log("NOT VERIFIED HERE!$nl");// }if (strncmp ($result, "0", 1) == 0) $endOfStreamReached = true;
 }
 }
 fclose ($socket);
 $result = false;
 if ($receivedVerification == false) {
 $this->logger->log(
 "$nl$nlINVALID TRANSACTION! (Improper PayPal response received)$nl");
 }
 else {
 $this->ipnVerifiedC = true;
 $this->logger->log("TRANSACTION VERIFIED!$nl");
 $result = true;
 }
 return$result;
 }
 }
 ?>

接下来是数据库处理。 第一行的db_config.php 文件存储数据库用户和密码信息。 关键是在外部世界不可见 db_config.php,这就是为什么你必须将它放置在你站点上最顶层的public 文件夹。

<?phprequire_once(BASE_PATH. "pp_db_config.php");
require_once(BASE_PATH. "phpLoggerClass.php");class rmwStoreDB {
 private $loggerC; //This will actually be a reference;//see 'setloggerC' for mechanism which accomplishes this. private $lastRowC;
 function setlogger(mylogger $logFile) { $this->loggerC = $logFile; }
 function tellDb($sql) {
 $ret = mysql_query($sql);
 if (!$ret) {
 $this->loggerC->log("DATABASE ERROR:". PHP_EOL. mysql_error(). PHP_EOL. 
 "Query:". HtmlEntities($sql));
 die();
 }
 return$ret;
 }
 function itemExists($table, $column, $value) {
 $rows = $this->tellDb("Select * from". $table. " where". $column. " = '". 
 $value. "'");
 $lastRowC = mysql_fetch_array($rows);
 if ($lastRowC) return true;
 return false;
 }
 function getCellValue($what, $table, $column, $theId) {
 //No checking will be done to ensure only one row is returned, and default value//will be '0.00.'$rows = $this->tellDb("Select". $what. " from". $table. " where". $column. 
 " =". $theId);
 $row = mysql_fetch_array($rows);
 if ($row) return $row['price'];
 elsereturn 0.00;
 }
 function addOrUpdateUser() {
 //We are using the $_POST data, which is global to the application.//These are the fields we are dealing with: 'first_name' 'last_name' //'payer_email', and, for shipped products ONLY - 'address_name'//'address_street' (can be 2 lines separated by PHP_EOL?) 'address_city' //'address_state' 'address_zip' 'address_country_code' 'address_country'//First, see if the user exists:$rows = $this->tellDb("Select * from customers where email = '". 
 $_POST['payer_email']. "'");
 //Simply use the first of the returned rows:$row = mysql_fetch_array($rows);
 if (!$row) {
 $this->loggerC->log("Adding user to database");
 $this->addUser();
 }
 else {
 $this->loggerC->log("User already exists in DB.". PHP_EOL);
 //See if the records match the ones we have, and if not, change it: $this->updateUser($row);
 }
 }
 private function addUser() {
 $cmd = "Insert into customers (firstName, lastName, shippingName, email,". 
 "addressLine1, city, state, zipCode, country) values ('". $_POST['first_name']. "', '". 
 $_POST['last_name']. "', '". 
 $_POST['address_name']. "', '". 
 $_POST['payer_email']. "', '". 
 $_POST['address_street']. "', '". 
 $_POST['address_city']. "', '". 
 $_POST['address_state']. "', '". 
 $_POST['address_zip']. "', '". 
 $_POST['address_country']. "')";
 $this->tellDb($cmd);
 $this->loggerC->log("Added: '". $_POST['first_name']. "', '". 
 $_POST['last_name']. "', '". 
 $_POST['address_name']. "', '". 
 $_POST['payer_email']. "', '". 
 $_POST['address_street']. "', '". 
 $_POST['address_city']. "', '". 
 $_POST['address_state']. "', '". 
 $_POST['address_zip']. "', '". 
 $_POST['address_country']. "')");
 }
 private function updateUser(array $row) {
 //First, check old values:if ($row['firstName']!= $_POST['first_name'] ||
 $row['lastName']!= $_POST['last_name'] ||
 $row['shippingName']!= $_POST['address_name'] ||
 $row['email']!= $_POST['payer_email'] ||
 $row['addressLine1']!= $_POST['address_street'] ||
 $row['city']!= $_POST['address_city'] ||
 $row['state']!= $_POST['address_state'] ||
 $row['zipCode']!= $_POST['address_zip'] ||
 $row['country']!= $_POST['address_country']) {
 //Form a command string:$cmd = "UPDATE customers SET";
 $cmd. = "firstName = '". $_POST['first_name']. "',";
 $cmd. = "lastName = '". $_POST['last_name']. "',";
 $cmd. = "shippingName = '". $_POST['address_name']. "',";
 $cmd. = "addressLine1 = '". $_POST['address_street']. "',";
 $cmd. = "city = '". $_POST['address_city']. "',";
 $cmd. = "state = '". $_POST['address_state']. "',";
 $cmd. = "zipCode = '". $_POST['address_zip']. "',";
 $cmd. = "country = '". $_POST['address_country']. "'";
 $cmd. = "WHERE email = '". $_POST['payer_email']. "'";
 $this->loggerC->log(PHP_EOL. "Changing user with email". 
 $_POST['payer_email']. PHP_EOL);
 $old = $row['firstName']. ",". $row['lastName']. ",". 
 $row['shippingName']. ",". $row['email']. ",". 
 $row['addressLine1']. ",". $row['city']. ",". 
 $row['state']. ",". $row['zipCode']. ",". 
 $row['country']. PHP_EOL. PHP_EOL;
 $this->loggerC->log($old);
 $this->loggerC->log($cmd. PHP_EOL);
 $this->tellDb($cmd);
 }
 }
 function addOrder() {
 $nl = PHP_EOL;
 //Everything will be in the $_POST values//First, get the customer number:$cmd = "Select id from customers where email = '". $_POST['payer_email']. "'";
 //We just entered this information into the database, so error checking isn't//really needed? We will put something in just for grins.$rows = $this->tellDb($cmd);
 $row = mysql_fetch_array($rows);
 if (!$row) {
 $this->loggerC->log("HUGE PROBLEM! CUSTOMER ID NOT FOUND - ABORTING$nl");
 die();
 }
 $id = $row['id'];
 $theDate = date('F j, Y, g:i a');
 $tz = date('T');
 $ppID = $_POST['txn_id'];
 $grossPay = $_POST['mc_gross'];
 $shipping = $_POST['shipping'];
 $cmd = "Insert into orders (customer, date, timeZone, payPalTransId, grossPmt,". 
 "shipping) values ('$id', '$theDate', '$tz', '$ppID', '$grossPay', '$shipping')";
 $this->tellDb($cmd);
 $this->loggerC->log("Inserting order into orders table:$nl$cmd$nl$nl");
 //Now we have to add the order items into the orderItems table.//First, we need to get the order number from the record which was just entered:$cmd = "Select id from orders where payPalTransId = '$ppID'";
 $rows = $this->tellDb($cmd);
 $row = mysql_fetch_array($rows);
 //Should not need this, but...if (!$row) {
 $this->loggerC->log("HUGE PROBLEM! ORDER ID NOT FOUND - ABORTING$nl");
 die();
 }
 $id = $row['id'];
 //And the command to enter the item:$itemNum = $_POST['item_number'];
 $qty = $_POST['quantity'];
 $info = $_POST['option_selection1'];
 $cmd = "Insert into orderItems (orderNumber, item, quantity, extraInfo)". 
 "values('$id', '$itemNum', '$qty', '$info')";
 $this->loggerC->log("Inserting into order items:$nl$cmd$nl");
 $this->tellDb($cmd);
 }
 }?>

对以下类比以前的小很多 ! 第一个是邮件机单元。 当然,它的简洁性可以能与它的使用更多的事实相关,而且我应该发送更好的HTML响应。 一切就绪。

<?phprequire_once(BASE_PATH. "pp_hiddenPasswords.php");
require_once(BASE_PATH. "phpLoggerClass.php");
require_once(BASE_PATH. "lib/swift_required.php");class myMailer {
 private $myEmail;
 private $logger; //This will actually be a reference;//see 'setLogger' for mechanism which accomplishes this. function __construct() {
 $this->myEmail = "someone@somewhere.com";
 }
 function setLogger(myLogger &$logFile) { $this->logger = $logFile; }
 function sendEbook($ebookType, $mailTo) {
 //You can either send a link to the product or a file. The choice is yours.//Refer to swift mailer documentation, and other online resources for the appropriate//steps to take for each option. If you want to send a file, the 'mailWithAttachments'//routine may be useful. The usage of it is:. . .
 $this->mailWithAttachment($fileName, BASE_PATH, $mailTo, $this->myEmail, $from,
 $replyTo, $subject, $msg);
 }
 private function mailWithAttachment($filename, $path, $mailTo, $from_mail, $from_name,
 $replyto, $subject, $message) {
 $transport = Swift_SmtpTransport::newInstance('mail.some_server.com', 465, 'ssl')
 ->setUsername(hiddenEmailAccount())
 ->setPassword(hiddenEmailPassword())
 ;
 $mailer = Swift_Mailer::newInstance($transport);
 $message = Swift_Message::newInstance()
 ->setSubject($subject)
 ->setFrom(array($from_mail => $from_name))
 ->setTo(array($mailTo))
 ->setBody($message)
 //->addPart('<p>Here is the message itself</p>', 'text/html') ->attach(Swift_Attachment::fromPath($path.$filename))
 ;
 $this->logger->forceLog(date('F jS, Y h:i:s A'). ": Sending". $filename. 
 " to". $mailTo. ":");
 $result = $mailer->send($message);
 $this->logger->forceLog("Result =". $result. PHP_EOL);
 }
 }?>

最后是 logger 类:

<?phpclass myLogger {
 private $fileNameC;
 private $doLoggingC;
 function __construct() {
 $this->fileNameC = "log.txt";
 $this->doLoggingC = true;
 }
 function setLogFile($fileName) { $this->fileNameC = $fileName; }
 function setLogging($state) { $this->doLoggingC = $state; }
 function log($msg) {
 if ($this->doLoggingC == true) {
 file_put_contents($this->fileNameC, $msg, FILE_APPEND);
 }
 } 
 function forceLog($msg) {
 file_put_contents($this->fileNameC, $msg, FILE_APPEND);
 }
 }
 ?>

但是还有两个文件需要,尽管你可以将它们合并到一个,如果你需要相应的修改 上面。 它们用于存储数据库信息和电子邮件信息。 同样,必须不要向外部公开这些内容,并且必须将它们放置在站点上最顶层的可见文件夹中。

db_config.php 文件的内容如下所示。 除了那些不是真正的用户,密码和数据库 !

<?php// Connection Parameters$db_con = mysql_connect("localhost", "theUser", "thePassword", true) or die(mysql_error());$db_selected = mysql_select_db("theDatabase") or die(mysql_error());?>

finally,电子邮件密码文件(" emailPassword.php"在 上面 代码中),具有同样的警告:

<?php// Connection Parametersfunction hiddenEmailAccount() {
 return"someone@somewhere.com";
 }
function hiddenEmailPassword() {
 return"thePassword";
 }?>

如果你走这个路线,我希望以前的信息是有用的,即使你没有。 感谢你阅读,我将寄送给你快乐编码的希望 ! 如果你有任何改进,请在评论中发表。


MAN  PHP    notification  classes  SOM  
相关文章