wordpress网站中所有页面的用户身份验证

时间:2014-08-10 23:07:12

标签: php wordpress

我想只为授权用户提供一个wordpress网站。我想在我的主题的头文件中添加自定义php代码,检查特定用户是否有权访问该页面,如果没有用户被重定向到自定义登录页面。我也在考虑在我的数据库中添加一个新表来获取用户信息。有插件可以帮我吗?我找不到任何。

3 个答案:

答案 0 :(得分:0)

将基本身份验证应用于wordpress站点所在的站点文件夹。

在这样的文件夹中创建.htaccess文件(但使用您自己的路径和用户名):

AuthUserFile /home/YOURACCOUNT/public_html/wordpress/.htpasswd
AuthGroupFile /dev/null
AuthName "Authentication"
AuthType Basic
<LIMIT POST GET>
require valid-user YOURUSERNAME
</LIMIT> 

然后创建一个.htpasswd文件,如下所示:

YOURUSERNAME:YOURPASSWORD_AS_MD5_ENCRYPTION

要将密码转换为MD5加密,请转到此处:

http://hash.online-convert.com/apache-htpasswd

答案 1 :(得分:0)

根据您的其他评论,尝试使用带有wordpress的插件,让您轻松完成工作。

https://wordpress.org/plugins/tags/user-registration

答案 2 :(得分:0)

好吧,有一些讨厌的黑客攻击我能够解决它。 在我的主题header.php文件中,我将以下代码行添加到顶部:

if(session_id() == '') {
  session_start();
}
if (!$_SESSION['authenticated']) {
  header('Location: '.get_site_url().'/login.php');
}

我在项目的根文件夹中创建了一个自定义的login.php文件,其中包含一个简单的登录表单,将其数据发布到另一个自定义文件&#34; webservice.php&#34;使用以下代码行:

//webservice.php
<?php
    if(session_id() == '') {
        session_start();
    }
    $username = $_POST['username'] ? $_POST['username'] : '';
    $password = $_POST['password'] ? $_POST['password'] : '';

    if ($password && $username) {
        $response = file_get_contents("http://example.com/api/Accounts?username=".$username."&password=".$password);
        $json = json_decode($response);

        if ($json->{'Status'} == "Success") {
            $_SESSION['authenticated'] = true;
            echo "<META http-equiv=\"refresh\" content=\"0;URL=http://example.com/\">";
        }
        else {
            $_SESSION['authenticated'] = false;
            echo "<META http-equiv=\"refresh\" content=\"0;URL=http://example.com/login.php?incorrect=yes\">";
        }
    }
    else {
        echo "<META http-equiv=\"refresh\" content=\"0;URL=http://example.com/\">";
    }

然而,在另一个自定义文件(logout.php)中,我只取消了我的会话变量:

<?php
if(session_id() == '') {
    session_start();
}
if ($_SESSION['authenticated']) {
    $_SESSION['authenticated'] = false;
    echo "<META http-equiv=\"refresh\" content=\"0;URL=http://example.com/\">";
}
?>

我很想知道我的解决方案的缺点......