2010-01-20 2 views

ответ

2

Сохраните страницу перед входом в сеанс, после входа в систему прочитайте URL-адрес предыдущей страницы из сеанса и перенаправьте использование этой страницы.

+0

Это может помочь с тестированием, если вы помещаете условие где-нибудь, чтобы ваш код не пытался перенаправить на страницу, с которой вы только что пришли. Если в браузере вы видите ошибку цикла переадресации, условие может исправить ее. – berty

1

Вы можете передать URL-адрес «возврат» в качестве параметра на страницу входа. i.e http://yourserver.com/login/?return=http%3a%2f%2fyourserver.com%2fsomedir%2fsomething%2f. После успешного входа в систему вы используете параметр get для перенаправления с помощью простого HTTP-заголовка location:http://yourserver.com/somedir/something/.

Это, например, практикуется в различных службах Google и Microsoft, где есть одна страница для входа в систему и различных услуг, которые требуют, чтобы пользователь loogged в.

+0

Это (imho) опасно, поскольку URL-адрес может быть изменен. То есть пользователь получает ссылку на похожий URL-адрес, связанный с MS (содержит www.microsoft.com), регистрируется и перенаправляется на вредоносный URL-адрес, который выглядит просто как страница Microsoft. Перенаправление к реферер почти столь же опасно, поэтому единственный безопасный способ (для пользователя), чтобы сохранить первоначально названный URL перенаправлять: если ему {StoreUrl (CURRENTURL (user.IsLoggedIn!): Любая страница); RedirectTo ("Вход"); } Логин: if (LoginSuccessfull()) {RedirectTo (GetStoredUrl()); } – dbemerlin

+0

Согласен с этим. Фактически, страница входа в систему должна проверять путь «возврат», если она является источником происхождения. – naivists

1

Вы можете это использовать действие помощника я написал некоторое время назад. Ура!

class Your_Controller_Action_Helper_GoBack 
extends Zend_Controller_Action_Helper_Abstract 
{ 
    /** 
    * @todo Check if redirecting to the same domain 
    * @param bool $required Throw exception? 
    * @param bool $validateDomain 
    * @param bool $allowSubdomain 
    * @param string $alternative URL to redirect to when validation fails and required = true 
    * @param string $anchorParam Request parameter name which holds anchor name (#). Redirect to page fragment is not allowed according to HTTP protocol specification, but browsers do support it 
    * @throws Zend_Controller_Action_Exception if no referer is specified and $required == false or $checkdomain is true and domains do not match 
    */ 
    public function direct($required = true, $anchorParam = null, $validateDomain = true, $allowSubdomain = false, $alternative = null) 
    { 
    $front = Zend_Controller_Front::getInstance(); 
    $request = $front->getRequest(); 

    $referer = $request->getPost('http_referer'); 

    if (empty($referer)) { 
    $referer = $request->getServer('HTTP_REFERER'); 
    if (empty($referer)) { 

    $referer = $request->getParam('http_referer'); 

    } 
    } 

    if (null === $alternative) { 
    $alternative = $request->getPost('http_referer'); 
    if (null === $alternative) { 
    $alternative = $request->getParam('http_referer'); 
    } 
    } 

    if ($referer) { 

    if ($validateDomain) { 
    if (!$this->validateDomain($referer, $allowSubdomain)) { 
     $this->_exception($alternative); 
    } 
    } 

    if (null != $anchorParam) { 
    $referer .= '#' . $request->getParam($anchorParam); 
    } 

    $redirector = new Zend_Controller_Action_Helper_Redirector(); 
    $redirector->gotoUrl($referer); 
    } elseif($required) { 
    $this->_exception($alternative); 
    } 
    } 

    /** 
    * @throws Zend_Controller_Action_Exception With specified message 
    * @param string $message Exception message 
    * @param string $alternative 
    */ 
    private function _exception($alternative = null, $message = 'HTTP_REFERER is required.') 
    { 
    if ($alternative) { 
    if (Zend_Uri::check($alternative)) { 
    $redirector = new Zend_Controller_Action_Helper_Redirector(); 
    $redirector->gotoUrl($alternative); 
    } 
    } 

    throw new Zend_Controller_Action_Exception($message); 
    } 


    /** 
    * Check if domain from current url and domain from specified url are the same 
    * @param string $url Target url 
    * @param string $allowSubdomain false 
    */ 
    public function validateDomain($url, $allowSubdomain = false) 
    { 
    if (!Zend_Uri::check($url)) { 

    return false; 
    } 

    $currentUri = $this->getCurrentUri(); 

    $uri = Zend_Uri_Http::fromString($currentUri); 
    $currentDomain = $uri->getHost(); 

    $uri = Zend_Uri_Http::fromString($url); 
    $target = $uri->getHost(); 

    if ($allowSubdomain) { 
    // Find second dot from the end 
    $pos = strrpos($target, '.'); 

    if (false !== $pos) { 
    $pos = strrpos(substr($target, 0, $pos), '.'); 

    if (false !== $pos) { 
     $target = substr($target, $pos+1); 
    } 
    } 
    } 

    if ($target === $currentDomain) { 
    return true; 
    } 

    return false; 
    } 

    /** 
    * @return string Current URL 
    */ 
    public function getCurrentUri() 
    { 
    $request = $this->getRequest(); 
    $path = $request->getRequestUri(); 

    $server = $request->getServer(); 

    $host = $request->getServer('HTTP_HOST'); 
    $protocol = $request->getServer('SERVER_PROTOCOL'); 

    if (!empty($protocol)) { 
    $protocol = explode('/', $protocol); 
    $protocol = strtolower($protocol[0]); 
    } 

    if (empty($protocol)) { 
    $protocol = 'http'; 
    } 

    $baseUrl = $protocol . '://' . $host . '/'; 

    $path = trim($path, '/\\'); 

    $url = $baseUrl . $path; 

    return $url; 
    } 

    /** 
    * Like str_replace, but only once 
    * @param string $search 
    * @param string $replace 
    * @param string $subject 
    */ 
    public function replaceOnce($search, $replace, $subject) 
    { 
    $firstChar = strpos($subject, $search); 
    if($firstChar !== false) { 
    $beforeStr = substr($subject, 0, $firstChar); 
    $afterStr = substr($subject, $firstChar + strlen($search)); 

    return $beforeStr . $replace . $afterStr; 
    } else { 

    return $subject; 
    } 
    } 
} 
Смежные вопросы