Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用wp_set_auth_cookie而不使用任何钩子_Php_Wordpress_Wordpress Theming_Instagram Api_Wordpress Login - Fatal编程技术网

Php 使用wp_set_auth_cookie而不使用任何钩子

Php 使用wp_set_auth_cookie而不使用任何钩子,php,wordpress,wordpress-theming,instagram-api,wordpress-login,Php,Wordpress,Wordpress Theming,Instagram Api,Wordpress Login,Wordpress版本4.7.5 我正在从Instagram注册新用户,我已成功注册新用户。注册后,我尝试登录用户 我可以用钩子,但我想用比脏钩子更好的解决方案 因为那将是完全无用的 然后 add_action( 'wp', 'login_current_user' ); function login_current_user(){ if ( is_page() && get_the_id() == 863 ){ if ( isset( $_GET['id

Wordpress版本4.7.5

我正在从Instagram注册新用户,我已成功注册新用户。注册后,我尝试登录用户

我可以用钩子,但我想用比脏钩子更好的解决方案 因为那将是完全无用的

然后

add_action( 'wp', 'login_current_user' );
function login_current_user(){
    if ( is_page() && get_the_id() == 863 ){
        if ( isset( $_GET['id'] ) ){
            if ( !is_user_logged_in() ) {
                $user_id = $_GET['id'];
                $user = get_user_by( 'id', $user_id ); 
                wp_clear_auth_cookie();
                wp_set_current_user( $user_id, $user->user_login );
                wp_set_auth_cookie( $user_id, true );
                do_action( 'wp_login', $user->user_login );
                if ( is_user_logged_in() ){
                    $redirect_to=site_url() . '/profile';
                    wp_safe_redirect($redirect_to);
                    exit();
                }
            }
        }
    }
}
add_action( 'wp', 'login_current_user' );
    function login_current_user(){
        if ( is_page() && get_the_id() == 863 ){
            if ( isset( $_GET['id'] ) ){
                if ( !is_user_logged_in() ) {
                    $user_id = $_GET['id'];
                    $user = get_user_by( 'id', $user_id );
                    if ( $_GET['token'] == get_user_meta( $user_id, 'temporary_token', true ) ){
                        delete_user_meta( $user_id, 'temporary_token', $_GET['token'] ); 
                        wp_clear_auth_cookie();
                        wp_set_current_user( $user_id, $user->user_login );
                        wp_set_auth_cookie( $user_id, true );
                        do_action( 'wp_login', $user->user_login );
                        if ( is_user_logged_in() ){
                            $redirect_to=site_url() . '/profile';
                            wp_safe_redirect($redirect_to);
                            exit();
                        }
                    }
                }
            }
        }
    }
玩弄肮脏的把戏。如果
id
作为get参数传递 存在于wp_users表中,则不会记录任何验证 在

更新

在重定向到配置文件页面之前,我创建了一个user_meta,在验证之后,我登录用户并删除了usermeta,就像OTP一样。如果可能的话,试着让它变得更好

代码如下:-

if ( $wpdb->insert_id ){  //that is registration with instagram
    $temporary_token = sha1(rand());
    update_user_meta( $wpdb->insert_id, 'temporary_token', $temporary_token);
   ?>
    <script>jQuery(document).ready(function(){ window.location.href = "<?php echo get_bloginfo('url') . '/profile/?id=' . $wpdb->insert_id . '&token=' . $temporary_token; ?>";});</script>
   <?php
}

如果在调用
wp\u clear\u auth\u cookie()
wp\u set\u auth\u cookie()
之前发送了头,则两者都不起作用,因为它们都依赖于PHP的
setcookie
函数。
after_setup_主题
操作将在发送头之前发生,因此挂接并在那里设置cookie应该可以解决问题

function myPlugin_createUser(){
  // Probably need to put code that creates user here too otherwise $user_created will never return true
  if( $user_created ) {
      $new_user = get_user_by( 'id', $user_created ); 
      if ( !is_user_logged_in() ) {
          wp_clear_auth_cookie();
          wp_set_current_user( $user_created, $new_user->user_login );
          wp_set_auth_cookie( $user_created, true );
          do_action( 'wp_login', $new_user->user_login );
      }
  } 
}

add_action( 'after_setup_theme', 'myPlugin_createUser' );
更新:

如果在触发
createUser
函数之前输出视图元素,它将无法工作,因此我们需要确保它们完全独立运行。下面我给出了一个简单的例子来说明如何实现这一点——我们向视图输出一个链接,该链接传递一个GET参数,该参数由插件拾取,并在呈现任何内容之前运行操作,从而允许我们设置cookie

my plugin.php

// Note: you'll need to include a completed version of myPlugin_createUser() function as above for these examples to work
add_shortcode( 'my-plugin', function() {  
   // I've used a GET request for simplicity- a form + POST request may be more practical for your purposes and will prevent caching issues.
   echo "<a href='?my_plugin_login_user=1'>Login</a>";
});

// Check for our GET parameter 
if( isset( $_GET['my_plugin_login_user'] ) ) {
  // Nothing should be rendered yet as plugin template hasn't even started loading yet so cookies should set fine
  add_action( 'plugins_loaded', 'myPlugin_createUser' ); 
}
<div> ...
    <?php 
        do_shortcode( 'my-plugin' ); 
    ?>
</div>
$result将是JSON响应的对象表示形式:

{
"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
"user": {
    "id": "1574083",
    "username": "snoopdogg",
    "full_name": "Snoop Dogg",
    "profile_picture": "..."
  }
}

从这里,我们只需创建用户/使用WordPress进行身份验证,重定向到所需页面/呈现模板(如果通过条件挂钩处理,则无需重定向),并在需要时恢复页面状态

如果在
wp\u set\u auth\u cookie()
之前发送头,则cookie可能不会被设置。如果可以钩住init操作
add_操作('init','my_操作')
并在那里打电话可能会解决问题。非常欢迎:)很抱歉,您不能使用
init
?还是说
init
不起作用?如果输出缓冲或
init
操作都不起作用,那么可能会出现一些输出,或者对
add\u action
的调用发生得太晚,因此要解决这个问题,我们可能需要更多地了解如何/从何处调用它,以及可能需要更大的代码段,因为可能没有设置cookie的另一个原因是如果
$user\u created
的计算结果为
false
。我认为您可能必须将代码移动到
函数中。php
或在插件中,然后使用
$\u GET
$\u POST
变量触发它,或者注册一个自定义URL,然后重定向到该URL执行操作。操作需要在任何输出之前被钩住,否则它们将不起作用,所以在模板中这样做在很多时候都是不好的。取决于你是如何和在哪里做的-
在设置之后\u theme
是主题可用的最早的钩子,并且发生在auth之前,因此这对你可能也有用。是的,通过POST传递它会更安全,或者在创建时生成一个用于登录的一次性或有时间限制的代码会更好。使用用户id通过get请求登录将非常不安全,因为攻击者可以通过浏览器历史记录登录,或者只是“暴力强制”使用有效id。使用Instagram认证API可能还有更好的方法,虽然我有点太累了,有点头痛,但很抱歉,我现在还没有完全适应。我已经很感谢你的努力,我的朋友,我也在努力想办法解决这个问题,因为它在未来也会很有用。您的个人资料中没有任何社交网络或github,我正在考虑连接。wordpress最好使用wp_remote_post和wp_remote_get。我还有一个问题,所以我不认为在这里解释这个过程有任何必要。
<div> ...
    <?php 
        do_shortcode( 'my-plugin' ); 
    ?>
</div>
$uri = 'https://api.instagram.com/oauth/access_token'; 
$data = [
    'client_id' => '213esdaedasdasd12...YOUR_CLIENT_ID', 
    'client_secret' => 'a8b4aaf06c0da310...YOUR_CLIENT_SECRET', 
    'grant_type' => 'authorization_code', 
    'redirect_uri' => 'http://www.YOUR_REDIRECT_URL.it',      // The exact redirect URL as used previously
    'code' => $_GET['code']  
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri); // uri
curl_setopt($ch, CURLOPT_POST, true); // POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // POST DATA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN RESULT true
curl_setopt($ch, CURLOPT_HEADER, 0); // RETURN HEADER false
curl_setopt($ch, CURLOPT_NOBODY, 0); // NO RETURN BODY false / we need the body to return
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // VERIFY SSL HOST false
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // VERIFY SSL PEER false
$result = json_decode(curl_exec($ch));
{
"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
"user": {
    "id": "1574083",
    "username": "snoopdogg",
    "full_name": "Snoop Dogg",
    "profile_picture": "..."
  }
}