PHP 重定向阻止脚本
更新
我发现了阻碍我的脚本的问题。显然它与解密无关,而是我的重定向。当我删除这段代码时,脚本开始快速执行。仍然不确定为什么会导致这个问题?
// Make sure we have an Order ID
if( ! isset($_GET['id']) && ! isset($_POST['id']) ) {
header("Location: https://www.website.com/orders/");
exit;
}
原始问题:
我一直在使用此处找到的加密类:加密等级。我将数据存储在 MySQL 数据库中,采用 VARCHAR 二进制数据类型(以前我尝试过 BLOB 和 TINYBLOB)。
加密和解密都可以,但是解密需要大约 1 分钟。加密速度很快。
我想我还应该说这是通过 https 连接发生的(如果相关的话)。
我不记得解密总是需要这么长时间。您知道是什么原因造成的吗?当我注释掉 PHP 代码的解密部分并仅回显加密字符串时,它执行得很快。
下面注释中要求的代码
class Encryption
{
const CYPHER = 'blowfish';
const MODE = 'cfb';
const KEY = 'MyPersonalKey';
public function encrypt($plaintext)
{
$td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, self::KEY, $iv);
$crypttext = mcrypt_generic($td, $plaintext);
mcrypt_generic_deinit($td);
return $iv.$crypttext;
}
public function decrypt($crypttext)
{
$plaintext = '';
$td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
$ivsize = mcrypt_enc_get_iv_size($td);
$iv = substr($crypttext, 0, $ivsize);
$crypttext = substr($crypttext, $ivsize);
if ($iv)
{
mcrypt_generic_init($td, self::KEY, $iv);
$plaintext = mdecrypt_generic($td, $crypttext);
}
return $plaintext;
}
}
这是网页中的代码,我在其中设置了 MySQL 行中的变量。我正在使用 WordPress 的 $wpdb 对象。
$order = $wpdb->get_row("SELECT * FROM orders WHERE id = ".$order_id." LIMIT 0,1");
$addons_price = $order->addons_price;
$hooked_package = (isset($_GET['hooked_package'])) ? $_GET['hooked_package'] : $order->hooked_package;
$arrival_date_unix = $order->arrival_date_unix;
$order_data = unserialize($order->order_data);
$preview_total = $order_data['preview_price'] + $addons_price + $order_data['travel_insurance'];
$normal_total = $order_data['normal_price'] + $addons_price + $order_data['travel_insurance'];
$package_price = $order->package_price;
$total_price = $order->total_price;
$billing_cc = Encryption::decrypt($order->billing_cc);
另外,这是 MySQL 类型...
`billing_cc` varbinary(255) DEFAULT NULL
UPDATE
I found the issue that was holding up my script. Apparently it had nothing to do with decryption, but my redirect instead. When I removed this block of code, the script starting performing quickly. Still not sure why this was causing the issue?
// Make sure we have an Order ID
if( ! isset($_GET['id']) && ! isset($_POST['id']) ) {
header("Location: https://www.website.com/orders/");
exit;
}
ORIGINAL QUESTION:
I have been using the Encryption class found here: Encryption class. I am storing the data in a MySQL database, with a VARCHAR binary data type (formerly I tried BLOB and TINYBLOB).
The encrypting and decrypting both work, however it takes like 1 minute to decrypt. The encryption is fast.
I guess I should also say that this is happening over a https connection (in case that's relevant).
I don't remember it always taking this long to decrypt. Do you have any idea what could be causing this? When I comment out the decryption portion of the PHP code, and just echo back the encrypted string, it performs quickly.
CODE AS REQUESTED BELOW IN THE COMMENTS
class Encryption
{
const CYPHER = 'blowfish';
const MODE = 'cfb';
const KEY = 'MyPersonalKey';
public function encrypt($plaintext)
{
$td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, self::KEY, $iv);
$crypttext = mcrypt_generic($td, $plaintext);
mcrypt_generic_deinit($td);
return $iv.$crypttext;
}
public function decrypt($crypttext)
{
$plaintext = '';
$td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
$ivsize = mcrypt_enc_get_iv_size($td);
$iv = substr($crypttext, 0, $ivsize);
$crypttext = substr($crypttext, $ivsize);
if ($iv)
{
mcrypt_generic_init($td, self::KEY, $iv);
$plaintext = mdecrypt_generic($td, $crypttext);
}
return $plaintext;
}
}
Here is the code from the webpage, where I set the variables from the MySQL row. I am using WordPress' $wpdb object.
$order = $wpdb->get_row("SELECT * FROM orders WHERE id = ".$order_id." LIMIT 0,1");
$addons_price = $order->addons_price;
$hooked_package = (isset($_GET['hooked_package'])) ? $_GET['hooked_package'] : $order->hooked_package;
$arrival_date_unix = $order->arrival_date_unix;
$order_data = unserialize($order->order_data);
$preview_total = $order_data['preview_price'] + $addons_price + $order_data['travel_insurance'];
$normal_total = $order_data['normal_price'] + $addons_price + $order_data['travel_insurance'];
$package_price = $order->package_price;
$total_price = $order->total_price;
$billing_cc = Encryption::decrypt($order->billing_cc);
Also, here is the MySQL type...
`billing_cc` varbinary(255) DEFAULT NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指出的问题代码是一个简单的条件重定向。所以它应该与解密没有任何关系。我发现重定向缓慢的唯一原因是 Web 服务器负载过重、连接速度缓慢或存在其他性能问题。
The code you indicate as being your problem is a simple conditional redirect. So it shouldn't have anything to do with the decryption. The only reason I can see for the redirect being slow is that the web server is under heavy load, on a slow connection or has some other performance issue.