简体   繁体   English

使用标头进行PHP重定向

[英]PHP redirection using header

My web structure is 我的网络结构是


         Header-of-page

Nav Link || 导航链接|| iFrame 的iFrame


       Footer

I'm Trying to handle session timeout, when session has timeout I'm trying to redirect page to login page, this works fine(session timeout). 我正在尝试处理会话超时,当会话超时时我正在尝试将页面重定向到登录页面,这很好(会话超时)。

Problem: When I'm redirecting the page,login page is displayed in iFrame, which is not expected. 问题:当我重定向页面时,登录页面显示在iFrame中,这是不期望的。

How can I redirect to login page(whole window),rather than opeing it in iFrame. 如何重定向到登录页面(整个窗口),而不是在iFrame中打开它。

I Tried: 1. using header 2. using javascript(Commented) 我试过: 1。使用标题2.使用javascript(评论)

<?php session_start();

$timeout = 1; // Set timeout minutes
$timeout = $timeout * 60; // Converts minutes to seconds

if (isset($_SESSION['timeout'])) 
{
    $session_life = time() - $_SESSION['timeout'];
    if ($session_life > $timeout) 
    {
        session_destroy();
        header("Location: login.php?msg=timeout");
        // echo '<script language="javascript">'; 
                    // echo 'window.location.replace("login.php");';
                    // echo '</script>';
    }
}
$_SESSION['timeout'] = time();

?>

Please guide me for this issue. 请指导我解决这个问题。 Thanks! 谢谢!

use this one 用这个

in script window.parent.location='http://localhost/users/login.php' 在脚本window.parent.location='http://localhost/users/login.php'

or follow this link 或点击此链接

https://forums.digitalpoint.com/threads/button-to-navigate-to-a-new-page-but-exit-the-iframe-too.1846291/ https://forums.digitalpoint.com/threads/button-to-navigate-to-a-new-page-but-exit-the-if​​rame-too.1846291/

hope you will get solution. 希望你能得到解决方案。

Try this: window.top.location.href = " http://www.site.com "; 试试这个:window.top.location.href =“ http://www.site.com ”;

As long as this is on the same domain name. 只要这是在同一个域名上。

More here: Redirect parent window from an iframe action 更多信息: 从iframe操作重定向父窗口

create form with target="_parent" and action="login.php" 使用target="_parent"action="login.php"创建表单

and submit using $(form).submit(); 并使用$(form).submit();提交$(form).submit();

The problem here is than the iFrame is a another window in the partent window. 这里的问题是iFrame是partent窗口中的另一个窗口。

So when is begin redirected the iFrame only affect self (Not the parent or partents of the parent). 因此,当开始重定向时,iFrame仅影响self(不是父项的父项或部分项)。

To don't use javascript we can put a link to login.php wich target the parent. 为了不使用javascript,我们可以为login.php添加一个链接到父级的链接。

<a href="login.php" target="_top">Goto login.php</a>

_top will target the top window frame. _top将定位顶部窗口框架。

http://reference.sitepoint.com/html/a/target#target__li5 Read about target attribute set on _top. http://reference.sitepoint.com/html/a/target#target__li5阅读有关_top上设置的目标属性的信息。

The other method is using javascript: 另一种方法是使用javascript:

window.top.location.assign("http://www.yoursite.com/login.php"); // Redirect
// window top frame to "http://yoursite.com/login.php"

Please read following links about javascript window documentation: 请阅读以下有关javascript窗口文档的链接:

http://www.w3schools.com/jsref/prop_win_top.asp http://www.w3schools.com/jsref/prop_win_top.asp

http://www.w3schools.com/js/js_window_location.asp http://www.w3schools.com/js/js_window_location.asp

In case yo want a "PHP" code, just use echo 如果你想要一个“PHP”代码,只需使用echo

http://www.php.net/manual/en/function.echo.php http://www.php.net/manual/en/function.echo.php

I hope this mightly helps. 我希望这可能会有所帮助。

one of the probably mistakes, 其中一个可能的错误,

if the encoding of this file is "UTF-8" it will create 2 hidden characters in the top of the file. 如果此文件的编码为“UTF-8”,它将在文件顶部创建2个隐藏字符。

