简体   繁体   English

隐藏在JavaScript代码中的URL(PHP生成的URL)

[英]Conceal URL in javascript code ( URL generated by PHP )

I am looking for a solution to the following: 我正在寻找以下解决方案:

I have a piece of JS code, that performs a redirection to a URL that is constructed with PHP, and that redirection is only done when the user presses a button on a confirmation dialog. 我有一段JS代码,该代码执行到使用PHP构造的URL的重定向,并且仅当用户按下确认对话框上的按钮时才完成重定向。

The code is, as follows: 代码如下:

function one() {
    window.location.replace("<?php 

if($new_redir == "1")  {

echo "$new_second_redirect_URL/?token=$hash";

}

else  {

echo "$second_redirect_URL/?token=$hash";

}

?>");
}

It works perfectly fine. 它工作得很好。 What I wanna do is conceal the URL that is displayed in the source code when a user opens the page. 我想做的是隐藏用户打开页面时在源代码中显示的URL。

What would be the best way to do that? 最好的方法是什么?

You're thinking too much into this to be honest. 老实说,您对此考虑太多。

If they want to avoid the confirmation screen and get the URL from the source, there's not really much you could do. 如果他们想避免出现确认屏幕并从来源获取URL,那么您实际上无能为力。

The best really is possibly performing an AJAX request on confirmation and getting a CSRF token based URL from the response and using that, but that could end up being overkill as well. 最好的办法实际上是对确认执行AJAX请求,并从响应中获取基于CSRF令牌的URL并使用它,但这也可能最终导致过度使用。

You could also make it into an actual <form></form> form with a few hidden fields (again, such as a CSRF token), and perform the post validation onclick. 您也可以将其变成带有几个隐藏字段(同样,例如CSRF令牌)的实际<form></form>表单,然后执行onclick验证后。 If it a success - redirect them. 如果成功-重定向他们。

UPDATE: 更新:

Use robots.txt to stop bots Build the QS with JS to stop most bots, something like: 使用robots.txt停止漫游器使用JS构建QS来停止大多数漫游器,例如:

var csrftoken='XJIWHEOU324uipHFOFUHR'; 
var url="http://url.com/page.php?token="; 
url=url+csrftoken;

What you could also do, is something like us actually, although for your use case it could be too much. 实际上,您也可以像我们一样做,尽管对于您的用例来说可能太多了。 Log every single page load into the DB, and check if if they're a first time visitor to the page after confirmation. 将每个页面加载记录到数据库中,并在确认后检查它们是否是该页面的首次访问者。

AJAX call (jQuery example): AJAX调用(jQuery示例):

$.post( "url_to_backend_page_to_get_url", {hasSubmittedForm:"true"}, function( data ) {
 window.location.href = data;
});

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

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