如何使用$_SESSION将wordpress上的"登录"更改为"注销"按钮



所以我在Wordpress上有一个自定义的登录页面,它连接到我的用户数据库,并检查所有信息是否正确。这是login.php:


<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_POST['email'])){
// removes backslashes
$email = stripslashes($_REQUEST['email']);
//escapes special characters in a string
$email = mysqli_real_escape_string($conn,$email);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($conn,$password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE email='$email'
and password='".md5($password)."'";
$result = mysqli_query($conn,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['email'] = $email;
// Redirect user to index.php
header("Location: index.php");
}else{
echo "<div class='form'>
<h3>Email/password is incorrect.</h3>
<br/>Click here to <a href='../login/'>Login</a></div>";
}
}else{
?>
<div class="form">
<!-- <h1>Log In</h1> -->
<form action="" method="post" name="login">
<input type="text" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<br>
<input name="submit" type="submit" value="Login" />
</form>
<p>Not registered yet? <a href='../register/'>Register Here</a></p>
</div>
<?php } ?>
</body>
</html>

我想做的是在用户登录后,将Wordpress标题上的LOGIN按钮更改为LOGOUT(如果可能的话,显示用户信息(,我想我可以使用$_SESSION['email'] = $email;变量来实现这一点。

我该怎么做?

非常感谢!

您可以使用内置的WordPress函数is_user_loged_in((,或者您的登录是否也使用数据库中的自定义表,而不是WordPress用户表wp_user?

<?php
if ( is_user_logged_in() ) {
echo '<a href="../wp-login.php?action=logout">Login out</a>';
} else {
echo '<a href="../login/">Login</a>';
}
?>

如果你的登录系统独立于WordPress,你需要检查你的登录功能,看看它创建了什么会话变量,你可能还需要自己启动会话,如果它不在功能中,那么

session_start();
if (isset($_SESSION['email'])) {
/// your login button code here
} else {
/// your logout button code here
}

一个将其添加到wordpress菜单中的功能,您需要对其进行样式设置:

add_filter('wp_nav_menu_items', 'button_login_logout', 10, 2);
function button_login_logout() {
ob_start();
if (isset($_SESSION['email'])) : 
?>
<a role="button" href="logoutlink">Log Out</a>. 
<?php 
else : 
?>
<a role="button" href="loginlink">Log In</a> 
<?php 
endif;

return ob_get_clean();
}

最新更新