I have this code which will redirect you to the last page you opened if you go to subdirectoryname/index.php
. It's working but I find it really silly. Does anyone have a better idea on how i can achieve this more elegantly?
Do not confuse this with window.history.back(); I am trying to make the browser remember your last page opened. So even if you close your browser. and open the website tomorrow again. www.example.com You'll be redirected to www.example.com/lastpageyouopened.php
setCookieLastPage.php
<?php
setcookie(
'LastPageVisited',
$_POST['lastpagevisited'],
time() + 199999999
);
?>
index.php
<?php include_once 'header.php'; ?>
<a href="<?php if(isset($_COOKIE['LastPageVisited'])){echo $_COOKIE['LastPageVisited'];}else{ echo 'defaultpage.php';} ?>" class="hiddenlink-lastpageopened"></a>
<?php include_once 'footer.php'; ?>
custom.js
if($(location).attr('pathname') == "/subdirectoryname/" || $(location).attr('pathname') == "/subdirectoryname/index.php" || $(location).attr('pathname') == "/subdirectoryname/index.php/" )
{
var lastvisitedpagelink = $('.hiddenlink-lastpageopened').attr('href');
setTimeout(function(){
window.location.href = lastvisitedpagelink;
},300);
}
runSETCookieLastPageVisited(window.location.href);
function runSETCookieLastPageVisited(lastpagevisited)
{
if($(location).attr('pathname') != "/subdirectoryname/" || $(location).attr('subdirectoryname') != "/play2win/index.php" || $(location).attr('subdirectoryname') != "/play2win/index.php/" )
{
$.ajax(
{
type: "POST",
url: "setCookieLastPage.php",
data: {"lastpagevisited":lastpagevisited},
datatype: "json",
cache: false,
success: function(data)
{
//alert(data);
}
});
}
}
NOTE: I am using a subdirectory of a different website project for now because this project still doesn't have its own domain. When we move it to it's own domain, there will no longer be a /subdirectoryname/ and the path to index.php will just be "index.php" and not "subdirectoryname/index.php"