我想在登錄後重定向到之前的URL。請告訴我該怎麼做?如何在登錄後重定向到上一個URL
回答
在登錄到會話前存儲頁面,登錄後從會話中讀取前一頁的URL並將使用重定向到該頁面。
您可以將「返回」URL作爲參數傳遞給登錄頁面。即http://yourserver.com/login/?return=http%3a%2f%2fyourserver.com%2fsomedir%2fsomething%2f
。成功登錄後,您可以使用get參數通過使用簡單的HTTP標頭location:http://yourserver.com/somedir/something/
進行重定向。
這一點,例如,在不同的谷歌和微軟的服務,其中有一個頁面,用於登錄,並要求用戶不同的業務實踐應在loogged。
這是(imho)危險,因爲URL可以更改。即用戶可以看到一個看似與MS相關的URL(包含www.microsoft.com)的鏈接,然後登錄並重定向到一個看起來像微軟頁面的惡意URL。 重定向到引用者是幾乎一樣危險,所以唯一安全的方法(用戶)是存儲原名URL的重定向到它:如果{StoreUrl(CURRENTURL (user.IsLoggedIn!): 任何網頁); RedirectTo( 「登錄」); } 登錄: if(LoginSuccessfull()){RedirectTo(GetStoredUrl()); } – dbemerlin 2010-01-20 12:31:17
同意。事實上,登錄頁面應該驗證「返回」路徑,如果它是知道的原點。 – naivists 2010-01-20 12:37:44
您可以在此用行動幫助我寫的前一段時間。乾杯!
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;
}
}
}
- 1. Facebook登錄:如何登錄後重定向到一個特定的URL在
- 2. 登錄後重定向到上一頁?
- 3. 登錄後重定向到上一頁
- 4. PrettyFaces登錄後重定向到RESTful url
- 5. 如何在登錄後重定向到上一頁
- 6. 如何在j_security_check登錄後重定向到url
- 7. 重定向到上一個url或返回()登錄/註冊後在Laravel 5.3
- 8. 瓶頸成功登錄後如何重定向到上一頁
- 9. 登錄後重定向上一頁
- 10. 登錄到前一頁後重定向
- 11. 登錄後重定向到另一頁
- 12. 登錄後,如何重定向到「url」而不是「state」SAML-sso
- 13. 如何成功登錄網站後重定向到新的URL?
- 14. 登錄後重定向,Wordpress URL
- 15. 重定向至Facebook登錄後的URL
- 16. 如何在webview登錄後重定向?
- 17. 如何在登錄後重定向WordPress
- 18. Magento登錄後如何在上一頁重定向
- 19. PHP登錄後重定向到BASE URL而不是前一頁
- 20. 如何在Laravel 5登錄到該頁面後登錄到上一頁後重定向回到頂端
- 21. 如何在jsp登錄後重定向到同一頁面?
- 22. 如何在登錄後重定向到同一頁面
- 23. 從登錄URL重定向
- 24. 如何在登錄URL後設置重定向?
- 25. Laravel重定向登錄後,JavaScript重定向到另一頁
- 26. Django在外部OAuth登錄後重定向到上一頁
- 27. 登錄後預期的URL重定向+默認重定向?
- 28. 如何重定向到最後一個環節登錄後從谷歌在PHP?
- 29. 重定向到相同的URL登錄
- 30. 如何在登錄django後將用戶重定向到用戶特定的url?
它可以幫助您進行測試,如果您在某處放置了一個條件以防止您的代碼嘗試重定向到您剛剛來的頁面。如果您在瀏覽器中看到重定向循環錯誤,則該情況可能會解決此問題。 – berty 2010-01-20 18:39:18