0
votes

im posting data with ajax to PHP, this code is working fine in local server (im using XAMPP), but when i upload it into web server it is not taking data and showing me this error

POST http://my_server_IP/login.php 500 (Internal Server Error)

if i change dataType from json to application/json and add contentType: 'application/json; charset=utf-8',

then it is shwong another error

POST http://my_server_IP/login.php 500 (Internal Server Error)

but in local machine it is working fine, i can login with same code

here is my JQuery, AJAX code

$.ajax({
  dataType: "json",
  type: "POST",
  data: {user: sEmail, pass: sPass},
  url: "login.php",
  success:function(response){
    var a = response;
    if(a.res=='agree'){
      var st = a.role+","+a.name;
      $.cookie("imgup", st, { path: '/imgup', expires: 2 });
      window.location.href = "logged.php";
    }else if (a.res == 'error') {
      errorDisplay(a.error);
    }
  }
});

and this is PHP

<?php
header('Content-Type: application/json');
require_once('inc/init.php');
require_once "helper/helper.php";
$email = sanitize($_POST['user']);
$pass = sanitize($_POST['pass']);

$sql_query = $db->query("SELECT * FROM `user_admin` WHERE `username`='$email'");
if(mysqli_num_rows($sql_query) > 0){
    $sql = mysqli_fetch_assoc($sql_query);
  if(password_verify($pass, $sql['password'])){
    $role = explode(',',$sql['role']);
    if(in_array("admin", $role)){
            echo json_encode(array('res' => 'agree', 'role' => 'admin','name' => $sql['id']));
    }elseif(in_array("editor", $role)){
        echo json_encode(array('res' => 'agree', 'role' => 'editor','name' => $sql['id']));
    }elseif(in_array("user", $role)){
        echo json_encode(array('res' => 'agree', 'role' => 'user','name' => $sql['id']));
    }
  }else{
    echo json_encode(array('res' => 'error', 'error' => 'user_pass'));
  }
}else{
    echo json_encode(array('res' => 'error', 'error' => 'user_not_exist'));
}
?>

Error log is

PHP Fatal error: Call to undefined function password_verify() in /var/www/html/login.php on line 11, referer: http://__my_IP__/login_page.php

but that function working fine in localhost!!!

how can i fix this error?

NOTE: sanitize is the function to check html entities present in the string

4
Look at your server error logs, it will tell you why it's throwing a 500 error. - aynber
check server logs... and share the error so we can help or suggest better. - Naincy
where password_verify function is defined...? As Php not able to find that functions definition - Naincy
It sounds like your webserver is running below PHP 5.5. - aynber
Your hoster probably has 5.4 as the default. Talk with them, or see if you can install it with the php55 or php56 package. - aynber

4 Answers

1
votes

Put phpinfo() on top of your PHP code and run it directly (not through AJAX) on your hosting server. If PHP Version you see on your screen in browser is lower than 5.5.0 - ask your hoster to upgrade it or use another logic for password checking.

0
votes

this error causing because I had an old version of PHP (v5.4), updated my PHP version to 5.6 and problem solved!

password_verify() and some more features are added after v5.5.0,

make sure that your PHP version is 5.5+ then these functions works fine :)

0
votes

If you have done json_encode() with an array, it works for localhost.
If you are at live server you should remove the header("Content Type","application/json"); to work perfectly.
I have tried and searched lots, but this worked for me finally.

0
votes

It seems the password_verify function is not supported by your PHP version. For more info, you can have a look at PHP manual:

https://www.php.net/manual/en/function.password-verify.php

Main source: https://www.php.net