当我在本地服务器上运行它时,我的ajax请求工作正常,但是一旦我将它上传到我的服务器,就没有什么工作了。我的帐户注册页面一直返回:POST http://www.mydomainnottellingyou.net/php/registerUser.php 500(内部服务器错误)
其他文件的链接已正确链接,因为它在本地工作,并且数据库登录用户名和密码也没有任何问题。我已经尝试使用localhost和127.0.0.1让主机连接到数据库。
这是Ajax请求:
var data = {
username: registerUsername,
password: loginPassword
};
$.ajax({
url: "php/registerUser.php",
data: data,
method: "POST",
success: function(response){
if(response == "1"){
window.location.replace("roomSelection.php");
}
else{
$("#errorsDiv").html(response);
}
}
});
然后是php:
<?php
session_start();
if(isset($_POST["username"]) && isset($_POST["password"])){
//Validate
$username = $_POST["username"];
$password = $_POST["password"];
if(strlen($username) >= 3 && strlen($username) <= 18 && strlen($password) >=6 && strlen($password) <= 16){
//Check for special chars
$allowed = true;
for($i=0; $i<strlen($username); $i++){
$charCode = ord($username[$i]);
if(!(($charCode >= 48 && $charCode <= 57) || ($charCode >= 65 && $charCode <= 90) || ($charCode >= 97 && $charCode <= 122))){
$allowed = false;
}
}
if($allowed){
//Insert user to database now
include "../lib/databaseHandler.php";
$dbo = createDBO("chat");
//Check if username already exists
$statement = "SELECT * FROM users WHERE username=:username";
$bindings = [
":username" => $username
];
$query = executeQuery($dbo, $statement, $bindings);
$numRows = getQueryRowCount($query);
if($numRows <= 0){
//Create user
$statement = "INSERT INTO users(username, password) VALUES(:username, :password)";
$password = hash("sha256", md5($password));
$bindings = [
":username" => $username,
":password" => $password
];
$query = executeQuery($dbo, $statement, $bindings);
//Get id
$statement = "SELECT userId FROM users WHERE username=:username";
$bindings = [
":username" => $username
];
$query = executeQuery($dbo, $statement, $bindings);
$rows = getQueryRows($query);
if(isset($rows[0]["userId"])){
$_SESSION["chatUserId"] = $rows[0]["userId"];
}
echo "1";
}
else{
echo "Username already taken";
}
}
}
}
?>
还有一个包含文件(databaseHandler.php),如下所示:
<?php
function createDBO($dataBase){ //Make Database Object
if(strtolower($dataBase)=="chat"){
include $_SERVER['DOCUMENT_ROOT'] . "/PeasFactory/Config/chatDbConfig.php";
}
return(new PDO("mysql:host=".$dbConfig["host"].";dbname=".$dbConfig["dbname"],$dbConfig["username"],$dbConfig["password"]));
}
function executeQuery($dbo, $statement, $bindings){
if($dbo != null){
$query = $dbo->prepare($statement);
$query->execute($bindings);
}
return $query;
}
function getQueryRows($query){
if($query != null){
return $query->fetchAll();
}
}
function getQueryRowCount($query){
if($query != null){
return $query->rowCount();
}
}
?>