Problem with $ _SERVER ['PHP_SELF']

0

I have the following code for a login form, from a previous page I sent by POST method the variable id_modulo and I recover it in the login page, but if the user miswrites the password or the name of user the system stays in the same page since the action of the form is $_SERVER['PHP_SELF'] . In this same form I have a hidden field to save the result of the variable id_modulo , but it happens that when reloading the page for the above reason (user or bad password) I can not recover the value of id_modulo , it gives me the error of:

  

Notice: Undefined index: module_id in   C: \ xampp \ htdocs \ security \ questionnaire \ loginp.php on line 67

Code of the page:

$loginFormAction = $_SERVER['PHP_SELF'];

$id_modulo = $_POST['id_modulo'];
if ($id_modulo == "todos") {
  echo "<h3>CUESTIONARIO CON TODOS LOS MODULOS</h3>";
}else{

 $SQLModulo = "SELECT * FROM modulos WHERE id_modulo='$id_modulo'";
 $SQLconsultaModulo = $conexionmysqli->query($SQLModulo);
 $SQLconsultaModulo->data_seek(0);
 $row_nombreModulo = $SQLconsultaModulo->fetch_assoc();
 $nombreModulo ="".$row_nombreModulo['n_modulo']."";
 $SQLconsultaModulo->close(); // Liberar memoria usada por consulta.
  echo "<h3>$nombreModulo</h3>";
 }

<form role="form" id="login" name="form_ingreso" method="post" action="<?php echo $loginFormAction; ?>">
                          <input type="text" name="id_modulo" id="id_modulo" value="<?php echo"$id_modulo"; ?>">
                            <fieldset>
                                <div class="form-group input-group">
                                    <span class="input-group-addon">Usuario</span>
                                    <input type="text" class="form-control field required" name="usuario" id="login_username" value="" placeholder="..." data-toggle="tooltip" title="Teclee su nombre de usuario" data-placement="bottom" autofocus>
                                </div>
                                <div class="form-group input-group">
                                    <span class="input-group-addon">Contrase&ntilde;a</span>
                                    <input type="password" class="form-control field required" name="contrasena"  value="" id="login_password" placeholder="..." data-toggle="tooltip" title="Teclee su contrase&ntilde;a" data-placement="bottom">
                                </div>
                                <p class="help-block"><em>Verifique que la tecla Block May&uacute;s (CAPS LOCK) no este activada.</em></p>
                                <!-- Change this to a button or input when using this as a form -->
                            </fieldset>
                    </div>
                    <div class="panel-footer">
                        <div class="btn-group">
                          <button type="submit" class="btn btn-primary btn-sm">ACCEDER</button>
                        </div>
                        <span class="pull-right"><a href="../" class="btn btn-danger btn-sm">REGRESAR</a></span>
                    </div>
                  </form>
    
asked by Dixander Carballo Buque 06.12.2017 в 23:10
source

1 answer

0

Assuming that the initial page (which does not have the module fixed) shows all the modules, then you can check if the superglobal $_POST['id_modulo'] is set and otherwise assume that it is "all".

$id_modulo = isset($_POST['id_modulo']) ? $_POST['id_modulo'] : 'todos';

The error it gives you is because you try to recover a key that is not present in $_POST , which will occur when you do not send the form but load the page directly.

If you want to persist the value of the last id_modulo sent, you can do it in a session variable:

<?php

session_start();

if (isset($_POST['id_modulo'])) {
  $id_modulo = $_POST['id_modulo'];
  $_SESSION['id_modulo'] = $_POST['id_modulo'];
} else if (isset($_SESSION['id_modulo'])) {
  $id_modulo = $_SESSION['id_modulo'];
} else {
  $id_modulo = 'todos';
}

I am still surprised that if you save the id_modulo selected for the first time in a hidden input, it is not sending it in the second request.

    
answered by 06.12.2017 / 23:16
source