2016-04-26 21 views
0

私はPHPを使用して作成している管理サイトにワードプレス認証を使用しようとしています。私は管理者の役割を持つユーザーのみがこのサイトにログインできるようにします。ユーザーが管理者であることを確認しながらワードプレス認証を使用する

if (in_array('administrator', (array) $user->roles)) { 
    echo "<script type='text/javascript'>alert('User is an admin!')</script>"; 
    if(!wp_authenticate($user, $pass)){ 
     echo "<script type='text/javascript'>alert('Wrong password!')</script>"; 
    }elseif(!wp_authenticate($user, $pass)){ 
     echo "<script type='text/javascript'>alert('Correct password!')</script>"; 
    } 
}else{ 
    echo "<script type='text/javascript'>alert('You're not an admin!')</script>"; 
} 

そして、これは私がユーザーIDを取得するには、DBを照会する方法である:これは私が持っているものである

$link = mysqli_connect($host, $username,$password ,$db); 
$q1 = " SELECT ID FROM wp_users WHERE 
    user_email=?"; 
$stmt = mysqli_prepare($link, $q1); 
if($stmt){ 

     mysqli_stmt_bind_param($stmt, "s", $email); 
     mysqli_stmt_execute($stmt); 
     mysqli_stmt_bind_result($stmt, $id); 
     mysqli_stmt_fetch($stmt); 
     //echo $id; 
     $user = get_userdata($id); 

} 

私は警告を取得し、「ユーザーが管理者である」と、それは空白を示しページ。私は間違って何をしていますか?

答えて

2

まず:この質問はに関連しているCheck if user is an admin by username or email only

第二:あなたの最初のブロック内のロジックは、これが

使用WordPressのログインワークフロー

あなたがすべきのようになりますは何かということです...非常に奇妙です常にWordPressのログインフォームと認証ワークフローを使用してください。これは、使用して、独自のログインページをローリングすると、あなたがそれを行う場合を除き、アプリケーション非常に危険を作るためにそれは難しいことではありません正確に正しい

デフォルトのWordPressのログインフローを使用するには、ダッシュボードの一般的なPHPファイルでこのようなものを置きます

<?php 
require_once 'path/to/wp-load.php'; 
// Before every page load 
if (! is_user_logged_in()) { 
    // Not logged in at all... redirect to WP Login Page 
    auth_redirect(); 
    exit(); // Exit to be safe 
} elseif (! in_array('administrator', wp_get_current_user()->roles)) { 
    wp_logout(); // Destroy their login session 
    wp_die('You must be an administrator'); // Die with a nice error page 
} 

私はこのワークフローを説明してみましょう:

まず、我々はwp-load.phpをインポートします。

ユーザーがhttps://example.com/my-admin/index.phpをロードすると、common.phpは最初にユーザーがログインしているかどうかを確認します。If If ないそれはWP-ログインページを使ってログインしますhttps://example.com/wp-login.php?return_url=https%3A%2F%2Fexample.com%2Fmy-admin%2Findex.php

ユーザーにユーザーをリダイレクトしますauth_redirect()して終了を呼び出しますし、彼らはあなたのcommon.phpが今is_logged_in()が真として認識されるhttps://example.com/my-admin/index.phpに戻ってリダイレクトされます...次のelseifに移動して、ユーザーが管理者であるかどうかを確認します。そうでなければ、認証セッションを終了し、wp_dieで失敗します。認証セッションを終了し、ページをリロードするとログインページに戻って管理者の資格情報を入力し、wp_dieページが繰り返し表示されないようにします。 (wp-admin /にリダイレクトするか、またはwp_dieで正規/ wp-admin /のリンクを提供する)

もしそうなら、これを調整することができます管理者の役割があれば、要求の実行は続行され、ダッシュボードにアクセスできます。

この機能を利用するには、WordPressのインストールと同じドメインでダッシュボードを実行する必要があります。そうしなければ、クッキーは転送されません。

あなた自身のログインページをロールします。これは悪い練習です

難しいです...ログインのほかに、あなたが他のページを読み込む前に、この例では、

<?php 
// Your login.php page 

// Really, no really, this should just be done with WordPress's default login page with an auth_redirect() 
// Really this is not recommended, it's really bad practice, and much less secure 
// You've been warned 

// If they provided a username and password, then check them 
if (isset($_POST['username']) && isset($_POST['password'])) { 
    $username = $_POST['username'] 
    $username = $_POST['password'] 
    // Check the username and password with WordPress to see if they are for any user 
    $user = wp_authenticate($username, $password); 
    $error = ''; 
    if (is_a($user, 'WP_User')) { 
     // Verify that the user is an admin 
     if (in_array('administrator', (array) $user->roles)) { 
      // Redirect them to the dashboard 
      wp_redirect('dashboard.php'); 
      exit(); 
     } else { 
      $error = "Correct password, but not an Admin : ("; 
     } 
    } else { 
     $error = "Wrong password :("; 
    } 
} 
// The Login Page 
?> 
<html> 
<head> 
    <title> My Login </title> 
</head> 
<body> 
    <form action="login.php" method="POST"> 
    <?php if ($error): ?> 
     <div class="error"><?php echo $error; ?></div> 
    <?php endif; ?> 
    <label> 
    Username: 
     <input type="text" name="username" placeholder="username" required /> 
    </label> 
    <label> 
    Password: 
     <input type="password" name="password" placeholder="password" required /> 
    </label> 
    </form> 
</body> 
</html> 

(デフォルトのコアまたは追加のプラグインのいずれかから)標準のログインフォームを取得しますナンス、ハニーポット、またはその他のセキュリティ機能が含まれていません。あなたはこのコードを持っているはずです

<?php 
if (! is_user_logged_in() || ! in_array('administrator', wp_get_current_user()->roles)) { 
    wp_redirect('login.php?error=Please%20Login'); 
    exit(); 
} 
// now you can do your stuff 
+0

大丈夫ですので、どうすれば私のサイトのログインページとしてWordPressのデフォルトのログインページを使用できますか? –

+0

私は私の答えでそれを使用する方法の概要を説明した、カスタムダッシュボード/管理の各ページに含まれている共通のPHPファイルが必要です。これはあなたがとにかく持っているべきものでなければなりません。そのファイルでは、私は – edhurtig

+0

大丈夫、私はあなたの答えの最初のブロックを追加した共通のPHPに追加されたコードのようなものを持っている必要がありますが動作していないようです。そのコードでは、「資格情報が正しい場合は、私の管理サイトに行きます」とはどこですか? –

関連する問題