如何为每个链接动态构建查询字符串?

发布于 2024-12-10 11:38:53 字数 625 浏览 0 评论 0原文

我正在构建一种在我的网站上模拟某个用户的方法,类似于 Facebook 现在通过“查看为”按钮在个人资料上进行的操作。

这里的目标是能够将我想要模拟的人的用户 ID 附加到查询字符串中,如下所示:viewas=1234。看到这一点后,该网站将允许我(作为管理员)“冒充”该用户。

然而,问题在于在页面加载之间携带“模拟”状态。我点击的每个链接都需要进行调整以携带 viewas=1234

例如,一个通常看起来像这样的链接...

<a href='http://www.example.com/profile?profileid=5678'>My Profile</a>

...必须动态地知道变成...

<a href='http://www.example.com/profile?profileid=5678&viewas=1234'>My Profile</a>

...每当我想冒充 ID 为 1234 的用户时。这必须发生站点范围广泛。

加载后使用 JS、在服务器端使用 PHP 还是其他方式执行此操作的最佳方法是?

I am building a method for impersonating a certain user on my website, similar to how Facebook now does it on the profiles with the "View As" button.

The goal here is to be able to append the user id of the person that I would like to impersonate to the query string like so: viewas=1234. Upon seeing that, the site would allow me (as an admin) to "impersonate" that user.

However, the problem comes with carrying the "impersonation" state between page loads. Each link that I click on will need to be adjusted to carry the viewas=1234.

For example, a link that would typically look like this...

<a href='http://www.example.com/profile?profileid=5678'>My Profile</a>

...would have to dynamically know to turn into...

<a href='http://www.example.com/profile?profileid=5678&viewas=1234'>My Profile</a>

...whenever I would like to impersonate the user with the id 1234. And this would have to happen site wide.

Is the best way to somehow do this with JS after the load, with PHP on the server side, or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-12-17 11:38:53

jQuery 非常适合此类事情;为所有“a”标签创建一个选择器,并将查询字符串附加到 href 属性。

例如,

$('a').each(function () {
  var href = $(this).attr('href');
  href += '?viewas=1234';
  $(this).attr('href',href);
});

jQuery is ideal for this sort of thing; create a selector for all 'a' tags, and append the query string to the href property.

For example,

$('a').each(function () {
  var href = $(this).attr('href');
  href += '?viewas=1234';
  $(this).attr('href',href);
});
青芜 2024-12-17 11:38:53

不确定这是否是您正在寻找的答案,但它就是这样。我最近必须实现一组类似的功能。我将“viewas”的值存储在会话变量中。这样,就不需要修改 HTML、javascript 等 - 只需修改您的代码(您已经在修改它以处理查询字符串) - 在代码中检查会话变量。

然后,当管理员从模拟中“注销”时,您可以取消设置此变量。

Not sure whether this is the answer you'd be looking for, however here it goes. I recently had to implement a similar set of functionality. I went with storing the value of "viewas" in the session variable. This way, there is no need to modify HTML, javascript, etc. - only your code (which you already are modifying anyway to handle the query string) - in the code check the session variable instead.

You can then unset this variable when the admin "logs out" from impersonation.

我很坚强 2024-12-17 11:38:53

您应该检测它是否已设置,如果是,则将其附加到每个链接。

比如:

if (isset($_GET['viewas'])){
   $linkurl .= '&viewas='.$_GET['viewas'];
}

当然你不应该对每个链接进行检查,而应该将其设置为一个设置变量。还要进行一些安全检查,以便您确定它是有效的视图。

You should detect if it has been set, if so, append it for each link.

Something like:

if (isset($_GET['viewas'])){
   $linkurl .= '&viewas='.$_GET['viewas'];
}

of course you should not do the check for every link, but make it a set variable. Also do some security checks so you know for sure it is an valid viewas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文