【发布时间】:2016-03-25 15:13:59
【问题描述】:
如果您转到subdirectoryname/index.php,我有此代码会将您重定向到您打开的最后一页。它有效,但我觉得它真的很愚蠢。有没有人对我如何更优雅地实现这一点有更好的想法?
不要将此与 window.history.back() 混淆; 我试图让浏览器记住您打开的最后一页。因此,即使您关闭浏览器。明天再打开网站。 www.example.com 您将被重定向到 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);
}
});
}
}
注意:我现在正在使用不同网站项目的子目录,因为该项目仍然没有自己的域。当我们将它移动到它自己的域时,将不再有 /subdirectoryname/ 并且 index.php 的路径将只是“index.php”而不是“subdirectoryname/index.php”
【问题讨论】:
-
我认为这是duplicate
-
@adeneo 我不是在寻找像 window.history.back() 这样的后退按钮,我试图让浏览器记住您打开的最后一页。因此,即使您关闭浏览器。明天再打开网站。 www.example.com 您将被重定向到 www.example.com/lastpageyouopened.php
-
我认为这个问题需要详细说明。用户打开的最后一页,是当前页,不是吗?
-
@Teemu 我说如果他/她在 index.php 中,它不会将它保存在 cookie 中:D
标签: javascript php jquery html ajax