【问题标题】:500 Internal Server Error on PHP callsPHP 调用时出现 500 内部服务器错误
【发布时间】:2015-07-05 07:52:48
【问题描述】:

当我使用$.ajax 请求php 文件时遇到此问题,它总是返回500 服务器错误,但仅当我将文件上传到我的服务器时。 我在 Windows 8.1 上使用 XAMPP,并在 localhost 中完美运行。

我尝试直接访问 php 文件的 url,也给了我同样的错误。

Ajax

function get_blog(reload_list){
    var $titulo, $texto, $response, $post, $post_list;
    var parameters = window.location.search;
    $.ajax({
        method: 'get',
        url: '/php/blog.php' + parameters,
        success: function(data){
            if(data == "404"){
                $("#blog").html("404 El articulo que buscas no existe");
            } else {
                $response = data;
                $post_list = $response.post_list;
                $titulo = $response.post.titulo;
                $texto =  $response.post.texto;
                $("title#blog_title").html("IBR - " + $titulo);
                $("h3#blog_title").html($titulo);
                $("#blog_text").html($texto);

                if(reload_list){
                    for(i=0; i<=$post_list.length; i++){
                        $("#post_list").append($post_list[i]);
                    }
                }
            }
        }
    });

PHP

<?php
error_reporting(E_ALL);
ini_set('display_errors' 1); 

$l =  mysql_connect ( "localhost" , "myserver" , "mypass" ) or die("Error connecting:<BR><BR>".mysql_error());

mysql_select_db( "mydb" ) or die("Error getting db:<BR><BR>".mysql_error()); 
if(!isset($_GET['post_id'])) {
    $query = mysql_query("SELECT * FROM mytable ORDER BY id DESC LIMIT 1") or die(mysql_error());
    $row = mysql_fetch_array($query);
    $id = stripslashes($row['id']); 
    $titulo = stripslashes($row['titulo']); 
    $texto = stripslashes($row['texto']); 

    $post = [
            'id' => $id,
            'titulo' => $titulo,
            'texto' => $texto
    ];

}else{
    // echo $_GET['post_id'];
    $post_id = $_GET['post_id'];
    $query = mysql_query("SELECT * FROM mytable WHERE id=$post_id ORDER BY id DESC LIMIT 1") or die(mysql_error());
    $row = mysql_fetch_array($query);
    if(empty($row)){
        die("404");
    }
    $id = stripslashes($row['id']); 
    $titulo = stripslashes($row['titulo']); 
    $texto = stripslashes($row['texto']); 


    $post = [
            'id' => $id,
            'titulo' => $titulo,
            'texto' => $texto
    ];
}


$query = mysql_query("SELECT * FROM mytable ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_array($query) )
{

    $id = stripslashes($row['id']); 
    $titulo = stripslashes($row['titulo']); 

    $li = '<a class="text_decoration post_list_item" href="devocional.html?post_id='.$id.'"><li>'.$titulo.'</li></a>';

    $post_list[]= $li;
}

$response = [
    'post' => $post,
    'post_list' => $post_list
];

$json_response = json_encode($response);
print_r($json_response);
?>

实际上我无法访问我的error_log。还尝试通过以下方式查看错误:

error_reporting(E_ALL);
ini_set('display_errors' 1);

但是浏览器只显示一个空白页面。我在使用PHP SDK 的 Facebook API 调用中也遇到了同样的问题。我真的不知道该怎么办。感谢您提前提供帮助。

【问题讨论】:

  • 在网络服务器上,您可以尝试使用实际的 GET 参数打开 /php/blog.php 吗?所以你可以这样调试?
  • 您运行的是相同版本的 PHP 吗?您的错误日志中有任何内容吗?我怀疑您的网络服务器正在运行 *.com/questions/4271874/…。
  • @Ron 我已经做到了,除了开发人员工具中网络选项卡上的状态代码 500 之外,仍然没有收到任何警告。我正在使用 Chrome 42。
  • 您的ini_set 缺少昏迷:ini_set('display_errors', 1);

标签: php facebook-php-sdk server-side http-error ajax-request


【解决方案1】:

在 5.4 之前,PHP 不支持您当前使用的速记数组。 http://php.net/manual/en/migration54.new-features.php, http://php.net/manual/en/language.types.array.php

这是您更新后的代码,它应该可以工作并且与 5.6 向前兼容。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1); 

$l =  mysql_connect ( "localhost" , "myserver" , "mypass" ) or die("Error connecting:<BR><BR>".mysql_error());

mysql_select_db( "mydb" ) or die("Error getting db:<BR><BR>".mysql_error()); 
if(!isset($_GET['post_id'])) {
    $query = mysql_query("SELECT * FROM mytable ORDER BY id DESC LIMIT 1") or die(mysql_error());
    $row = mysql_fetch_array($query);
    $id = stripslashes($row['id']); 
    $titulo = stripslashes($row['titulo']); 
    $texto = stripslashes($row['texto']); 

    $post = array(
            'id' => $id,
            'titulo' => $titulo,
            'texto' => $texto
    );

}else{
    // echo $_GET['post_id'];
    $post_id = $_GET['post_id'];
    $query = mysql_query("SELECT * FROM mytable WHERE id=$post_id ORDER BY id DESC LIMIT 1") or die(mysql_error());
    $row = mysql_fetch_array($query);
    if(empty($row)){
        die("404");
    }
    $id = stripslashes($row['id']); 
    $titulo = stripslashes($row['titulo']); 
    $texto = stripslashes($row['texto']); 


    $post = array(
            'id' => $id,
            'titulo' => $titulo,
            'texto' => $texto
    );
}


$query = mysql_query("SELECT * FROM mytable ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_array($query) )
{

    $id = stripslashes($row['id']); 
    $titulo = stripslashes($row['titulo']); 

    $li = '<a class="text_decoration post_list_item" href="devocional.html?post_id='.$id.'"><li>'.$titulo.'</li></a>';

    $post_list[]= $li;
}

$response = array(
    'post' => $post,
    'post_list' => $post_list
);

$json_response = json_encode($response);
print_r($json_response);
?>

另外,这里是强制性说明...

  1. 将 SQL 驱动程序从 mysql_ 更改为 mysqli 或 PDO。
  2. 将您的用户输入与您的查询或在 最少使用 mysql_real_escape_string (该函数名可能是 按顺序倒置)。您目前对 SQL 注入持开放态度。

【讨论】:

  • 嗯,就是这样。非常感谢我一整天都在苦苦挣扎。
  • 最后一个问题是将用户输入与查询分开的最佳形式是什么。从字面上看,我只用了一周的 PHP 和 MySQL。
  • 使用 mysql_ 驱动程序,您只能使用转义函数或将数据转换为整数 $post_id = (int)$_GET['post_id'];。使用 mysqli 和 PDO 驱动程序,它们支持准备好的语句。这是一个关于该主题的更详细的主题,*.com/questions/60174/…
最近更新 更多