Modify variable in a PHP Session

0

I have this in a PHP file

<?php session_start(); ?>

<?php $inscripcioncerrada = "inscripcioncerrada.php" ?>
<?php $inscripcionabierta = "inscripcionabierta.php" ?>    
<?php $_SESSION['estadoInscripcion'] = $inscripcioncerrada ?>

in the pages to which I refer to $_SESSION['estadoInscripcion'] I have just started an include such that <?php include("periodoinscripcion.php"); ?> which is what the file is called.

So all right.

Well, now what I want (I do not know if it is possible to do it) is to make a script to modify that variable $_SESSION['estadoInscripcion'] in the PHP file so that when the rest of the pages refer to it, the new value will appear.

That is to say, if for example the inscription is closed, that calling a script changes it to open ( $_SESSION['estadoInscripcion'] = $inscripcionabierta ) in the PHP. So all the pages that include it change the value.

How to do it?

    
asked by Paolo Frigenti 09.05.2018 в 17:49
source

1 answer

2

JS

function llamadaAjax() {

            var nuevoEstado = 'minuevaurl.php', //Valor a asignar a la $_SESSION
                xhr = new XMLHttpRequest();

            xhr.open('POST', 'miphp.php'); //El PHP con el código que cambia la SESSION
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onload = function() {
                if (xhr.status === 200 && xhr.responseText !== nuevoEstado) {
                    alert("Cambiado!");

                }
                else if (xhr.status !== 200) {
                    alert('Error ' + xhr.status);
                }
            };
            xhr.send(encodeURI('estado=' + nuevoEstado)); //Envíamos variable 
}

PHP

if (isset($_REQUEST["estado"])) { //Nos aseguramos de que no llegue vacía
    $_SESSION["miVarDeSesion"] = $_REQUEST["estado"]; 
}
    
answered by 10.05.2018 / 10:02
source