Notaras
Notaras

Reputation: 717

HTML PHP: Preventing an iframe from remembering session state on page refresh

Is there a way to declare an iframe in a page so that each time you refresh the page, it clears any session variables within it?

Right now i have a web application written in .NET MVC and on one of its pages it loads an iframe that points to another application written in PHP. basically what is happening is that the iframe is remembering session state when i refresh the whole page but would ideally like to re-start it each time. Are there any ways to do this? or would i need to call a logout script within the iframe on page refresh?

Upvotes: 1

Views: 1013

Answers (1)

jmercier
jmercier

Reputation: 584

It's just a proof of concept, but it work . There is some informations missing about your project but:

you can create cookie for first visit, if user reload page and if cookie exist

load Iframe with parameter, else load normal iframe (the code is deliberately brief to produce the iframe)

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>with iframe</title>
    <script>
        function checkFirstVisit() {    
            if(document.cookie.indexOf('checkreload')==-1) {
                document.cookie = 'checkreload=1';
                document.write('<iframe width="560" height="315" src="content.php"></iframe> ');
            }
            else {
                document.write('<iframe width="560" height="315" src="content.php?reloaded=yes"></iframe>');
            }
        }
    </script>
</head>
<body onload="checkFirstVisit()">

</body>
</html>

And in your php code (iframe), just check and unset when you need

<?php
session_start();
if(isset($_GET['reloaded'])) {
    unset($_SESSION['name']);
} else {
    $_SESSION['name'] = 'nameSession';
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>frame</title>
</head>
<body>

    <p>
        <?php
        if(isset($_SESSION['name']))  { 
           echo $_SESSION['name'];
       }else{
           echo 'no name now !';
       }
       ?>
   </p>
</body>
</html>

Don't forget to play with cookie at your convenience (to manage sessions) I hope it will help you

Upvotes: 1

Related Questions