PHP Session Start Bug - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, July 23, 2023

PHP Session Start Bug

When i try a fetch post request to a file the echo isnt done, and I dont know why.

/global/config.php

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

if (session_status() === PHP_SESSION_NONE) session_start();

// SMTP
const SMTP_HOST = '(something)';
const SMTP_USER = '(something)';
const SMTP_USERNAME = '(something)';
const SMTP_PSW = '(something)';

// LINKS
define('DOC_ROOT', $_SERVER['DOCUMENT_ROOT']);
const LINK_HOME = '/';
const IMPORT_PATH = DOC_ROOT . LINK_HOME;
const LINK_API = LINK_HOME . 'api/';
define('LINK_WEBSITE', $_SERVER['HTTP_HOST'] . LINK_HOME);

const APP_NAME = '(something)';
const LOGO = LINK_HOME . '(something)';
const LOGO_WHITE = LINK_HOME . '(something)';
const LOGO_SM = LINK_HOME . '(something)';
const LOGO_SM_WHITE = LINK_HOME . '(something)';
const DEFAULT_AVATAR = LINK_HOME . '(something)';

$page_name = '(Sem nome)';
require_once(IMPORT_PATH . "api/check.php");

/api/check.php

require_once($_SERVER['DOCUMENT_ROOT'] . '/global/config.php');
$cur_loc = $_SERVER['REQUEST_URI'];

if (!str_contains($cur_loc, LINK_HOME . "api/auth/")) {
    if (!isset($_SESSION['user']) && !str_contains($cur_loc, LINK_HOME . "pages/auth/")) {
        header('Location: ' . LINK_HOME . 'pages/auth/login');
        exit();
    } else if (isset($_SESSION['user']) && $cur_loc !== LINK_HOME . "pages/auth/logout" && str_contains($cur_loc, LINK_HOME . "pages/auth/")) {
        header('Location: ' . LINK_HOME);
        exit();
    }
}

/api/user/edit.php

require_once($_SERVER['DOCUMENT_ROOT'] . '/global/config.php');
header('Content-Type: application/json');
$response = ["status" => "error", "message" => "Método inválido!"];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {}
session_write_close();
echo json_encode($response, JSON_THROW_ON_ERROR);

The script I'm using is this

window.addEventListener("DOMContentLoaded", () => {
    const form = document.querySelector("form#account-profile-details-form");
    const btnSubmit = document.querySelector("#account-info-submit");
    const validator = FormValidation.formValidation(form, {
        fields: {
            'email': {
                validators: {
                    notEmpty: {
                        message: 'E-mail é obrigatório'
                    },
                    emailAddress: {
                        message: 'E-mail inválido'
                    },
                    invalidEmail: {
                        message: 'E-mail inválido'
                    }
                }
            }
        },

        plugins: {
            trigger: new FormValidation.plugins.Trigger(),
            bootstrap: new FormValidation.plugins.Bootstrap5({
                rowSelector: '.fv-row',
                eleInvalidClass: '',
                eleValidClass: ''
            })
        }
    });

    form.onsubmit = (ev) => {
        ev.preventDefault();

        if (validator) {
            validator.validate().then((status) => {
                if (status === 'Valid') {
                    btnSubmit.setAttribute('data-kt-indicator', 'on');
                    btnSubmit.disabled = true;

                    const url = '<?= LINK_API ?>user/edit';
                    const data = new FormData(form);
                    const options = {
                        method: 'POST',
                        body: data,
                        headers: {
                            Accept: 'application/json'
                        }
                    };

                    fetch(url, options)
                        .then(res => res.json)
                        .then(res => {
                            console.log(res);
                        })
                        .catch(err => console.error('Ocorreu um erro!', err))
                        .finally(() => {
                            btnSubmit.removeAttribute('data-kt-indicator');
                            btnSubmit.disabled = false;
                        });
                }
            });
        }
    };
});

But when I do the fetch instead of return the json dont return nothing, when I tried to access the file via GET method return me the json correct, anyone can help?



source https://stackoverflow.com/questions/76745355/php-session-start-bug

No comments:

Post a Comment