我有 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();
}
}
谢谢。