执行 PHP:检测链接何时被点击
我对一些 PHP 代码有点进退两难。我正在开发的网站有一个“返回上一页”选项,我希望它的行为与浏览器的后退按钮非常相似。就目前而言,我使用 $_SESSION 变量来跟踪当前页面和上一页的内容。我还对变量进行了“刷新验证”,这样我就不会导致前一页和当前页相同。
所以这是问题: 在当前的实现中,如果我转到一个页面,说“register.php”,然后转到“forgot.php”,则前一页将是“register.php”,这很好。但是,如果我单击“返回上一页”,我最终会回到“register.php”,而上一页是“forgot.php”,现在我会返回一个 2 页循环。
我尝试实现 SplQueue 来帮助我跟踪变量,并尝试在链接中使用 dequeue() 函数来获取最后一页作为链接显示。当实际调用出队并导致元素消失时,问题就出现了,因此如果我刷新,该元素将不再在队列中并且链接会发生变化。我通过“刷新验证”为我调用出队的函数解决了这个问题,它按照我希望的方式工作。现在的问题是前向链接。如果我将自己定向到另一个页面,我不希望旧链接使信息出列。
前任: 我在 register.php 上,我的上一页是“forgot.php”。 “返回上一页”链接准确地显示“forgot.php”是它将定向到的页面,但现在它不再在队列中,因此如果我转到另一个页面,请说“profile.php”,然后使用返回按钮返回到“register.php”,如果您再次点击“返回上一页”,它将不再显示“forgot.php”作为您将转到的页面。
所以,我想我的问题实际上是如何让链接调用 PHP 函数,而无需实际调用该函数,直到单击链接为止。我尝试过让链接指向 JavaScript 函数,但 JS 函数往往告诉我我的队列是空的,这是完全错误的。
附带说明一下,这些页面是 HTML 和 PHP 的混合体。 HTML 是提供给我的,我一直在添加 PHP 以向字段添加功能并从数据库获取数据。如果需要的话,我可以使用 PHP 来回显 HTML 链接,并且可以使用一个小的 ,那也可以。
我感谢您花时间尝试帮助我。
编辑:
因此,为了尝试澄清一点,我有一个结构,当前正在跟踪用户访问时已经访问过的页面。它创建页面的迷你历史记录。我的问题是我有这样的代码:
<a href="somelink">Back To Previous Page</a>
而且我不知道“somelink”是什么,因为它会根据您的历史记录而改变。我知道我可以这样做:
<a href="<?php someFunction() ?>">Back To Previous Page</a>
如果我执行上述操作,该函数会在页面显示时执行,因此很难使用 array_pop()
或 dequeue()
但同样,一旦页面显示,PHP 就会被执行。我正在寻找一种方法来显示链接,然后当且仅当单击“返回上一页”链接时将其从历史记录中删除。截至目前,我按照下面的建议在 $_SESSION
中存储一个数组,因为它是一个数组,所以我可以将数组中的最后一个元素显示为链接,所以唯一真正的问题是找到一种在单击链接时从数组中删除元素的方法。
编辑2:
我一直在互联网上搜索并决定使用JavaScript和AJAX来调用PHP文件。这允许我对我拥有的链接进行 onClick,以便我可以控制何时从我的 $_SESSION['links']
变量中进行 array_pop
操作。
我不认为我的 AJAX 实际上做了任何可悲的事情,所以我使用的代码如下。
<script src="js/jquery-1.4.2.min.js" type="text/javascript">
function dequeue()
{
$.ajax({
type: "POST",
url: "common.php",
data: {action: "rem"},
success: function(output) {
alert(output);
}
});
}
</script>
据我所知, PHP
switch($_POST['action'])
{
case "rem":
array_pop($_SESSION['links']);
break;
default:
if(isset($_SESSION['current']) && $_SESSION['current'] != $_SERVER['REQUEST_URI'])
{
array_push($_SESSION['links'], $_SESSION['current']);
}
$_SESSION['current'] = $_SERVER['REQUEST_URI'];
break;
}
允许我在会话变量中添加历史记录的链接,除非我单击“返回上一页”链接,因为该链接将具有“rem”代码。我也对 $_SESSION['current'] = $_SERVER['REQUEST_URI'];
以及它应该放置的位置有点怀疑。
I've got a bit of a dilemma with some PHP code. The site I'm working on has a "Back to Previous Page" option and I'd like it to behave much like a browser's back button. As it stands right now, I'm using a $_SESSION variable to track what the current and previous pages are. I've also "refresh-proofed" the variables so that I don't end up with both the previous and current pages being the same.
So here's the issue:
With the current implementation, if I go to one page, say "register.php" and then go to "forgot.php", the previous page will be "register.php" which is fine. However, if I click "Back to Previous Page" I'll end up back at "register.php" with the previous page being "forgot.php" which now leaves me with a 2-page loop with going back.
I tried implementing SplQueue to help me keep track of variables and I tried using the dequeue() function in my links to get the last page to show up as the link. The problem comes in when the dequeue is actually called and causes the element to disappear so that if I refresh, the element is no longer in the queue and the link changes. I fixed this by "refresh-proofing" the function that calls the dequeue for me and it works as I would like it to. The problem is now forward-linking. If I direct myself to another page, I don't want the old links to dequeue information.
Ex:
I'm on register.php and my previous page is "forgot.php". The "Back to Previous Page" link accurately shows that "forgot.php" is the page it will direct to, but now it's no longer in the queue, so if I go to another page, say "profile.php" and then use the back button to go back to "register.php", it will no longer show "forgot.php" as the page that you will go to if you hit "Back to Previous Page" again.
So, I guess my question is really how I can make a link call a PHP function without actually calling that function UNTIL the link has been clicked. I've tried having the link point to a JavaScript function, but the JS functions tend to tell me that my queue is empty, which is completely wrong.
As a side note, the pages are a mix of HTML and PHP. The HTML is supplied to me and I've been adding the PHP in to add functionality to fields and to get data from a database. I have no problem using PHP to echo the HTML links if I have to, and if it can be done in HTML with a small <?php someCode(); ?>
, that's fine too.
I thank you for your time to try and help me out.
EDIT:
So to try and clarify a bit, I have a structure that is currently tracking pages that the user has already been to as they visit them. It creates a mini history of the pages. My issue is that I have code like this:
<a href="somelink">Back To Previous Page</a>
And I don't know what "somelink" is since it will change depending on your history. I know I can do something like:
<a href="<?php someFunction() ?>">Back To Previous Page</a>
If I do anything like the above, the function is executed as the page is being displayed, so it makes it difficult to use an array_pop()
or a dequeue()
but again, the PHP will be executed as soon as the page is displayed. What I'm looking for is a way to display the link and then remove it from the history if and only if the "Back to Previous Page" link is clicked. As of right now, I'm storing an array in $_SESSION
as was suggested below and since it's an array, I can show the last element in the array as the link, so the only real problem is to find a way to remove elements from the array when the link is clicked.
EDIT 2:
I've been scouring the internet and decided upon using JavaScript with AJAX to call a PHP file. This allows me to us an onClick on the links I have so that I can control when I array_pop
from my $_SESSION['links']
variable.
I don't think my AJAX is actually doing anything sadly, so the code I'm using is below.
<script src="js/jquery-1.4.2.min.js" type="text/javascript">
function dequeue()
{
$.ajax({
type: "POST",
url: "common.php",
data: {action: "rem"},
success: function(output) {
alert(output);
}
});
}
</script>
and the PHP is
switch($_POST['action'])
{
case "rem":
array_pop($_SESSION['links']);
break;
default:
if(isset($_SESSION['current']) && $_SESSION['current'] != $_SERVER['REQUEST_URI'])
{
array_push($_SESSION['links'], $_SESSION['current']);
}
$_SESSION['current'] = $_SERVER['REQUEST_URI'];
break;
}
As far as I can tell, this will allow me to add a link to the history in the session variable unless I'm clicking on the "Back to Previous Page" link since that link will have the "rem" code. I'm also a bit suspicious of the $_SESSION['current'] = $_SERVER['REQUEST_URI'];
and where it should be placed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将数组存储在会话中并将数组视为堆栈(相应地使用
array_push
和array_pop
)。当用户点击后退按钮以外的其他内容时,将当前页面推入堆栈。否则,弹出它。You can store array in a session and treat the array like a stack (use
array_push
andarray_pop
accordingly). When the user hits something but the back button, push the current page to the stack. otherwise, pop it.如果必须这样做,我会这样做:
在每个“可记住”页面的标题中:
这样做的作用是:“如果该页面存在于历史记录中,则将其从任何位置删除并将其作为最后一页如果没有,就把它作为历史的最后一页”。这样你就不会有任何循环。
I would do it like this if I had to:
And within the header of every "rememberable" page:
What this does is: "If the page exists in the history, remove it from wherever it is and put it as the last page of the history. If not, just put it as the last page of the history". That way you won't have any loops.