Php mail()打破了650个字符长的Url

前端之家收集整理的这篇文章主要介绍了Php mail()打破了650个字符长的Url前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用 PHP邮件功能发送链接与许多参数.编码后的url长度可以是650或更多字符,因为它保存变量以重新填充表单.

当我点击我的电子邮件中的链接时,它已损坏,因为在URL中的某处插入了一个空格.

继承我的sendMail功能

  1. protected function sendEmail($to,$subject,$body) {
  2. $headers = 'MIME-Version: 1.0' . "\r\n";
  3. $headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
  4. $headers .= 'From: Sales Order From <sales@imninjas.com>' . '\r\n';
  5. $headers .= 'X-Mailer: PHP/' . PHPversion() . '\r\n';
  6.  
  7. $body = '<html><body style="font-size: 10pt; font-family: Arial,Helvetica,sans-serif;">'.$body.'</body></html>';
  8.  
  9. return mail($to,$body,$headers);
  10. }

下面是我用sendMail调用代码.其’$salesUrl = $this-> getSalesFormUrl();’这是650个字符的网址塞满了编码的参数.

  1. function emailRep() {
  2. $params = $this->getParamaterArray();
  3. $shortUrl = $this->getShortUrl();
  4. $salesUrl = $this->getSalesFormUrl();
  5.  
  6. $mailSubject = "Return to the sales order form for ".$params['clientName']." at ".$params['company'].".";
  7.  
  8. $mailBody = "The following information is from an order created on,".date("l,F j,Y \a\t g:i a").",<br/><br/>";
  9. $mailBody .= "Customer Contact Information:<br/>";
  10. $mailBody .= "Name: ".$params['clientName'] params['company']."<br/>";
  11. $mailBody .= "Shortened Url to Send to the Customer:<br/>";
  12. $mailBody .= ($shortUrl) ? "<a href='".$shortUrl."'>".$shortUrl."</a><br/><br/>" : "There was an error shortening your url.<br/><br/>";
  13. $mailBody .= "The URL back to the sales form: For sales rep use only,<strong>Do not give to the customer</strong>.:<br/>";
  14. $mailBody .= "<span style='font-style: italic;'>Some email clients add special characters to long urls. If the link does not work then copy and paste it into your browser.</span><br/>";
  15. $mailBody .= "<a href='".$salesUrl."'>".$salesUrl."</a><br/><br/>";
  16.  
  17. return ($this->sendEmail($params['repEmail'],$mailSubject,$mailBody));
  18. }

这是我在电子邮件中收到的URL,您会注意到URL中间的空格’… BhsNKq Jsd_x4 …’.即使我发送完全相同的网址,每次都会在不同的地方发生这种情况.为了证明这一点,我在emailRep方法中没有空格的情况下对此网址进行了硬编码并多次发送.空间四处移动.

  1. http://example.com/admin/index.PHP?fdJdj9QgFAbgXzPcNJ3AAdbxgotxdk28cNRMjPESW9yihVbKxHR_vaeU7TSZxqSfHDhPX9Jg-lPneu1H9cFHE7yJxUcdfpto_XNxtv6XHkgw_Vk7oy7aFRdnYzONPDltWxV01Zi23glqnU-z91XnpvrnpvNGSXYo4Q0t6UEKUmUp9Sh28JC7Va01Pmaibcc83M8dpCzzKYn5H_rX_BhsNKq Jsd_x4w7e4zHqputSWdc1Uwzezt2LS5xGQJHKxlF98qbzUZMhauxw_k5ebK8YPwDFr776GEb11WPzGtfhjIFE68zL9H2l3FOCFXea5qkHUmO9pCihThlegDLAHamuIeCmTiXSGv8cm_TorL-6q8NnYuvp6nEfpntthgrvx3enkhWP-FJ0P4vYYAvyJ45pbR9slaw9pbPLsnu4d9nNZSuXJZdll2WXJRc2XKYgu0zRvcwuqBSVwuzylQu4ILugxOJCciG7kF1Qx8vjZl5Y8sIqL59dRu9dfnP5yuXJ5dnl2eXJ3crLl7x8lVeoFJWKe1co_uoK_B1eXZFckV2RXaG-fHvazCuWvGKVV84u23DlzZUrVyZXZldmVyZ3K69c8so57z8

这是我用来在通过电子邮件发送之前对url参数进行编码/解码的代码.

  1. class UrlEncoder {
  2. function compressUrl($url) {
  3. return rtrim(strtr(base64_encode(gzdeflate($url,9)),'+/','-_'),'=');
  4. }
  5.  
  6. function uncompressUrl($url) {
  7. $startParams = strrpos($url,"?");
  8.  
  9. if($startParams) {
  10. $paramaterString = substr($url,$startParams);
  11. $host = substr($url,strrpos($url,"?"));
  12. $uncompressedParamaters = gzinflate(base64_decode(strtr($paramaterString,'-_','+/')));
  13. return $host."?".$uncompressedParamaters;
  14. } else {
  15. return NULL;
  16. }
  17. }
  18. }

我该如何防止这个空间?我知道我可以用bit.ly这样的东西缩短URL,但它是一个内部工具.我觉得必须有办法阻止插入空间.

谁在正确的思想中使用650个字符长的查询字符串?

我的建议是保存查询字符串服务器端.将它放在带有AUTO_INCREMENT字段的数据库中,然后就可以获得它的ID.然后,您只需将URL发送为http://example.com/?email_key=ID_GOES_HERE,这是一个更短的网址.然后只需从数据库中查找查询字符串.

完成.

猜你在找的PHP相关文章