problem when passing $ _SESSION

1

Login.html

</div>
<div class="col">
  <form action="../php/comprobar_inicio_sesion.php" method="POST" name="form_login">
  <div class="form-group">
    <label for="exampleInputEmail1">Email</label>
    <input name="email" type="email" class="form-control" id="campo_email" aria-describedby="emailHelp" placeholder="Introduce tu email">
    <small id="emailHelp" class="form-text text-muted">Nunca mostraremos tu <b>email</b> a nadie.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Contraseña</label>
    <input name="password" type="password" class="form-control" id="campo_contraseña" placeholder="Contraseña">
    <small id="emailHelp" class="form-text text-muted">Nunca mostraremos tu <b>contraseña</b> a nadie.</small>

  </div>
  <div class="row">
    <div class="col-md-3">
        <a  id="boton_menu" type="submit" class="btn btn-danger" onclick="comprobarCampos()">Inicia Sesion !!</a>
    </div>
    <div class="col-md-3">
      <a  id="btn_atras" href="../index.html" class="btn btn-info align-middle">atras</a>
    </div>
  </div>
  

I have here my login with a form. That will pass the data of the inputs text to the following php file to verify its existence.

Check StartSession.php

> comprueba que el usuario existe para abrir el inicio logeado de la pagina
<?php
ob_start();
?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">

    <!--  CSS -->
    <link rel="stylesheet" type="text/css" href="../css/estilos.css">

    <title></title>
  </head>
  <body>

<?php
      include 'conexion.php';

      $tabla="noesrarosepresta_usuarios";
    /*
      $nombre = "pepito";
      $apel_1 = "perez";
      $apel_2 = "rodriguez";
      $email = "[email protected]";
      $contraseña = "atiketesale";
      */
      $email = $_POST['email'];
      $password =  $_POST['password'];


    /*  $query = "SELECT 'usuario_email', 'usuario_password' FROM '$tabla'
       WHERE usuario_email= '$email' AND
        usuario_password='$password'";*/

        $query= "SELECT *
        FROM '$tabla'
        WHERE usuario_email = '$email' AND
        usuario_password = '$password'";


      $result = mysqli_query($link,$query);
//si existe al menos uno ( que solo va a salir uno si es que existe)
      if (mysqli_num_rows($result)> 0) {
        while($row = mysqli_fetch_array($result)) {




              $_SESSION["id"]=  $row['usuario_id'] ;
              $_SESSION["nombre"]=  $row['usuario_nombre'] ;
              $_SESSION["user"] = $row['usuario_user'];
              $_SESSION["apel_1"]=  $row['usuario_apel_1'] ;
              $_SESSION["apel_2"]=  $row['usuario_apel_2'] ;
              $_SESSION["email"]=  $row['usuario_email'] ;
              $_SESSION["password"]=  $row['usuario_password'] ;
              $_SESSION["mensajes"]=  $row['usuario_mensajes'] ;


            if (headers_sent()) {

                 header("Location:../html/inicio.php");

                   $_SESSION["id"]=  $row['usuario_id'] ;
              $_SESSION["nombre"]=  $row['usuario_nombre'] ;
              $_SESSION["user"] = $row['usuario_user'];
              $_SESSION["apel_1"]=  $row['usuario_apel_1'] ;
              $_SESSION["apel_2"]=  $row['usuario_apel_2'] ;
              $_SESSION["email"]=  $row['usuario_email'] ;
              $_SESSION["password"]=  $row['usuario_password'] ;
              $_SESSION["mensajes"]=  $row['usuario_mensajes'] ;

            }
            else {



              $_SESSION["id"]=  $row['usuario_id'] ;
              $_SESSION["nombre"]=  $row['usuario_nombre'] ;
              $_SESSION["user"] = $row['usuario_user'];
              $_SESSION["apel_1"]=  $row['usuario_apel_1'] ;
              $_SESSION["apel_2"]=  $row['usuario_apel_2'] ;
              $_SESSION["email"]=  $row['usuario_email'] ;
              $_SESSION["password"]=  $row['usuario_password'] ;
              $_SESSION["mensajes"]=  $row['usuario_mensajes'] ;


                                 header("Location:../html/inicio.php");

            }

        }

  //abro la siguiente pagina

            //header("Location:../html/inicio.php");
           // header("../html/inicio.php");
           // exit();





      }
      else {


        echo "<h1> <small>EL USUARIO NO EXISTE</small></h1>";
        header('Location: pag_error.php');
      }






