5

My web structure is


         Header-of-page

Nav Link || 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.

How can I redirect to login page(whole window),rather than opeing it in iFrame.

I Tried: 1. using header 2. using javascript(Commented)

<?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!

Grant
  • 2,413
  • 2
  • 30
  • 41
Emma
  • 617
  • 2
  • 12
  • 29
  • Is your page within an iframe already? – Grant Aug 20 '13 at 10:51
  • @Grant : My login.php is not in iFrame,but my home.php(Structure mentioned above),which is page after login successed. Same structure is followed on all pages of web. – Emma Aug 20 '13 at 10:54
  • 1
    From what I understand you cannot redirect to a destination outside of an iframe (traverse the iframe). – Grant Aug 20 '13 at 10:55
  • @Grant : It seems like as you said,but stil hoping to get somthing,that will help to sort this. Thanks for your help. – Emma Aug 20 '13 at 11:02
  • You may use echo ''; instead of header("Location: login.php?msg=timeout"); – vivek salve Aug 30 '13 at 06:53

12 Answers12

5

use this one

in script 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/

hope you will get solution.

Lucifer
  • 516
  • 1
  • 6
  • 25
  • This also get redirected in iFrame only. – Emma Aug 20 '13 at 11:03
  • 1
    Login you can use this – Lucifer Aug 20 '13 at 11:15
  • @Biswajeet: your hyperlink options works well, but how can I click link onLoad or something. I tried $('#linkLogin').click() & $('#linkLogin').trigger.('click'). But its not redirecting automatically, Can you help me on this? – Emma Aug 26 '13 at 12:17
  • u can use window.location.replace("login.php"); – Lucifer Aug 26 '13 at 12:54
  • @Biswajeet: I tried this already. Now just looking if my link is like this on page say timeout.html : Login. Link should get clicked automatically,cause this is the only way by which I can redirect to _parent(outside of iFrame), but it should be work direct rather than manually clicking. Thanks! – Emma Aug 26 '13 at 13:22
  • plz check your if block is properly conditioned because you wrote `$_SESSION['timeout'] = time();` after if then `if (isset($_SESSION['timeout'])) ` always return false so for this how will you go to if block you have to place `$_SESSION['timeout'] = time();` on else block – Lucifer Aug 26 '13 at 13:39
  • @Emma: This should work fine; I'm not sure why it wouldn't be working for you. Are you trying to do this in addition to the http redirect? It should be done instead of the http redirect; using both won't work right. – user1618143 Aug 29 '13 at 20:19
  • don't echo the scripts write it in this way it is warking ` $timeout) { session_destroy(); // header("Location: login.php?msg=timeout"); ?> ` – Lucifer Aug 30 '13 at 04:46
5

Try this: 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

Community
  • 1
  • 1
Grant
  • 2,413
  • 2
  • 30
  • 41
  • @Emma Was this helpful at all? – Grant Aug 20 '13 at 15:54
  • Now I have created,timeout page,and while loading it I have added your code. And I'm getting desired results and I'm sorted now. Thanks a lot! :) – Emma Aug 30 '13 at 09:16
3

create form with target="_parent" and action="login.php"

and submit using $(form).submit();

Dharmesh Patel
  • 1,881
  • 1
  • 11
  • 12
3

The problem here is than the iFrame is a another window in the partent window.

So when is begin redirected the iFrame only affect self (Not the parent or partents of the parent).

To don't use javascript we can put a link to login.php wich target the parent.

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

_top will target the top window frame.

http://reference.sitepoint.com/html/a/target#target__li5 Read about target attribute set on _top.

The other method is using 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:

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

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

In case yo want a "PHP" code, just use echo

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

I hope this mightly helps.

Marcos Eusebi
  • 617
  • 7
  • 17
2

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.

To fix this issue, try to change the encoding to "UTF-8 without BOM"

T.Baba
  • 649
  • 7
  • 17
2

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();

?>

MZaragoza
  • 10,108
  • 9
  • 71
  • 116
2

Try this way. Php redirection works before JS redirection so browser never runs 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();
risyasin
  • 1,325
  • 14
  • 24
2

You would need to break the 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);
Dru
  • 556
  • 4
  • 21
2

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.

Echo the below code from php so when you page will render in browser, it will instruct the script to reload the page. But for the page not to load the rest of the page issue an 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.

LoneWOLFs
  • 2,306
  • 3
  • 20
  • 38
2

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

<script type="text/javascript">
<!--
    if (top.location!= self.location) {
        top.location = self.location.href
    }
//-->
</script>
Luc
  • 273
  • 1
  • 9
2

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';


For the login , you have to send the login query to new page, from there you can easily redirect easily...

Gautam Tak
  • 129
  • 1
  • 7
2

Try this one

php

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

html

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