板邓:wordpress自定义登录页面实现用户登录
首先检查用户是否已经登录,如果已经登录就返回info目录下的页面。
<?php global $current_user; $loginuserid = $current_user->ID; if($loginuserid){ //如果已经登录 header("Location:".get_bloginfo('url')."/info/"); exit; }
如果用户未登录;验证登录。
<? if($_POST) { $username = $_POST['username']; $password = $_POST['password']; //判断是否存在,存在则返回用户id $user_id = username_exists($username); if ($user_id){ //验证登录名是否正确 if(!user_pass_ok($username, $password)) { echo "<script language='javascript'>alert('帐号或者密码错误!');</script>"; echo "<script language='javascript'>location='". get_bloginfo('url')."/login/';</script>"; exit; } else { wp_set_auth_cookie($user_id,false,$secure); echo "<script language='javascript'>location='". get_bloginfo('url')."/info/';</script>"; exit; } }else{ echo "<script language='javascript'>alert('帐号或者密码错误!');</script>"; echo "<script language='javascript'>location='". get_bloginfo('url')."/login/';</script>"; exit; } } ?>
html表单代码:
<form action="#"method="post"> 用户名:<input type="text" name="username" ><br> 密码:<input type="password" name="password"><br> <input type="submit" name="submit"value="登录"> </form>
个人博客:http://www.qbeee.cn