Question

Im trying to get a Logout button which first unsets the sessions and then closes the actual tab.
So far I only got the part with the session destroy and I can't figure out how to get the Tab close done.
I tried echo'ing that function after the sessions are destroyed, with setIntervals but I didn't manage to get that done. Anyone knows some tipps? Thanks!
Edit : all code is on the same page

Here's my code :

[HTML]

<input type="hidden" name="vLogout" value="false">
<input type="button" value="Logout" name="Logging out" onClick="logout(this.form)" style="background-color:#cc0000;color:#ffffff">  

[PHP]
This is the very first line of code after <?php

if($_REQUEST["vLogout"])
{
session_start();

session_unset();
session_destroy();
echo "<script>closeTab();</script>";
}  

[Javascript]

function logout(f)
{
var confirmed = confirm("Are you sure you want to logout?");
    if (confirmed){
    f.method="post";
    f.vLogout.value = true;
    f.submit();
    }
}

function closeTab(){
    confirm("test");
//window.open(document.URL,'_self','resizable=no,top=-245,width=250,height=250,scrollbars=no');
//window.close();
}
Was it helpful?

Solution

NOTE: Most probably, you can not close a tab with JS for security issue. You can only close a tab with JS what is opened with JS.

You can use JQuery. Do not submit the form. Send Ajax request to server with onclick event. When dataType of Ajax request is 'script', browser will execute response body as JS.

note: save files (index.php and logout.php) in same directory.

in index.html :

<html>
<head>
    <title>Ajax Logout</title>
      <style>
      .logout-button {
        background:#00aaee;
        display:inline-block;
        padding: 5px 10px;
        cursor: pointer;
      }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>

<body>

    <div class="logout-button">logout</div>

    <script type="text/javascript">

      function closeTab(){
        confirm("test");
        //your code here
      }

      $('.logout-button').on('click', function(){
        $.ajax({
            url: ('logout.php'),
            type: 'POST',
            dataType: 'script'
          });
      });

    </script>

  </body>
</html>

in logout.php :

<?php
session_start();
session_unset();
session_destroy();

echo "closeTab();";
?>

OTHER TIPS

you can try adding this line in logout(f) function.

f.dataType = 'script';

logout function should be:

function logout(f)
{
    var confirmed = confirm("Are you sure you want to logout?");
    if (confirmed){
      f.method="post";
      f.dataType = 'script'; //this line should be added
      f.vLogout.value = true;
      f.submit();
    }
}

in php code write script without script tag:

echo "closeTab();";

it should work.

function logout(f)
...
    f.submit();

You are actually submitting the form which results in a new document being requested, i.e. when the php script is done and its output <script>closeTab();</script> has been transmitted to the client, there is no function closeTab ..anymore.
There's also no guarantee that your javascript code is allowed to close that particular window, so I'd suggest letting your php script print out a complete html document that tries to close the window/tab but also displays some information for the user just in case the script fails.

use session_start(); in top of the page and not in any function

and than onClick="logout(this.form)" to onClick="logout(document.form)"

use input type=submit. not "Button" and on place of $_REQUEST["vLogout"] use
if(isset($_POST["vLogout"])) or if(isset($_GET["vLogout"]))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top