0

我有 Wordpress 网站,我在其中设置了自己的表来存储用户登录凭据。人们可以很好地注册,但是当需要登录并创建会话时,它似乎不起作用。

这是在我的标题中:

<?php
// Initialize session
session_start();
// Initialize DB
include("functions/connect.php");
<span class="login">
      <?php  
        if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))  
        { 
      ?>
        <p>Welcome, <?php echo $_SESSION['Username']?></p>
      <?php 
        }
        else
        {
      ?>
        <p>Login</p><p>|</p><p>Signup</p>
      <?php } ?>
</span>

这是在我的登录页面上。

<?php 
    if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['password']) && !empty($_POST['password']) AND isset($_POST['name']) && !empty($_POST['name'])){
                $username = mysql_escape_string($_POST['name']);
                $password = mysql_escape_string(md5($_POST['password']));

                $search = mysql_query("SELECT username, password, active FROM wp_thread_users WHERE username='".$username."' AND password='".$password."' AND active='1'") or die(mysql_error()); 
                $match  = mysql_num_rows($search);

                if($match > 0){
                    $msg = 'Login Complete! Thanks';
                    //$email = $row['EmailAddress'];  
                    $_SESSION['Username'] = $username;  
                    //$_SESSION['EmailAddress'] = $email;  
                    $_SESSION['LoggedIn'] = 1;  
                }else{
                    $msg = 'Login Failed!<br /> Please make sure that you enter the correct details and that you have activated your account.';
                }
            }


        ?>
        <!-- stop PHP Code -->

        <!-- title and description -->  
        <h2>Login Form</h2>
        <p>Please enter your name and password to login</p>

        <?php 
            if(isset($msg)){ // Check if $msg is not empty
                echo '<div class="statusmsg">'.$msg.'</div>'; // Display our message and add a div around it with the class statusmsg
            } ?>

        <!-- start sign up form --> 
        <form action="" method="post">
            <label for="name">Username:</label>
            <input type="text" name="name" value="" />
            <label for="password">Password:</label>
            <input type="password" name="password" value="" />

            <input type="submit" class="submit_button" value="login" />
        </form>

我的代码可以在没有 Wordpress 的常规 php 页面上运行,但是一旦我登录 Wordpress 站点,它就会将我重定向到“wp-login.php”,它也从不存储会话。有什么帮助吗?

编辑:

忘了补充这是我的functions.php

add_action('init', 'initEverything');

function initEverything() {
    if(!session_id()) {
        session_start();
    }
}

谢谢。

4

2 回答 2

0

I know it's really annoying when people ask questions like this rather than help, but why aren't you just using WordPress's wp_users table? Then you can just log someone in with one line function wp_signon:

http://codex.wordpress.org/Function_Reference/wp_signon

You might be creating extra work for yourself by not using WordPress's out-of-the-box user management stuff.

EDIT: Given our comments below, here's some pointers on simple ways to do what you're trying to do using WordPress's wp_users. I've done exactly what you're trying to achieve on other sites and I'd highly recommend not creating a custom user table.

Custom user meta: http://codex.wordpress.org/Function_Reference/add_user_meta

Prevent users accessing wp-admin (one of many many simple ways of doing this): http://pento.net/2011/06/19/preventing-users-from-accessing-wp-admin/

I'm actually writing a blog post on how to do this at the moment so I'll let you know once it's up.

于 2012-11-15T15:31:03.373 回答
0

我知道这现在已经过时了,但我终于创建了一个方法来做我认为你正在尝试做的事情。我可以充实一些内容,例如添加用户元数据,但我希望得到您的反馈并知道它是否对您有帮助:

http://haveposts.com/2013/01/how-to-create-a-really-simple-custom-wordpress-login-and-sign-up-without-plugins/

这是博客的第一篇文章,因此欢迎您提出任何其他想法。

干杯,

杰克

于 2013-01-25T01:29:47.670 回答