Avoid viewing a page without going through index.php

0

I have a index.php that is a form of login and that being correct the user and the password directs to another page. The problem is that if I go to the other page I can see it without having gone through the login and I want to see this page if you need to go through index.php any ideas to implement this?

I leave you the index.php

<?php
    require('conexion.php');

    session_start();

    if(isset($_SESSION["id_usuario"])){
        header("Location: welcome.php");
    }

    if(!empty($_POST))
    {
        $usuario = mysqli_real_escape_string($mysqli,$_POST['usuario']);
        $password = mysqli_real_escape_string($mysqli,$_POST['password']);
        $error = '';

        $sha1_pass = sha1($password);

        $sql = "SELECT * FROM tbl_login WHERE email = '$usuario' AND password = '$sha1_pass'";
        $result=$mysqli->query($sql);
        $rows = $result->num_rows;

        if($rows > 0) {
            $row = $result->fetch_assoc();
            $_SESSION['email'] = $row['email'];
            $_SESSION['password'] = $row['password'];

            header("location: descargar_excel.html");
            } else {

            $error = "Inicio de cuenta ha fallado. Verifique su nombre de usuario y clave.";
        }
    }
?>
<html>
    <head>
        <title>Iniciar sesión</title>
               <link rel="stylesheet" type="text/css" href="css2.css">
    </head>

    <body style="background-color:#FFFFFF;">
                          <center>
                          <img src="ic_launcher.png">



        <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" > 

                        <div id="h1">Iniciar sesión</div>
                         <section>
            <div><input class="form-element form-field" id="usuario" name="usuario" placeholder="Usuario" type="text" ></div>

                          </section>

                          <section>
            <div><input class="form-element form-field" id="password" name="password"  placeholder="Contraseña" type="password"></div>

                         </section>

                          <section>
            <div><input class="form-element form-button" name="login" type="submit" value="Siguiente" id="button"></div> 
                           </section>
        </form> 

        <div style = "font-size:16px; color:#00695C;"><?php echo isset($error) ? utf8_decode($error) : '' ; ?></div>



</center>

    </body>
</html> 

I leave you where you address if the login is correct

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

        <title>REPORTE EXCEL</title>
        <meta name="description" content="" />
        <meta name="author" content="Swnk" />

        <meta name="viewport" content="width=device-width; initial-scale=1.0" />

           <link rel="stylesheet" type="text/css" href="css2.css">
    </head>

    <body  style="background-color:#FFFFFF;">
              <center>
        <div>
            <header>

            </header>
                              <center>
                                      <div id="h1">Reporte encuesta en excel</div> 
             <section>           
            <div>
                    <a href="reporteexcel.php"><FONT COLOR="#004D4">Haz clic para descargar el reporte</FONT></a>
            </div>
                         </section>

                          <section>
                         <a href="logout.php"><FONT COLOR="#004D4">Salir</FONT></a>
                         </section>
        </div>


                </center>

    </body>
</html>
    
asked by Ashley G. 24.01.2017 в 19:16
source

1 answer

3

To protect your download_excel.html page, you must check if the session exists when it is created when the user logs in, in case the session does not exist, we redirect to the index.php (login).

download_excel.html

//Arrancamos la sesión
session_start();
//Comprobamos existencia de sesión
if (!isset($_SESSION['email']) && !isset($_SESSION['password'])) {
   header('loacation:index.php');
}
<!DOCTYPE html>
<html>
//Etc.
    
answered by 24.01.2017 / 20:03
source