简体   繁体   English

<a>点击时</a>设置会话变量

[英]Set session variable on <a> click

I'm trying to make an <a> link which triggers PHP code on the next page. 我试图建立一个<a>链接,该链接将触发下一页的PHP代码。 I've tried using $_GET variables to do this but the thing is I also want to remove the variable afterwards, as I automatically link back to the redirected page with header() . 我试过使用$ _GET变量来执行此操作,但是事情是我也想在以后删除该变量,因为我会自动通过header()链接回重定向页面。 There don't seem to be any feasible ways to do this without redirecting the user to one page alone, but the thing is they're expected to be redirected to the page they were on previously. 没有将用户单独重定向到一个页面,似乎没有任何可行的方法可以做到这一点,但事实是,人们希望将他们重定向到他们先前所在的页面。 Keeping $_GET variables then cause an endless loop of redirects. 保留$ _GET变量将导致无限循环的重定向。

In general, I wish to avoid using $_GET as it could be abused in the context I'm using it in. Any other workarounds would be greatly appreciated, though. 通常,我希望避免使用$ _GET,因为在我使用它的上下文中它可能会被滥用。不过,任何其他解决方法都将不胜感激。 Basically I'm just trying to use an <a> link to remove an entry from a MySQL database. 基本上,我只是想使用<a>链接从MySQL数据库中删除条目。

Here's the PHP that handles the variable. 这是处理变量的PHP。

if (isset($_GET['rm'])) # 'rm' contains the uuid of the entry to be deleted.
{
    $uuid = $_GET['rm'];
    unset($_GET['rm']); # Didn't expect this to work, of course it didn't remove the variable from the URL.
    $query = "DELETE FROM posts WHERE uuid = '$uuid'";
    $result = $mysqli->query($query);
    header("Location: " . $_SERVER['REQUEST_URI']);
    exit();
}

EDIT: I realize now that I have wildly complicated my explanation here. 编辑:我现在意识到,我在这里疯狂地解释了我。 The main goal was to make the click of an <a> link trigger PHP code, with a variable specific to the link clicked. 主要目标是使<a>链接的点击触发PHP代码,并使用特定于所单击链接的变量。 (Each link is a delete button on a post, and each post has a UUID) (每个链接都是帖子上的删除按钮,每个帖子都有一个UUID)

If there is a way to alternatively trigger javascript code, that would be immensely helpful as well, since I'm looking to use such a method here too. 如果有一种方法可以替代地触发javascript代码,那也将非常有帮助,因为我也在这里使用这种方法。 I will likely be making a separate thread asking about this. 我可能会在一个单独的线程中询问这个问题。

You can use $_SESSION to delete the variable after for example 例如,可以使用$ _SESSION删除变量。

    if (isset($_SESSION['rm'])) # 'rm' contains the uuid of the entry to be deleted.
{
    $uuid = $_SESSION['rm'];
    unset($_SESSION['rm']); # Didn't expect this to work, of course it didn't remove the variable from the URL.
    $query = "DELETE FROM posts WHERE uuid = '$uuid'";
    $result = $mysqli->query($query);
    header("Location: " . $_SERVER['REQUEST_URI']);
    exit();
}

consider that you have register the value of the next shape. 考虑您已经注册了下一个形状的值。

$_SESSION['rm'] = "My value";

If your goal is to redirect to the current page but remove the query string, you can redirect to header("Location: ?"); 如果您的目标是重定向到当前页面但删除查询字符串,则可以重定向到header("Location: ?"); which is essentially just that. 本质上就是这样。 (Technically you are redirecting to a new query string with no value which is different than no query string at all but php will just show an empty array for $_GET which is essentially the same) (从技术上讲,您正在重定向到一个没有值的新查询字符串,该值与没有查询字符串完全不同,但是php只会显示$_GET的空数组,该数组本质上是相同的)

I was going to mention additional options like variables from $_SERVER , but many of those have various security or other issues associated with them. 我将要提到其他选项,例如$_SERVER变量,但是其中许多选项具有各种安全性或与之相关的其他问题。 I only mention this because I wouldn't suggest using any unless necessary. 我之所以仅提及这一点,是因为除非必要,否则我不建议使用任何方法。 Also, it really doesn't get easier than the above. 此外,它确实没有比上面更容易的了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM