I've not done this before, but the question seems interesting. Therefore, if the answers you mentioned did not work then I would try the following concept, it may not be the best, but it's something.
1- Make a tracker on the server (properly in session) to keep track of the pages that the user has opened. So whenever the user open a page, make an ajax request sending the page url with a random generated id number to be stored and checked later. Each tab will get a different id.
2- Add a javascript listener in all pages that keep waiting for commands from the server. Depending on the server response, those listen will do the following:
Nothing -> in case the page is opened first time.
Close tab -> in case a new tab with same link is opened. So keep the new tab and close the old one. // Or close new one and refresh old one?
Update -> update server once tab is opened (directly when the page gets loaded) and just before they get closed, send the page url and current tab id to be check on the server.
Keep checking server, if a newer tab with same url is opened, then close current.
I'm not sure if this solution is suitable for you, so I will only write a pseudo code. If it's applicable, then later maybe can improve it a bit.
pseudo code php: tabControl.php
//session array structure
//when a new tab is added, it will take the following index of current url array. So later we can know which one is
//$_SESSION["tracker"][ current url ][ tab id] newer id will be added to the array in an ascending order, not based on their value, so later we can check which tabId came later.
$action = $_POST["action"];
$tabId = $_POST["id"];
$tabUrl = $_POST["url"];
if($action === "check")
{
processTab(tabUrl , $tabId);
}
elseif($action === "closing")
{
closeTab(tabUrl , $tabId) ;
}
/*if this a second tab opened with the same url, then echo the previous tab id and command the client to close self.
*if this the first tab, then store it in session.
*/
function processTab($tabUrl , $tabId)
{
//if new, then pass it to addTab
// else, check if there is a newer tab opened, then call closeTab
}
function addTab($tabUrl , $tabId)
{
// add a new tab associated with tabUrl -> couter -> tabId
}
function closeTab($tabUrl , $tabId)
{
//set that $tabUrl with the id to null.
//and ask the client to close the tab. //echo json_encode(array("responce"=>"closeSelf"))
}
And from the client side, you can use something similar to this pseudo code:
//current tab id
var tabId = Math.floor((Math.random()*100)+1);
//On exit, just run once before user close the tab/window.
$(window).unload(function(){
$.ajax({
type: 'POST',
url: 'tabControl.php',
async:false,
data: {"url":window.location.pathname , "action":"closing" , "id":tabId}
});
});
// keep checking periodically
function checkTabs()
{
//On load, just run once
$.ajax({
type: 'POST',
url: 'tabControl.php',
data: {"url":window.location.pathname , "action":"check", "id":tabId},
type:"json",
success:function(data)
{
if(data.responce === "closeSelf")
{// if the user detected that there is a newer with this url opened.
window.close();
}
}
}).always(function(){ setTimeout(checkTabs, 1000);});
}
checkTabs();// to call for the first time.
Hope this helps.