To fix this issue, try to change the encoding to "UTF-8 without BOM" 要解决此问题,请尝试将编码更改为“无BOM的UTF-8”

you can put a exit; 你可以出口; after the redirect 重定向后

<?php session_start();

$timeout = 1; // Set timeout minutes
$timeout = $timeout * 60; // Converts minutes to seconds

if (isset($_SESSION['timeout'])) 
{
    $session_life = time() - $_SESSION['timeout'];
    if ($session_life > $timeout) 
    {
        session_destroy();
        header("Location: login.php?msg=timeout");
        exit(); // LOOK AT THIS LINE
        // echo '<script language="javascript">'; 
                    // echo 'window.location.replace("login.php");';
                    // echo '</script>';
    }
}
$_SESSION['timeout'] = time();

?> ?>

Try this way. 试试这种方式。 Php redirection works before JS redirection so browser never runs JS. Php重定向在JS重定向之前工作,因此浏览器永远不会运行JS。 which that's the only way you can redirect whole window object. 这是你可以重定向整个窗口对象的唯一方法。

<?php session_start();

$timeout = 1; // Set timeout minutes
$timeout = $timeout * 60; // Converts minutes to seconds

if (isset($_SESSION['timeout'])) 
{
    $session_life = time() - $_SESSION['timeout'];
    if ($session_life > $timeout) 
    {
        session_destroy();
        // header("Location: login.php?msg=timeout");
        echo '<script language="javascript">window.top.location.href = "login.php?msg=timeout";</script>'; 
    }
}
$_SESSION['timeout'] = time();

You would need to break the iframe. 您需要打破iframe。 Try this.. 试试这个..

if(this != top){
  top.location.href = this.location.href;
}

OR (with doc reference) 或(与文档参考)

if(this != top){
  top.document.location.href = this.document.location.href;
}

Alternatively 另外

this.top.location !== this.location && (this.top.location = this.location);

I am guessing this php being in the file running in the iframe in that case you have to instruct the parent window to redirect to login. 我猜这个php在iframe中运行的文件中,在这种情况下你必须指示父窗口重定向到登录。

Echo the below code from php so when you page will render in browser, it will instruct the script to reload the page. 从php回复下面的代码,这样当你的页面将在浏览器中呈现时,它将指示脚本重新加载页面。 But for the page not to load the rest of the page issue an exit(0) . 但是对于不加载页面其余部分的页面,请发出exit(0) Your final script should look like below. 您的最终脚本应如下所示。

<?php session_start();

$timeout = 1; // Set timeout minutes
$timeout = $timeout * 60; // Converts minutes to seconds

if (isset($_SESSION['timeout'])) 
{
    $session_life = time() - $_SESSION['timeout'];
    if ($session_life > $timeout) 
    {
        session_destroy();        
        echo '<script language="javascript">';

        //Echo the exact full url to your login page
        echo 'window.parent.location="login.php?msg=timeout"'; 
        echo '</script>';
        exit(0); // So script won't go further displaying the page
    }
}
$_SESSION['timeout'] = time();

?>

Hope that helps. 希望有所帮助。

Check this solution out. 检查此解决方案。 I would put it before the header to prevent flickering. 我会把它放在标题之前,以防止闪烁。 This way, your page will be prevented from swallowing itself. 这样,您的网页就会被禁止吞咽。

http://usablelayout.com/articles/automatically-break-out-iframe http://usablelayout.com/articles/automatically-break-out-iframe

<script type="text/javascript">
<!--
    if (top.location!= self.location) {
        top.location = self.location.href
    }
//-->
</script>

If you are using the login action on the same page, Header redirection will not work. 如果您在同一页面上使用登录操作,则标题重定向将不起作用。

you can use simple the window.location.href='url'; 你可以使用简单的window.location.href ='url';


For the login , you have to send the login query to new page, from there you can easily redirect easily... 对于登录,您必须将登录查询发送到新页面,从那里您可以轻松地轻松重定向...

Try this one 试试这个吧

php PHP

echo "<script> window.location='forgot.php'</script>";

html HTML

<META HTTP-EQUIV="REFRESH" CONTENT="3;URL=http://google.com">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM