在我的ZF2应用程序中,我希望加密我的参数,这样当有/product/1这样的链接时,将显示为/product/update/cdsk45kdi340kd0wlw0或类似的东西。我需要在控制器和视图中加密和解密它。最好的方法是什么?谢谢你的帮助。
发布于 2014-03-12 12:45:50
这里的要点是,您希望避免用户猜测url。我会为每个产品生成一个随机令牌。将此令牌与产品的id (和所有其他属性)一起存储在数据库中。
要生成作为令牌的随机字符串,可以使用Zend\Math\Rand:Rand::getString(10);为您提供10个字符的随机字符串。当您将产品存储在数据库中时,为每个产品生成一个随机字符串。然后,在您的控制器中,您不是基于标识符(id),而是基于令牌来获得产品。
发布于 2014-03-12 11:49:32
让路线看起来像这样
'product' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/product/update[/:hashedid]',
'constraints' => array(
'hashedid' => '[a-zA-Z0-9-_\.]+',
),
'defaults' => array(
'controller' => 'Index',
'action' => 'index',
),
),
),在控制器中
$hashedid = $this->params()->fromRoute('hashedid', 0);
$id = $this->dehash($hashedid);https://stackoverflow.com/questions/22348879
复制相似问题