如何防止通过直接 URL wordpress 访问网站?

问题描述

我有一个 wordpress 网站。有些人什么时候可以登录并将cookies保存在本地系统中,然后他们可以通过直接输入URL而无需登录即可访问?

谁能有想法?如何解决这个问题。

解决方法

您可以设置一组允许的裁判网址。如果我们的需求不是来自这些推荐人之一,那么我们会重定向用户。

<?php
add_action( 'wp',function() {
  if( ! is_admin() && ! is_home() || ! is_front_page() ) {
    $base = [ // ... allowed referees
      'https://github.com/',// ... referee 1
      'https://stackoverflow.com/',// ... referee 2
      // ... etc.
    ];
    if( ! in_array( $_SERVER['HTTP_REFERER'],$base ) ) { // ... if not in referees
      header( 'Refresh: 3; ' . esc_url( home_url() ) ); // ... automatically redirect user after 3 seconds
      wp_die( 'Something went wrong.',NULL,$args = array( 'back_link' => true,) ); // ... kills Wordpress execution and displays an HTML page with an error message
      exit; // ... terminate the current script
    };
  };
} ); ?>

这应该会阻止直接访问。无论是输入网址还是将其加入书签。访问它的唯一方法是通过裁判页面上的链接。

您不希望整个网站都无法访问,请不要忘记进一步限制条件语句(例如:is_page() 或 ...)。