【问题标题】:How to Prevent Access of website through direct URL wordpress?如何防止通过直接 URL wordpress 访问网站?
【发布时间】:2021-01-20 09:25:44
【问题描述】:

我有一个 WordPress 网站。当一些人可以登录并在本地系统中保存cookie之后,他们可以通过直接URL输入无需登录即可访问?

任何人都可以有一个想法吗?如何解决这个问题。

【问题讨论】:

    标签: wordpress password-protection urlaccess


    【解决方案1】:

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

    <?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() 或...)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2015-11-15
      相关资源
      最近更新 更多