?>

  </body>
</html>

<?php
ob_end_flush();
?>

BarraMenu.php

  

Barramenu.php is a component that I have created to make a include "fichero.php" and save code on all pages. The problem is that you are not reading the variables that are passed to you. Then when executing that include it tells me that it does not recognize the variables

                       

session_start();
if(!isset($_SESSION))
{


    $user = $_SESSION['user'];
    $id = $_SESSION["id"];
    $nombre = $_SESSION['nombre'] ;
    $apel_1 =  $_SESSION['apel_1'] ;
    $apel_2=  $_SESSION['apel_2'] ;
    $email=  $_SESSION['email'] ;
    $password=  $_SESSION['password'] ;
    $mensajes=  $_SESSION['mensajes'] ;


}
else{

    $user = $_SESSION['user'];
    $id = $_SESSION["id"];
    $nombre = $_SESSION['nombre'] ;
    $apel_1 =  $_SESSION['apel_1'] ;
    $apel_2=  $_SESSION['apel_2'] ;
    $email=  $_SESSION['email'] ;
    $password=  $_SESSION['password'] ;
    $mensajes=  $_SESSION['mensajes'] ;


}

echo  $_SESSION['id'];
echo  $_SESSION['user'];
echo  $_SESSION['nombre'];
echo  $_SESSION['apel_1'];
echo  $_SESSION['apel_2'];
echo  $_SESSION['email'];
echo  $_SESSION['password'];
echo  $_SESSION['mensajes'];



?>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <img id="icono_usuario" src="https://www.infojobs.net/ficha.foto?quina=62883927-06B0-F816-BA69AF6E0555C34B" alt="">
<div class="col-sm-3">
  <span class="navbar-brand" href="#"><?php
    echo "<h3 id='titulo_nombre'> <small>
      <b>$user</b>
    </small></h3>";
    ?></span>
  </div>


<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
  <span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
  <ul class="navbar-nav">
    <li class="nav-item active">
      <a class="nav-link" href="../html/inicio.php">Inicio <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="../html/crear_tarjeta.php">Crear Tarjeta</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="../html/ver_usuarios.php">Ver todos los usuarios</a>
    </li>
    <li class="nav-item">
      <a class="nav-link disabled" href="#">Disabled</a>
    </li>

    <li>

      <span class="navbar-brand" href="#"><?php
      echo "<h3 id='titulo_cant_mens_creados'> <small>
        <b>$mensajes tarjetas creadas</b>
        </small></h3>";
        ?></span>

    </li>
  </ul>
</div>
<div class="col">
  <a  href="../php/cerrar_sesion.php"  class="btn btn-danger">Salir</a>
</div>

Start.php

<?php
ob_start();
?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>

<!--  CSS -->
    <link rel="stylesheet" type="text/css" href="../css/estilo_entrado.css">
    <!-- <link rel="stylesheet" type="text/css" href="../css/estilos.css"> -->

    <title>Inicio</title>
  </head>


  <style >

  </style>
  <body>

    <?php


      //VARIABLES QUE RECUPERO DEL FICHERO COMPROBACION DE usuario

      if(isset($_SESSION))
      {
          session_start();
        //  $pk = $_SESSION["PK"];
          $id = $_SESSION["id"];
          $nombre = $_SESSION['nombre'] ;
          $user = $_SESSION['user'];
          $apel_1 =  $_SESSION['apel_1'] ;
          $apel_2=  $_SESSION['apel_2'] ;
          $email=  $_SESSION['email'] ;
          $password=  $_SESSION['password'] ;
          $mensajes=  $_SESSION['mensajes'] ;





      /*  $datos_user[0] =   $_SESSION["id"];
        $datos_user[1] =   $_SESSION["nombre"];
        $datos_user[2] =   $_SESSION["apel_1"];
        $datos_user[3] =   $_SESSION["apel_2"];
        $datos_user[4] =   $_SESSION["email"];
        $datos_user[5] =   $_SESSION["password"];
        $datos_user[6] =   $_SESSION["mensajes"];*/
      }


     ?>


     <?php
         // MENU
        include '../php/componentes/barra_menu.php';
      ?>




