我正在创建一个注册页面,用户只能在其中选择可用的用户名,即数据库中不存在的用户名.所以我为此使用ajax ..但是当我一次输入太多请求时,它失败了,我的网站停止工作..一段时间后,一切都会好起来的,但5-10分钟后它将停止工作.
网站提供错误:-
此页面无法正常运行.collegespike.com没有发送任何数据.
ERR_EMPTY_RESPONSE
从数据库获取数据的代码:
<?php
if (isset($_POST['user_name']) && $_POST['user_name'] != '') {
$response = array();
$username = mysqli_real_escape_string($db, $_POST['user_name']);
$sql = "select username from login_master where username='" . $username . "'";
$res = mysqli_query($db, $sql);
$count = mysqli_num_rows($res);
if ($count > 0) {
$response['status'] = false;
$response['msg'] = 'Username already exists.';
} else if (strlen($username) < 6 || strlen($username) > 15) {
$response['status'] = false;
$response['msg'] = 'Username must be 6 to 15 characters';
} else if (!preg_match("/^[a-zA-Z1-9]+$/", $username)) {
$response['status'] = false;
$response['msg'] = 'Use alphabet and numeric only.';
} else {
$response['status'] = true;
$response['msg'] = 'Username is available.';
}
echo json_encode($response);
}
?>
Ajax代码:-
//including jquery 2.0.3
<script type="text/javascript" charset="utf8" src="https.://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
//jquery code to fetch data
<script type="text/javascript">
$(document).ready(function() {
$('#username').keyup(function() {
var usercheck = $(this).val();
$('#usercheck').html('<img src="images/loading.gif" width="150" />');
$.post("signup/check1.php", {
user_name: usercheck
}, function(data) {
if (data.status == true) {
$('#usercheck')
.parent('div')
.removeClass('has-error')
.addClass('has-success');
} else {
$('#usercheck')
.parent('div')
.removeClass('has-success')
.addClass('has-error');
}
$('#usercheck').html(data.msg);
}, 'json');
});
});
</script>
HTML代码:
//代码以接受输入并显示输出
//taking input
<input class="check-form" name="user_name" id="username" type="text" placeholder="User Name (please enter unique username)">
//showing output
<span id="usercheck" class="help-block"></span>
<span><i class="ti-check"></i></span>
解决方法:
我认为问题是由于托管服务器设置而来的.如果您触发过多的发布请求,则服务器会因设置而阻止您60到120秒.
尝试使用GET请求而不是POST请求,我认为它将解决问题.
$(document).ready(function() {
$('#username').keyup(function() {
var usercheck = $(this).val();
$('#usercheck').html('<img src="images/loading.gif" width="150" />');
$.get("signup/check1.php", {
user_name: usercheck
}, function(data) {
if (data.status == true) {
$('#usercheck')
.parent('div')
.removeClass('has-error')
.addClass('has-success');
} else {
$('#usercheck')
.parent('div')
.removeClass('has-success')
.addClass('has-error');
}
$('#usercheck').html(data.msg);
}, 'json');
});
});