我使用 wordpress 作为我的基础并制作了一个自定义登录表单。
阿贾克斯:
(function ( $ ) {
jQuery(document).ready(function() {
$("#login_form").submit(function(event){
//check the username exists or not from ajax
jQuery.ajax({
type: 'POST',
url: my_ajax.ajaxurl,
data: $('#login_form').serialize(),
cache: false,
//dataType: 'json',
success: function(result) {
var result=trim(result);
if( result == 'success' ){
window.location='/my-dashboard';
} else {
$('#message').html(result);
}
console.log(result);
}
});
return false;
});
});
function trim(str){
var str=str.replace(/^\s+|\s+$/,'');
return str;
}
}( jQuery ));
登录表单:
<p id="message" style="color:red;"></p>
<form method="post" action="" id="login_form">
<div align="center">
<div >
Email : <input name="email" type="text" id="email" value="" />
</div>
<div style="margin-top:5px" >
Password :
<input name="password" type="password" id="password" value="" />
</div>
<div class="buttondiv">
<input type="hidden" name="action" value="my_ajax_callback" />
<input type="hidden" name="func" value="check_login" />
<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>
函数.php
// Ajax
add_action( 'wp_ajax_nopriv_my_ajax_callback', 'my_ajax_callback' );
add_action( 'wp_ajax_my_ajax_callback', 'my_ajax_callback' );
// Your Login function
function check_login( $params ){
require_once('lib/hash.php');
$session = new SC_Session;
// now you can use $session here
$message=array();
if(isset($_POST['email']) && !empty($_POST['email'])){
mysqli_real_escape_string($mysqli, $params['email']);
}else{
$message[]='Please enter email';
}
if(isset($_POST['password']) && !empty($_POST['password'])){
$password= mysqli_real_escape_string($mysqli, $params['password']);
}else{
$message[]='Please enter password';
}
$countError=count($message);
if($countError > 0){
for($i=0;$i<$countError;$i++){
echo ucwords($message[$i]).'<br/><br/>';
}
} else {
$hc=$mysqli->query("SELECT password FROM %table% email='".$email."' AND active=1");
while($hp = $hc->fetch_object()) {
$stored_hash = $hp->password;
}
$hasherd = new PasswordHash(8, false);
$check = $hasherd->CheckPassword($password, $stored_hash);
if($check) {
//now validating the username and password
$result=$mysqli->query("SELECT id, first_name, last_name, zip, email, password FROM %table% WHERE email='".$email."' AND active=1");
while($row = $result->fetch_object()) {
//if username exists
if($result->num_rows > 0)
{
$date = date('Y-m-d h:i:s');
$update_sql = $mysqli->query("UPDATE %table% SET last_login='".$date."'");
$firstname = $row->first_name;
$lastname = $row->last_name;
$zip = $row->zip;
$user_id = $row->id;
$sex = $row->sex;
$session->set_userdata( 'user_id', $user_id );
$session->set_userdata( 'email', $email );
$session->set_userdata( 'firstname', $firstname );
$session->set_userdata( 'lastname', $lastname );
$session->set_userdata( 'zip', $zip );
$session->set_userdata( 'sex', $sex );
}
}
echo ucwords('success');
//return $params;
} else{
echo ucwords('please enter correct user details');
}
}
}
/**
* Ajax Submit
*/
function my_ajax_callback() {
$response = call_user_func($_POST['func'], $_POST);
header( "Content-Type: application/json" );
echo json_encode($response);
exit;
}
登录当前运行良好,但每当 $message 抛出错误时,它都会与标题警告一起显示。
警告:无法修改标头信息 - 标头已由 /%wordpresslocation%/wp-content/themes/%theme 中的(输出开始于 /%wordpresslocation%/wp-content/themes/%theme%/functions.php:77)发送第 86 行的 %/functions.php 空
回答 我觉得自己像个白痴,想通了,因为我精通 php,所以我一直在混合 php 和 javascript。
(function ( $ ) {
jQuery(document).ready(function() {
$('#message').slideUp();
$("#login_form").submit(function(event){
$('#message').slideUp();
//check the username exists or not from ajax
jQuery.ajax({
type: 'POST',
url: my_ajax.ajaxurl,
data: $('#login_form').serialize(),
dataType: 'json',
success: function(params) {
if( params == 'success' ){
$('#message').html(params).fadeIn();
document.location='/my-dashboard';
} else {
$('#message').html(params).fadeIn();
}
}
});
return false;
});
});
}( jQuery ));
并改变了
echo ucwords('success');
//return $params;
} else{
echo ucwords('please enter correct user details');
}
对此
$params = 'success';
return $params;
}else{
$params = 'fail';
return $params;
并将参数发回
function check_login( $params ){