存储密码(unctions.php)

时间:2013-03-21 作者:HunterListInsight

我已经创建了一个密码表单,访问者可以使用所属密码访问任何受密码保护的帖子。基本上,你输入唯一的密码,你会被重定向到一个帖子,该帖子受到给定密码的保护。一切正常(在s\\u ha\\u dum的帮助下),除了一件事-您必须输入两次密码。

功能。php:

function doPasswordStuff(){
    if(isset($_POST[\'homepagepassword\'])){
        global $wpdb;
        $post_password = trim($_POST[\'passwordfield\']);
        $post_id = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_password = %s", $post_password) );
        if (!empty($post_id)) {
                wp_redirect(get_permalink($post_id));
                exit();
        } else {
            // oh dear, there isnt a post with this \'password\', put a redirect to a fallback here
            wp_redirect(xxx\');
            exit();
        }
        wp_reset_query();
    }
}
add_action(\'template_redirect\',\'doPasswordStuff\');
您知道如何在不输入两次密码的情况下执行此操作吗?

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

给你:

function dopasswordstuff(){
   if(isset($_POST[\'homepagepassword\'])) {
    global $wpdb;
    $post_password = trim($_POST[\'passwordfield\']);
    $post_id = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_password = %s", $post_password) );
    if (!empty($post_id)) {
      if ( empty( $wp_hasher ) ) {
        require_once( ABSPATH . \'wp-includes/class-phpass.php\' );
        // By default, use the portable hash from phpass
        $wp_hasher = new PasswordHash(8, true);
      }
      // 10 days
      setcookie( \'wp-postpass_\' . COOKIEHASH, $wp_hasher->HashPassword( stripslashes( $post_password ) ), time() + 864000, COOKIEPATH );
      wp_redirect(get_permalink($post_id));
    }
    exit;
  }
}
add_action(\'template_redirect\',\'dopasswordstuff\');
cribbed some stuff from the Core post login system 我相信我成功了。诀窍是设置cookie,这需要添加以下部分:

if ( empty( $wp_hasher ) ) {  
     require_once( ABSPATH . \'wp-includes/class-phpass.php\' ); 
     // By default, use the portable hash from phpass
     $wp_hasher = new PasswordHash(8, true);
}
// 10 days
setcookie( \'wp-postpass_\' . COOKIEHASH, $wp_hasher->HashPassword( stripslashes( $post_password ) ), time() + 864000, COOKIEPATH );

结束

相关推荐

从外部PHP脚本调用Functions.php中的函数

Preamble<我试图确保以前没有人问过这个问题。我刚开始使用StackExchange,所以我可能没有这么做。我不介意责备;厚皮肤,我学得很快:)THE PROBLEM我编写了一个函数并将其放置在主题函数中。phpfunction serverConnect() { $serverAccess = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$serverAccess) { die(\"