就字preSS网站登录工作太 [英] Make website login work on WordPress too

查看:178
本文介绍了就字preSS网站登录工作太的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发使用PHP和MySQL网站,已经有一个登录和注册表格。 ( myweb.com

I developed a website using PHP and MySQL, which already has a login and registration form. (myweb.com)

我在这个网址添加的单词preSS它 myweb.com/blog

I've added wordpress to it at this url myweb.com/blog

我要禁用词preSS和强制用户登录和注册页面中使用我的。基本上我的集成与Word preSS登录,这样用户将在这两个网站登录。

I want to disable the login and registration page on WordPress and force users to use mine. Basically integrate my login with WordPress so that user will be logged in on both sites.

我的网站成员表看起来像这样。和所有注册用户都存储在这里。而在我的数据库密码散列使用的MD5()

My site members table looks like this. And all registered users are stored here. And passwords in my DB are hashed using md5()

id | name | email | password

和字preSS的结构是这样的,目前是空的。

and WordPress structure is like this and is currently empty

ID | user_login | user_pass | user_nicename | user_email | user_url | user_registered | user_activation_key | user_status | display_name

我尝试以下步骤<一个href=\"http://word$p$pss.stackexchange.com/questions/8887/sso-authentication-integration-with-external-directory-service\">mentioned这里

但我得到的线这个错误254 的var_dump($用户);

but I get this error on line 254 var_dump($user);

    object(WP_Error)#620 (2) {
  ["errors"]=>
  array(1) {
    ["invalid_username"]=>
    array(1) {
      [0]=>
      string(166) "<strong>ERROR</strong>: Invalid username. <a href="http://localhost/dev/blog/wp-login.php?action=lostpassword" title="Password Lost and Found">Lost your password</a>?"
    }
  }
  ["error_data"]=>
  array(0) {
  }
}

在我的网站

此外,所有的用户信息都存储在我的成员表无法在Word preSS的分贝。

Also, all user info is stored in my members table on my site not in WordPress's db.

下面是我的登录code为我的网站,我最近增加了话语preSS登录它。

Here is my login code for my site, which I recently added the WordPress login to it too.

/*
 *  Login
 *
 *  $email = email of user
 *  $pass = user password (must already be in md5 form)
 *  $url = url of page they are login from
 */
function login($email = '', $pass = '', $url = '', $sticky = false)
{
        global $lang, $_db, $mod, $template_style;

        // Replace nasty things to stop sql injection
        $email = addslashes(strtolower($email));
        $email = strip_tags($email);
        $email = htmlspecialchars($email, ENT_QUOTES);

        //get user id
    $sql = "SELECT `id`, `name`, `username`
                FROM `members`
                WHERE `email`='".mysql_real_escape_string($email)."'
                AND `pass` = '" . mysql_real_escape_string($pass) . "'
                LIMIT 0,1";

        $q = $_db->query($sql);
        list($uid, $name, $username) = $_db->fetch_array($q);

        $login_check = $_db->num_rows($q);

        if ($login_check <= '0')  //check if login matches
        {
                  echo '0'; //login failed
                  die;
        }

        /*
         * wordpress login
         * 
         * read:
         * http://codex.wordpress.org/Function_Reference/wp_update_user
         */ 
         $credentials = array();
         $credentials['user_email'] = $email;
         $credentials['user_password'] = $pass;
         $credentials['remember'] = $sticky; // true/false
         $secure_cookie = false; // true / false

         $user = wp_authenticate($credentials['user_email'], $credentials['user_password']);

    if ( is_wp_error($user) ) {
        if ( $user->get_error_codes() == array('empty_email', 'empty_password') ) {
            //$user = new WP_Error('', '');
            $user = wp_update_user(array ( 'user_login' => $name, 'user_email' => $email, 'user_pass' => $pass ));
        }
    }

        var_dump($user);

        wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
        do_action('wp_login', $user->user_login, $user);

        /*
            set login cookies
        */
        set_login_cookie($uid, $pass, $sticky);

        //lock check
        lock_checker($uid);

        update_thisuser_online();           
}

