8

我使用 php 邮件功能发送包含许多参数的链接。编码后的 url 可以是 650 或更多字符长,因为它包含用于重新填充表单的变量。

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

这是我的 sendMail 功能:

    protected function sendEmail($to, $subject, $body) {
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
        $headers .= 'From: Sales Order From <sales@imninjas.com>' . '\r\n';
        $headers .= 'X-Mailer: PHP/' . phpversion() . '\r\n';

        $body = '<html><body style="font-size: 10pt; font-family: Arial, Helvetica, sans-serif;">'.$body.'</body></html>';

        return mail($to, $subject, $body, $headers);
    }

这是我调用 sendMail 的代码。它的'$salesUrl = $this->getSalesFormUrl();' 那是 650 多个字符的 url 塞满了编码参数。

    function emailRep() {
        $params = $this->getParamaterArray();
        $shortUrl = $this->getShortUrl();
        $salesUrl = $this->getSalesFormUrl();

        $mailSubject = "Return to the sales order form for ".$params['clientName']." at ".$params['company'].".";

        $mailBody = "The following information is from an order created on, ".date("l, F j, Y \a\t g:i a").",<br/><br/>";
        $mailBody .= "Customer Contact Information:<br/>";
        $mailBody .= "Name: ".$params['clientName'] params['company']."<br/>";
        $mailBody .= "Shortened Url to Send to the Customer:<br/>";
        $mailBody .= ($shortUrl) ? "<a href='".$shortUrl."'>".$shortUrl."</a><br/><br/>" : "There was an error shortening your url.<br/><br/>";
        $mailBody .= "The URL back to the sales form: For sales rep use only, <strong>Do not give to the customer</strong>.:<br/>";
        $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/>";
        $mailBody .= "<a href='".$salesUrl."'>".$salesUrl."</a><br/><br/>";

        return ($this->sendEmail($params['repEmail'], $mailSubject, $mailBody));
    }

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

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

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

class UrlEncoder {
    function compressUrl($url) {
        return rtrim(strtr(base64_encode(gzdeflate($url, 9)), '+/', '-_'), '=');
    }

    function uncompressUrl($url) {
        $startParams = strrpos($url, "?");

        if($startParams) {
            $paramaterString = substr($url, $startParams);
            $host = substr($url, 0, strrpos($url, "?"));
            $uncompressedParamaters = gzinflate(base64_decode(strtr($paramaterString, '-_', '+/')));
            return $host."?".$uncompressedParamaters;
        } else {
            return NULL;
        }
    }
}

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

4

3 回答 3

2

谁在他们的头脑中使用 650 个字符长的查询字符串?

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

完毕。

于 2012-12-20T04:12:22.303 回答
1

我有你需要的东西,http://www.9lessons.info/2009/01/split-url-from-sentence-using-php.html。使用 API 创建 tinyurl 链接,数据库中没有任何内容 :)

于 2013-01-03T20:08:24.923 回答
-1

好的,我有同样的问题。我的解决方案是我自己的链接缩小...在数据库中创建新表,其中包含几行,旧脚本中的几行代码以及用于重定向的新页面...这是最短的解释,如果您需要帮助,请询问: )

编辑:

    function emailRep() {
    $params = $this->getParamaterArray();
    $shortUrl = $this->getShortUrl();
    $salesUrl = $this->getSalesFormUrl();
/***********************************************************************************/   
    $arr = str_split('QWERTYUIOPLKJHGFDSAZXCVBNM123456789qwertyuioplkjhgfdsazxcvbnm'); // get all the characters into an array
    shuffle($arr); // randomize the array
    $arr = array_slice($arr, 0, 6); // get the first six (random) characters out
    $short = implode('', $arr); // smush them back into a string
    mysql_query("INSERT INTO shortlinks VALUES(NULL, '$salesUrl', '$short')");
/*******************************************************************************************/

    $mailSubject = "Return to the sales order form for ".$params['clientName']." at ".$params['company'].".";

    $mailBody = "The following information is from an order created on, ".date("l, F j, Y \a\t g:i a").",<br/><br/>";
    $mailBody .= "Customer Contact Information:<br/>";
    $mailBody .= "Name: ".$params['clientName'] params['company']."<br/>";
    $mailBody .= "Shortened Url to Send to the Customer:<br/>";
    $mailBody .= ($shortUrl) ? "<a href='".$shortUrl."'>".$shortUrl."</a><br/><br/>" : "There was an error shortening your url.<br/><br/>";
    $mailBody .= "The URL back to the sales form: For sales rep use only, <strong>Do not give to the customer</strong>.:<br/>";
    $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/>";
    $mailBody .= "<a href='".$short."'>".$short."</a><br/><br/>"; // Rename $salesUrl to $short

    return ($this->sendEmail($params['repEmail'], $mailSubject, $mailBody));
}

并重定向页面:

$token=$_GET['token']; // like http://example.com/out.php?token=ahgByT or make it cleaner with htaccess
$qry=mysql_query("SELECT * FROM links WHERE short='$token'");
$arr=mysql_fetch_array($qry);
$out=$arr['long_link'];
header("Location: ".$out);
?>
于 2012-12-20T01:45:37.110 回答