<div class="container">

  <!-- CONTENIDO -->
  <div class="row">
    <div class="col-md-8 col-sm-8" id="cuerpo">
      <h1 id="titulo_apartado">Inicio</h1>
      <!-- APARTADO TARJETAS -->

      <?php include "../php/componentes/listar_tarjetas_creadas.php" ?>


      </div>
    <!--  APARTADO NOTICIAS INICIO-->
    <?php include "../php/componentes/apartado_noticias.php" ?>
    <!--
    <div class="col-md-3 col-sm-3" id="tablon">
      <div class="row">
        <div class="col">
          <h3 id="titulo_tablon" class="text-center">Noticias</h3>
        </div>
      </div>
    <div class="row">
        <div  id="sub_apartado" class="col">
          <h6><b>La tarjeta mas votada</b></h6>
          <p>Titulo: <?php echo "string"; ?></p>
          <p>Autor: </p>
        </div>

    </div>
    <div class="row">
        <div class="col">
          <h6><b>Usuario con mas tarejtas creadas</b></h6>
          <p>'Nombre usuario'</p>
        </div>

    </div>
    <div class="row">
        <div class="col">
          <h6><b>Nuevos usuarios</b></h6>
          <ol>
            <li>Usuario nuevo </li>
            <li>Usuario nuevo </li>
            <li>Usuario nuevo </li>
            <li>Usuario nuevo </li>
            <li>Usuario nuevo </li>
          </ol>

        </div>

    </div>

    </div>
 -->
    <!--APARTADO NOTICIAS FIN  -->
  </div>
</div>

  </body>
</html>


<?php
ob_end_flush();
?>

DEPARTURE FROM ERROR

  

Notice: Undefined index: user in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   online 35

     

Notice: Undefined index: id in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   online 37

     

Notice: Undefined index: name in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   online 38

     

Notice: Undefined index: apel_1 in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   online 39

     

Notice: Undefined index: apel_2 in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   on line 40

     

Notice: Undefined index: email in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   on line 41

     

Notice: Undefined index: password in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   online 42

     

Notice: Undefined index: messages in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   on line 43

     

Notice: Undefined index: id in   /storage/ssd2/728/5405728/public_html/php/componentes/barra_menu.php   online 48

    
asked by josanangel 27.07.2018 в 15:32
source

1 answer

2

in the file of comprobar_inicio_sesion.php you do not start session ... check like this ...

 $result = mysqli_query($link,$query);
//si existe al menos uno ( que solo va a salir uno si es que existe)
      if (mysqli_num_rows($result)> 0) {
        while($row = mysqli_fetch_array($result)) {
//inicia session;
session_start()


          $_SESSION["id"]=  $row['usuario_id'] ;
          $_SESSION["nombre"]=  $row['usuario_nombre'] ;
          $_SESSION["user"] = $row['usuario_user'];
          $_SESSION["apel_1"]=  $row['usuario_apel_1'] ;
          $_SESSION["apel_2"]=  $row['usuario_apel_2'] ;
          $_SESSION["email"]=  $row['usuario_email'] ;
          $_SESSION["password"]=  $row['usuario_password'] ;
          $_SESSION["mensajes"]=  $row['usuario_mensajes'] ;

good luck bro and hopefully I'll help you By; JJ

    
answered by 27.07.2018 / 16:20
source