运行此Ajax XHR时出现500错误。我可以在控制台中看到php代码正在返回正确的响应,但是该响应并未反馈给客户端XHR。我缺少一些会导致此500错误的东西吗?我是PDO的新手,并且已经使用mysqli一段时间了。
index.php
$("#login-submit").click(function(){
$("#login-form").trigger("submit");
});
$("#login-form").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "http://admin.cvlgbtapp.com/login.php",
data: $("#login-form").serialize(),
cache: true,
success: function(data)
{
alert(data);
switch(data)
{
case "1":
alert("Admin login successful. Redirecting...");
window.location = "console.php";
break;
case "2":
alert("User not recognized as administrator. Redirecting...");
window.location = "index.php";
break;
case "3":
alert("Password does not match account records. Please try again.");
break;
}
}
});
});
login.php
<?php
session_start();
header('content-type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
$servername = "localhost";
$username = "redphyre";
$password = "Qazplm10!";
$user = $_POST['username'];
$pass = md5($_POST['password']);
try {
$conn = new PDO("mysql:host=$servername;dbname=cvlgbtq", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM admins WHERE BINARY username = '$user'";
$result = $conn->query($sql);
if ($result->fetchColumn() < 1) {
echo "2";
}
else {
foreach ($conn->query($sql) as $row) {
if ($row['password'] == $pass) {
echo "1";
}
else { echo "3"; }
}
}
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
// class SessionManager
// {
// static function sessionStart($name, $limit = 0, $path = '/', $domain = null, $secure = null)
// {
// // Set the cookie name before we start.
// session_name($name . '_Session');
//
// // Set the domain to default to the current domain.
// $domain = isset($domain) ? $domain : isset($_SERVER['SERVER_NAME']);
//
// // Set the default secure value to whether the site is being accessed with SSL
// $https = isset($secure) ? $secure : isset($_SERVER['HTTPS']);
//
// // Set the cookie settings and start the session
// session_set_cookie_params($limit, $path, $domain, $https, false);
// session_start();
// }
// }
$result->close();
$conn->close();
?>