Emma
Emma

Reputation: 657

PHP redirection using header

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!

Upvotes: 5

Views: 2939

Answers (12)

Marcos Eusebi
Marcos Eusebi

Reputation: 637

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.

Upvotes: 3

Chandru
Chandru

Reputation: 133

Try this one

php

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

html

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

Upvotes: 2

Gautam Tak
Gautam Tak

Reputation: 129

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

Upvotes: 2

Luc
Luc

Reputation: 273

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>

Upvotes: 2

LoneWOLFs
LoneWOLFs

Reputation: 2306

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.

Upvotes: 2

Dru
Dru

Reputation: 556

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

Upvotes: 2

risyasin
risyasin

Reputation: 1321

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

Upvotes: 2

MZaragoza
MZaragoza

Reputation: 10111

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

?>

Upvotes: 2

Dharmesh Patel
Dharmesh Patel

Reputation: 1891

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

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

Upvotes: 3

T.Baba
T.Baba

Reputation: 649

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"

Upvotes: 2

Grant
Grant

Reputation: 2441

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

Upvotes: 5

Lucifer
Lucifer

Reputation: 516

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.

Upvotes: 5

Related Questions