【问题标题】:500 Internal Server Error with Javascript AJAX POST on Cpanel [duplicate]Cpanel 上的 Javascript AJAX POST 出现 500 内部服务器错误 [重复]
【发布时间】:2019-06-24 14:58:38
【问题描述】:

尝试在我的网页上注册用户时出现以下错误:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

^ 使用注册脚本指向 PHP 文件

内部服务器错误指向以下脚本:

 $("#login-submit").on('click', function () {
    $.ajax({
      url: 'ext/login_process.php',
      type: "POST",
      data: $("#login-form").serialize(),
      success: function (result) {
        if (result === "done") {
          window.location.href = "index.php";
        } else {
          var elementExists = document.getElementById("msg-default");
          if (elementExists !== null) {
            document.getElementById("msg-default").innerHTML = "";
          }
          document.getElementById("err-msg").innerHTML = "<div class = 'alert alert-danger text-uppercase'><strong>Error!&nbsp;</strong>" + result + "</div></div>";
        }

      },
      error: function (xhr, resp, text) {
        console.log(xhr, resp, text);
      }
    });

  });

按下注册按钮时发生错误。如果用户名已经被占用,它会正常工作,然后该消息将按应有的方式显示。

HTML:

<form name="register-form" id="register-form" method="POST">
                        <h5 class="text-center">Register</h5>
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Username" name="username" required>
                        </div>
                        <div class="form-group">
                            <input type="password" class="form-control" placeholder="Password" name="password" required>
                        </div>
                        <div class="form-group">
                            <input type="password" class="form-control" placeholder="Repeat password" name="c-password" required>
                        </div>
                        <div class="form-group">
                            <label>I am...</label>
                            <div class="radio">
                                <label><input type="radio" name="position" checked value="s">&nbsp;Student</label>
                            </div>
                            <div class="radio">
                                <label><input type="radio" name="position" value="t">&nbsp;Teacher</label>
                            </div>
                        </div>
                        <div class="err-msg" id="err-msg"></div>
                    </form>
                    <button id="register-submit" class="btn btn-success">Senda</button>

PHP:

$username = trim(preg_replace('/\s+/', ' ', htmlspecialchars($_POST["username"])));
$password = htmlspecialchars($_POST["password"]);                                       // Do not trim the password, let the user input whatever they desire
$confirmPssword = htmlspecialchars($_POST["c-password"]);                               // Do not trim the password, let the user input whatever they desire
$position = trim(preg_replace('/\s+/', ' ', htmlspecialchars($_POST["position"])));

if (empty($username))
{
    echo "Invalid username!";
} else
if (empty($password))
{
    echo "Invalid password!";
} else
if ($confirmPssword != $password)
{
    echo "Password/Confirm Password didn't match!";
} else {
    $sql = get_user_details($username);
    $result = $conn->query($sql);

    if ($result)
    {
        if ($result->num_rows > 0)
        {
            $result->close();
            echo "username is already registered";
        } else {
            // Generate a new random salt
            $salt = '$1$' . substr(strtr(base64_encode(random_bytes(32)), '+', '.'), 0, 8) . '$';

            // Generate the MD5 hashed password
            $cryptedPassword = crypt($password, $salt);

            $sql = add_user($username, $cryptedPassword, $position);
            if ($conn->query($sql))
            {
                echo "done";
            } else {
                echo "please refresh the page and try again";
            }
        }
    } else {
        echo "please refresh the page and try again";
    }

    $conn->close();
}

我正在使用这个:https://code.jquery.com/jquery-3.1.1.min.js

我在哪里可以获得有关 cpanel 错误的更多具体信息?

网络标签:

Server  
nginx/1.14.1
Date    
Mon, 24 Jun 2019 14:44:51 GMT
Content-Type    
text/html; charset=UTF-8
Content-Length  
0
Connection  
keep-alive
Expires 
Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   
no-store, no-cache, must-reval…te, post-check=0, pre-check=0
Pragma  
no-cache
Request headers (542 B) 
Host    
(WEBSITE)
User-Agent  
Mozilla/5.0 (Windows NT 10.0; …Firefox/56.0 Waterfox/56.2.11
Accept  
*/*
Accept-Language 
en-US,en;q=0.5
Accept-Encoding 
gzip, deflate
Referer 
http://(WEBSITE)/register.php
Content-Type    
application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With    
XMLHttpRequest
Content-Length  
53
Cookie  
PHPSESSID=v1qp6qo01igqlho8668sch2mu5
Connection  
keep-alive
Pragma  
no-cache
Cache-Control   
no-cache

“响应”下不显示任何内容

【问题讨论】:

  • 服务器错误意味着这是您的 PHP 的问题,而不是您的 HTML 或 JS 的问题。请分享您的 PHP 代码。
  • A 500 错误是一种通用错误消息,几乎涵盖了脚本可能出错的每一件事。检查您的服务器错误日志以找出确切的错误消息。
  • 您的错误来自ext/login_process.php,而不是您向我们展示的任何代码。因此,您问题上唯一有效的标签是php,您实际上并未显示任何代码。
  • 在这种情况下你不应该使用htmlspecialchars()。这可能意味着用户的用户名和/或密码更改(他们将无法登录)。 htmlspecialchars() 用于在页面上输出用户输入。您还应该使用password_hash() 代替crypt()(在PHP 文档中鼓励)。如果没有您的错误(按照上面的建议检查日志),这只是一个猜谜游戏。我看不到任何语法错误。
  • 查看How do I get PHP errors to display? 以在页面上显示错误

标签: javascript php jquery ajax cpanel


【解决方案1】:

好吧,它现在无论如何都可以使用。我只是将 PHP 版本从默认的 5.6 更改为 7.0..

【讨论】:

  • 很高兴你能成功。我仍然建议您遵循我在上面的 cmets 中提供的建议。另外,不要忘记使用prepared statements 来防止SQL 注入。快乐编码:)
  • 我会检查一下,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
相关资源
最近更新 更多