0
我已經在頁數上應用了https重定向,但每當我通過ZF2重定向的其他路由時,下一頁將打開https。我只想在選定頁面上應用https。請建議.. ??我如何從ZF2中的HTTPS頁面重定向到HTTP頁面?
我已經在頁數上應用了https重定向,但每當我通過ZF2重定向的其他路由時,下一頁將打開https。我只想在選定頁面上應用https。請建議.. ??我如何從ZF2中的HTTPS頁面重定向到HTTP頁面?
搜索很多後,我找到了解決方案。只要更新你的應用程序的Module.php如下:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach('route', array($this, 'doHttpsRedirect'));
// your code
}
,並添加功能doHttpsRedirect,如:
public function doHttpsRedirect(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$uri = $e->getRequest()->getUri();
$controller = $e->getRouteMatch()->getParam('controller');
$action = $e->getRouteMatch()->getParam('action');
$securedPages = array('Application\Controller\Login', 'Application\Controller\Register', 'Application\Controller\shopping');
if (in_array($controller, $securedPages)) {
$scheme = $uri->getScheme();
if ($scheme != 'https') {
$uri->setScheme('https');
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $uri);
$response->setStatusCode(302);
$response->sendHeaders();
return $response;
}
} else if (!in_array($controller, $securedPages)) {
if ($_SERVER["HTTPS"] == "on") {
$uri->setScheme('http');
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $uri);
$response->setStatusCode(302);
$response->sendHeaders();
return $response;
}
}
}
這一切..!
Google是你的朋友。你有什麼企圖? – Mic1780 2014-09-20 04:10:41