如何为每个链接动态构建查询字符串?
我正在构建一种在我的网站上模拟某个用户的方法,类似于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 非常适合此类事情;为所有“a”标签创建一个选择器,并将查询字符串附加到 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,
不确定这是否是您正在寻找的答案,但它就是这样。我最近必须实现一组类似的功能。我将“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.
您应该检测它是否已设置,如果是,则将其附加到每个链接。
比如:
当然你不应该对每个链接进行检查,而应该将其设置为一个设置变量。还要进行一些安全检查,以便您确定它是有效的视图。
You should detect if it has been set, if so, append it for each link.
Something like:
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.