0

since I uploaded my project to a webhosting I am facing the following issue: whenever I send an ajax request I get this error: POST https://_ DOMAIN _ /backend/login.backend.php 500

apparently the google API works as I get all the data on php but the mysql requests never get through. It could be something about switching from http to https as it used to work perfectly on my localhost and now once I uploaded it, it does not work anymore.

            <script src='https://apis.google.com/js/api:client.js'></script>
            <script>
                var googleUser = {};
                var startApp = function() {
                    gapi.load('auth2', function(){
                    auth2 = gapi.auth2.init({
                        client_id: 'MY_ID_IS_HERE.apps.googleusercontent.com',
                        cookiepolicy: 'single_host_origin',
                        //scope: 'https://www.googleapis.com/auth/user.addresses.read',
                    });
                    attachSignin(document.getElementById('customGoogleButton'));
                    });
                };
                function attachSignin(element) {
                    auth2.attachClickHandler(element, {}, onLogin, onFail)
                }

                function onLogin(googleUser) {
                    var profile = googleUser.getBasicProfile();
                    var id_token = googleUser.getAuthResponse().id_token;
                
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', 'backend/login.backend.php'); // link
                    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    xhr.onload = function() {
                        console.log('Signed in:' + xhr.responseText);
                        //window.location.reload();
                    };
                    xhr.send('idtoken=' + id_token);
                }
                function onFail(error) {
                    console.log(error);
                }
                startApp();
            </script>
    require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';
    include_once $_SERVER['DOCUMENT_ROOT']."/db/config.php";

    //$jwt = new \Firebase\JWT\JWT;
    //$jwt::$leeway = 10;

    if (!isset($_POST["idtoken"])) {
        exit();
    }
    $id_token = $_POST["idtoken"];
    $CLIENT_ID = 'MY_ID_IS_HERE';

    $client = new Google_Client(['client_id' => $CLIENT_ID]); 
    $payload = $client->verifyIdToken($id_token);
    if ($payload) {

        //log in
        session_start();
        
        $_SESSION['userFirstName'] = $payload["given_name"];
        $_SESSION['userLastName'] = $payload["family_name"];
        $_SESSION['userFullName'] = $payload["name"];
        $_SESSION['email'] = $payload["email"];
        $_SESSION['user_image'] = $payload["picture"];
        
                
        $email = mysqli_real_escape_string($conn, $payload["email"]);
        $first = mysqli_real_escape_string($conn, $payload["given_name"]);
        $last_name = mysqli_real_escape_string($conn, $payload["family_name"]);
        $user_image = mysqli_real_escape_string($conn, $payload["picture"]);

        $stmt = $conn->prepare('SELECT id from users WHERE email= ?');
        $stmt->bind_param('s', $_SESSION['email']);
        $stmt->execute();
        $result = $stmt->get_result();
        $data = $result->fetch_assoc();
        if (empty($data)) { 
            $sql = "INSERT INTO users (first_name, last_name, user_image, email) VALUES (?, ?, ?, ?);";
        } else {
            $sql = "UPDATE users SET first_name = ?, last_name = ?, user_image = ? WHERE email = ?;";
        }
        mysqli_query($conn, $sql);
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            echo "SQL ERROR";
        } else {
            mysqli_stmt_bind_param($stmt, "ssss", $first, $last_name, $user_image, $email);
            mysqli_stmt_execute($stmt);
        }
        if (empty($data)) {
            $stmt = $conn->prepare('SELECT id from users WHERE email= ?');
            $stmt->bind_param('s', $_SESSION['email']);
            $stmt->execute();
            $result = $stmt->get_result();
            $data = $result->fetch_assoc();
        }
        $_SESSION['user_id'] = $data['id'];
        exit();
    
    } else {
        echo "<h1>ERROR</h1>";
    }

HassoN
  • 13
  • 2
  • A 500 error is a generic error message and covers pretty much every single thing that can go wrong with a PHP script. Check your server error logs to find out the exact error message. – aynber Aug 02 '21 at 13:12
  • 1
    Remove all `mysqli_real_escape_string`. They damage your data – Dharman Aug 02 '21 at 13:15
  • Thanks for the information, here's what I got: [02-Aug-2021 13:15:40 UTC] PHP Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::get_result() in /home/swisicfc/public_html/backend/login.backend.php:37 Stack trace: #0 {main} thrown in /home/swisicfc/public_html/backend/login.backend.php on line 37 – HassoN Aug 02 '21 at 13:16
  • Odd that http -> https would throw that error, but see https://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result – aynber Aug 02 '21 at 13:32
  • the answer on this post https://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result helped. – HassoN Aug 02 '21 at 14:16

0 Answers0