有什么方法可以绑定';任何';通过身份验证脚本重定向(链接/刷新等)


Is there any way to bind 'any' redirect (link/refresh/etc) through an authentication script?

上下文:我正在组建一个社交网络之类的东西,我希望能够在到达目的地之前通过将用户推到另一个脚本来验证任何刷新或重定向。。我曾想过使用"get"方法,但这需要我手动将每个链接设置为一个表单,无论是该表单还是某种ajax脚本,但我对ajax不太熟悉,不知道这是否完全可能。

我是否可以实现某种apache规则,将脚本应用于每次重定向/刷新?

平台:我使用的是php/mysql,在ajax/js 方面并不太差

原因:我希望能够验证用户的登录信息并跟踪他们的页面访问。(我确信后者一定与ajax有关(

欢迎任何想法

您可以使用Javascript(或jQuery(循环浏览页面上的所有链接,并添加指向处理程序函数的onClick属性。

<a href="www.example.com/somepage.html" onClick="tracker(this);">Go Here</a>

然后你可以定义一个自定义的跟踪器功能

function tracker(link){
  // Pass data to some php page with AJAX
  // Return true for success (user will move to the selected page)
  // Return false to cancel the click (will do nothing)
}

要使用jQuery设置它,它看起来像这样:

$('a').each(function(index) {
  var attr = $(this).attr('onClick');
  if (typeof attr === 'undefined' || attr === false){
    $(this).attr('onClick', 'tracker(this);');
  }else{
    // onClick already set
  }
});

您可能还可以使用jQuery的本机事件函数来绑定onClick事件。

在每个页面的顶部都包含一个脚本,该脚本使用会话检查登录信息(我希望你知道我在说什么(,如果会话不存在,它会将用户重定向到登录页面。在相同的脚本中,您还可以根据需要跟踪访问。