我想在登录后重定向到之前的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. PrettyFaces登录后重定向到RESTful url
- 3. 如何在j_security_check登录后重定向到url
- 4. 重定向到上一个url或返回()登录/注册后在Laravel 5.3
- 5. 瓶颈成功登录后如何重定向到上一页
- 6. 登录后重定向到另一页
- 7. 登录到前一页后重定向
- 8. 如何成功登录网站后重定向到新的URL?
- 9. 登录后,如何重定向到“url”而不是“state”SAML-sso
- 10. 登录后重定向,Wordpress URL
- 11. 重定向至Facebook登录后的URL
- 12. 如何在webview登录后重定向?
- 13. 如何在登录后重定向WordPress
- 14. PHP登录后重定向到BASE URL而不是前一页
- 15. 如何在Laravel 5登录到该页面后登录到上一页后重定向回到顶端
- 16. 如何在登录后重定向到同一页面
- 17. 如何在jsp登录后重定向到同一页面?
- 18. Laravel重定向登录后,JavaScript重定向到另一页
- 19. Django在外部OAuth登录后重定向到上一页
- 20. 登录后预期的URL重定向+默认重定向?
- 21. 重定向到相同的URL登录
- 22. 如何重定向到最后一个环节登录后从谷歌在PHP?
- 23. 如何在登录django后将用户重定向到用户特定的url?
- 24. 登录后重定向到最后一个视图
- 25. 使用弹簧安全登录重定向到特定URL后
- 26. Rails/Devise如何在登录后重定向到特定页面
- 27. 如何在登录到WordPress的特定页面后重定向?
- 28. 如何让Facebook的登录按钮重定向到一个特定的URL
- 29. Laravel 5.5登录后重定向到一个特定的路线
- 30. 在玩! 2.0 with Security trait如何在登录后重定向到原始URL?
它可以帮助您进行测试,如果您在某处放置了一个条件以防止您的代码尝试重定向到您刚刚来的页面。如果您在浏览器中看到重定向循环错误,则该情况可能会解决此问题。 – berty 2010-01-20 18:39:18