2

目前我正在使用Screets Wordpress 会话插件并使用自定义登录表单 [因为我不想使用 WP_users 数据库和 Wordpress 的用户管理员。这背后有我自己的原因。]

我的登录设置工作得很好,除了 Wordpress 删除会话这一事实,而且我不想在更新期间冒着风险破解核心文件。以下是我的设置:

登录表格:

<form method="post" action="" id="login_form">
<div align="center">
<div >
   User Name : <input name="username" type="text" id="username" value="" maxlength="20" />
</div>
<div style="margin-top:5px" >
   Password :
    &nbsp;&nbsp;
    <input name="password" type="password" id="password" value="" maxlength="20" />
</div>
<div class="buttondiv">
    <input name="Submit" type="submit" id="submit" value="Login" style="margin-left:-10px; height:23px"  /> <span id="msgbox" style="display:none"></span>
</div>
</div>
</form> 

Ajax 脚本

<script>
$("#login_form").submit(function(event){

    //remove all the class add the messagebox classes and start fading
    $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
    //check the username exists or not from ajax
    $.post("<?php echo get_stylesheet_directory_uri(); ?>%page%.php",{ username:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
      if(data=='yes') //if correct login detail
      {
        $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
        {
          //add message and change the class of the box and start fading
          $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
                  function()
          {
             //redirect to secure page
             document.location='/my-dashboard';
          });
        });
      }
      else
      {
        $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
        {
          //add message and change the class of the box and start fading
          $(this).html('Your login detail sucks...').addClass('messageboxerror').fadeTo(900,1);
        });
          }
       });
       return false;//not to post the  form physically
});
</script>

数据库链接

$username= mysql_escape_string(do something to username);
$pass= mysql_escape_string(do something to pass);

//now validating the username and password
$sql="SELECT username, password FROM %table% WHERE username='".$username."' AND active='1'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result); 

//if username exists
if(mysql_num_rows($result)>0)
{
  //compare the password
  if(strcmp($row['password'],$pass)==0)
  {
    echo "yes";

    //now set the session from here if needed
    $array = array(
    'username' => $row['username']
    );
    $session->set_userdata( $array );
    //$_SESSION['username']=$username;
    //$_SESSION['loggedIn'] = 1;

  }
  else
    echo "no";
}
else
  echo "no"; //Invalid Login

代码工作得很好,除了一点

$array = array(
        'username' => $row['username']
        );
        $session->set_userdata( $array );

这是使用插件设置会话的唯一方法,但我收到错误“调用非对象上的成员函数 set_userdata()”。是否有另一种方法可以使用我当前的设置或正确启用默认 PHP 会话的方法?谢谢。

4

1 回答 1

1

我认为你必须使用

$sql="SELECT username, password FROM %table% WHERE username='".$username."' 
  AND active=1"; 

if(is_object($session))  $session->set_userdata( $array );

http://codecanyon.net/item/wordpress-sessions-plugin-with-database/3405722

于 2012-11-19T06:00:08.013 回答