我必须一切从我的成员表复制并填充到 wp_users 还是有办法登录到字preSS,而无需在2个不同的表中重复数据?我不希望有2登录和2登记表上的两个站点。

Do I have to copy everything from my members table and populate it into wp_users or is there a way to login into wordpress without having duplicate data in 2 different tables? I don't want to have 2 logins and 2 registration forms on both sites.

为什么不 wp_authenticate()在我的code以上的身份验证?

Why won't wp_authenticate() in my code above authenticate?

推荐答案

您可以设置文字preSS登录通过编辑config.php文件并添加以下两行使用自定义表:

You can set the wordpress login to use a custom table by editing the config.php and adding these two lines:

define('CUSTOM_USER_TABLE','new_user_table'); //login, pass, email etc
define('CUSTOM_USER_META_TABLE', 'new_usermeta_table'); //optional bio, don't have to include this line

在哪里new_user_table是您的网站的桌子和new_usermeta_table是您的网站的生物表(如果你想要一个)

Where new_user_table is your website's table and new_usermeta_table is your website's bio table (if you want one)

定制表需要有相同的结构作为一个正常的字preSS表。因此,要获得与您现有的网站的餐桌这个工作,你必须添加一些字段,并确保密码被散列以同样的方式。

The custom table needs to have the same structure as a normal wordpress table. So, to get this working with your existing website's table you'll have to add some fields and make sure the password is hashed the same way.

下面是如何构造的用户表

下面是如何构建用户表元

要正确地散列在注册密码,包括文件WP-包含/ pluggable.php并使用搜索​​功能
&LT; PHP $哈希= wp_hash_password($密码)&GT;

To hash the passwords correctly at registration, include the file wp-includes/pluggable.php and use the function
<?php $hash = wp_hash_password( $password ) ?>

对于现有未正确散列密码,你必须建立一个电子邮件密码重置。

For existing passwords that are not hashed correctly, you'll have to set up an email password reset.

或者。如果您想保留您当前密码的哈希值(不推荐出于安全原因,但可行的),你可以改字preSS哈希函数。在WP-包括/ pluggable.php变化:

Or. if you'd like to retain your current password hashes (not recommended for security reasons but doable) you can change the wordpress hashing function. In wp-includes/pluggable.php change:

if ( !function_exists('wp_hash_password') ){
    function wp_hash_password($password) {
                //apply your own hashing structure here
            return $password;
    }
}

和变化:

if ( !function_exists('wp_check_password') ){
    function wp_check_password($password, $hash, $user_id = '') {
            //check for your hash match
            return apply_filters('check_password', $check, $password, $hash, $user_id);
            }
}

有关wp_check_password 去这里

For details on wp_check_password Go Here

或者

您可以跳过与您的自定义用户表乱搞,并有字preSS登录应用到您的网站的其余部分。要做到这一点,简单的​​使用下面的code:

You can skip messing around with your custom user table and have the wordpress login apply to the rest of your site. To do this, simple use the following code:

<?php
include 'wp-config.php';
if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    header( 'Location: http://google.com' ) ;
};
?>

确保的wp-config.php文件是对文件的完整相对路径,然后将在每一页这code你的非字preSS站点。替换任何内容将要被显示为登录用户的回波,并与任何要显示的位替换首部。如果内容是简单的HTML你可以做到以下几点:

Make sure that 'wp-config.php' is the full relative path to the file, then place this code in every page on your non wordpress site. replace the echo with whatever content is to be displayed for a logged in user, and replace the header with whatever is to be displayed for a guest. If the content is simple html you can do the following:

<?php
include 'wp-config.php';
if ( is_user_logged_in() ) {
?>

<html>
<head></head>
<body><p>Welcome Registered user</p></body>
</html>

<?php
} else {
?>

<html>
<head></head>
<body><p>Please log in</p></body>
</html>

<?php
};
?>

这篇关于就字preSS网站登录工作太的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