1

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"

  • 1
    I'm thinking this a [duplicate](http://stackoverflow.com/questions/3659782/code-for-back-button) – adeneo Dec 18 '15 at 20:46
  • @adeneo I am not looking for a back button like 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 – Johanna Cristine Dy Dec 18 '15 at 20:49
  • I'm thinking there's something to elaborate in the question. The last page user has opened, is the current page, isn't it? – Teemu Dec 18 '15 at 20:50
  • @YuriTkachenko That's what my code does. But I think the way I did it is very silly. Like I have to check for current location all the time. and i put a hidden in the index.php ^^; – Johanna Cristine Dy Dec 18 '15 at 20:51
  • @Teemu I put that if he/she is in index.php , it will not save it in the cookie :D – Johanna Cristine Dy Dec 18 '15 at 20:52
  • @JohannaCristineDy Well, to be honest, I only read the title ... – Teemu Dec 18 '15 at 20:53
  • @Teemu Only when he/she is index.php , it will redirect to last page opened. but if you open /differentpage.php , it will not redirect :D – Johanna Cristine Dy Dec 18 '15 at 20:54

1 Answers1

0

You could catch esc_url($_SERVER['REQUEST_URI']) at each page change then you could put that into a header( Location... php call. as the page loads when the browser is reopened and they log in - wrapped in an:

  $redirect_url_var = http etc from your database

  if( isset( $redirect_url_var)){
  header("Location: $redirect_url_var");
  } 

using the code from php.net/manual/en/function.header.php

Make sure no output happens before header() or it will make a mess - put it right at the top of the page before doctype (assumes they are logged in on your site while browsing.)

You might be able to get the information from a session cookie - How can I get $_SESSION data from SESSID? might help if you can use sessions.

You can see the settings in your php installation under "Sessions" if you put this into a blank php page on you web root:

 <?php phpinfo(); ?>

You can mess with the time they last for (plus many other SO posts): php.net/manual/en/session.configuration.php and how to keep the session active even if the browser was accidentally close? This will only work for the one location and browser and won't be picked up if the user changes to another machine, e.g. at work or home.

You might get some joy out of ModRewrite with an htaccess guru and a logfile of users https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever - perhaps in a flat csv type file with IP,URI,Time (Time is so you can delete dead ones where IP has changed.) The IP address, can change - e.g. TalkTalk reset theirs quite often, or they change when the router is restarted, leaving you with a log entry for an IP address that no longer exists, or at least doesn't belong to that user any more.

You could use something like the guts of fail2ban - storing the IP addresses – Fail2ban www.fail2ban.org/wiki/index.php If you are lucky you might figure out a way to access the logfiles on your server like Fail2ban does and then you could work out the last page visited by a given IP address from there - saving you the need to log it yourself.

There might also be a way to get user stats from awstats cryptome.org/2014/01/awstats-log-spy.pdf

Not sure I would want a browser to take someone else to the last page I had been looking at on your site, if no log-in is involved in the process, but that is up to you.

Community
  • 1
  • 1
Steve
  • 808
  • 1
  • 9
  • 14