Search code examples
phpangularjspostinternal-server-error

Angularjs 500 (Internal Server Error) in a very basic program


Hello I have a very basic program that throws the error ¿can you tell why?

the php: Basically puts into a file the post that is a JSON. It's a part of a long polling system.

<?php
if( empty($_POST || !isset($_POST)) ) {
    $last_ajax = null;
} else {
    $last_ajax = time();
}
clearstatcache();
$last_change = filemtime('cache.txt');

if ($last_ajax == null || $last_change > $last_ajax) {

    // Load the ajax
    $R = json_decode(file_get_contents('php://input'), true);
    // Load into memory the file
    $F = json_decode(file_get_contents('cache.txt'), true);
    // If not empty the file append
    if(!empty($F)) {
        $S = array_merge_recursive($F, $R);
        file_put_contents('cache.txt', json_encode($S), true); 
    // Else just write into the file the post
    } else {
        file_put_contents('cache.txt', json_encode($R), true); 
    }
    echo json_encode(file_get_contents('cache.txt'), true);
    break;
}
?>

And the very simple post in Angular. It's just a simple post that sends to de php above the data, that is a JSON

    var enrol = angular.module("enrol",[]);

enrol.controller("enrolController", function($scope, $http){

    $scope.submit  = function() {
        console.log("Submitting");
        $http({
            method  : "POST",
            url     : "dependencies/cache.php",
            data    : {
                'action': 'enrol',
                'data': {
                    'Login': $scope.apliclogin,
                    'Password': $scope.aplicpass,
                    'rol': {
                        'admin': $scope.admin,
                        'sala': $scope.sala,
                        'barra': $scope.barra,
                        'cocina': $scope.cocina,
                        'caja': $scope.caja
                    }
                }
            },
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            console.log(data);
        });
    };
});

Solution

  • First thing I noticed is your first line of PHP. Don't you mean ...

    if( empty($_POST) || !isset($_POST) ) {