Jeanbf
Jeanbf

Reputation: 317

How can I start a session and redirect in php?

Hi I have controllerLoginUsu.php:

<?php

require "dao/daoLoginUsu.php";  

class LoginUsuario{

    public function setDatos($aInput) {

        $obj = json_decode($aInput, true);

       $Dao = new daoLoginUsuario();
       $Dao->setDataDato($obj);

       $msj = $Dao->setDataDato($obj);


      if($msj === 'si'){
        return $msj;       
      }else{
        return $msj;
      }
   } 
}
?>

Well, In the $msj variable I get a "si" or "no" , that is the answer of a query.

If $msj is "si" I need start a session and redirect to http://localhost:8080/formulario_web/formulario/formulario_lazos.html

And I need see formulario_lazos.html only if I start a session:

<!DOCTYPE html>
<html lang="es">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type ="text/css" href="css/bootstrap.css">
        <link rel="stylesheet" type ="text/css" href="css_propio/boostrap.estilo.css">

        <link rel="stylesheet" type ="text/css" href="css_propio/boostrap.estilo.datepicker.css">
        <link rel="stylesheet" type ="text/css" href="css_propio/bootstrap.old.datepicker.css">

        <link rel="stylesheet" type ="text/css" href="css/boostrap.datepicker.css">
        <link rel="stylesheet" type ="text/css" href="css/bootstrap.min.css">
        <link rel="stylesheet" type ="text/css" media="all" href="css/bootstrap-select.min.css">

        <title>FORMULARIO</title>
    </head>

<body>



        <div id="main_container_id">

                <!--INICIO ENCABEZADO-->
                <div class="container-fluid" id="encabezado_container_id"></div>
                <!--FIN ENCABEZADO-->

                <!--INICIO CONTENEDOR OBLIGATORIO-->
                <div id="tarea_container_id"></div>
                <!--FIN CONTENEDOR OBLIGATORIO-->

                <!--INICIO CONTENEDOR INFORMACION DE REGISTRO-->
                <div class="container" id="informacion_de_registro_container_id"></div>
                <!--FIN CONTENEDOR INFORMACION DE REGISTRO-->

                <!--INICIO MODAL INGRESAR NUEVA TAREA-->
                <div id="modal_ingresar_tarea_id" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"></div>
                <!--FIN MODAL INGRESAR NUEVA TAREA-->       

                <!--INICIO MODAL DESCRIPCION TAREA-->
                <div id="modal_descripcion_tarea_id" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModal3Label"></div>
                <!--FIN MODAL INGRESAR NUEVA TAREA-->           

                <!--INICIO MODAL HISTORIAL-->
                <div id="modal_historial_tarea_id"  class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModal5Label"></div>
                <!--FIN MODAL HISTORIAL-->          

                <!--INICIO MODAL ELIMINAR TAREA-->
                <div id="modal_eliminar_tarea_id" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
                <!--FIN MODAL ELIMINAR TAREA-->

                <!--INICIO MODAL SELECCIONAR UNA TAREA-->
                <div id="modal_seleccionar_tarea_id"  class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModal2Label"></div>
                <!--FIN MODAL ELIMINAR TAREA-->             

                <!--INICIO MODAL ELIMINAR TAREA-->
                <div id="modal_descripcion_problema_id"  class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModal4Label"></div>
                <!--FIN MODAL ELIMINAR TAREA-->                         
        </div>





<!--JQUERY-->
<script src="js/jquery-1.11.2.min.js"></script>     
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap-select.min.js"></script>
<script src="js/datepicker.js"></script>


<script src="js_propio/bootstrap-datepicker.js"></script>
<script src="js_propio/bootstrap-selectpicker.js"></script>
<script type="text/javascript" src="choona.js/choona.js"></script>
<script type="text/javascript" src="js/head.load.js"></script>
<script src="modulos/MainTarea.js"></script>

<script type="text/javascript">

$(document).ready( function() {

choona.startApp({
id : "main_container_id",
module : MainTarea,
config :  {
'urlBase' : ''
}
});
});

</script>
<!--FIN JQUERY-->

</body>
</html>

I don't know How I do this correctly. sorry my english.

edit : I always need return $msj.

Upvotes: 3

Views: 362

Answers (3)

KAD
KAD

Reputation: 11112

First you need to make formulario_lazos.html a .php file in order to use php code within it.

In the if condition where $msg == "si" you need to use session_start() to start the session, then use the header("Location: ...") to redirect to your page. You can attach $msg to the link in location and use it later on in the landing page using $_GET

     <?php

      require "dao/daoLoginUsu.php";  

      class LoginUsuario{

public function setDatos($aInput) {

    $obj = json_decode($aInput, true);

   $Dao = new daoLoginUsuario();
   $Dao->setDataDato($obj);

   $msj = $Dao->setDataDato($obj);


  if($msj === 'si'){
    session_start();
    header('Location: http://localhost:8080/formulario_web
   /formulario/formulario_lazos.php?msg='.$msg );

  }else{
    header('Locarion: another_location.html');
  }
      } 
     }
  ?>

Inside formulario_lazos.php, you check if the session has been started by adding a php block at the top of the html code where you make sure that the session status is not NONE, if so you redirect to another page :

       <?php

   if (session_status() == PHP_SESSION_NONE) {
header('Location: another_page.php');
  }

echo $_GET['msg'];
  ?>

Upvotes: 1

Qirel
Qirel

Reputation: 26460

An example of how you can do it:

<?php
session_start();
if ($msj === 'si') {
    $_SESSION['msj'] = "si";
    return $msj;
    header("Location: /formulario_lazos.html");
    exit; // Exits the script, redirecting the user to the page above
}

And in your formulario_lazos.html you'll need

<?php
    session_start();
    if ($_SESSION['msj'] == "si") {
?> 
<!-- PUT YOUR HTML CODE FROM formulario_lazos.html HERE -->
<?php
    } else {
        echo "No session was set, you can't read this page!";
    }
?>

Remember that when you're using header(Location: ...); you can't have any outputs (whitespaces, HTML or echo in PHP) before the header, otherwise it will not work - and add a PHP Warning in your error_log. If you need to redirect after outputs, you'll need another way of redirecting the user. The same thing applies for session_start(); - it has to be called before any output (which is fine, there's no reason why you could put it later anyway - just put it after opening your PHP-tag).

Also, your formulario_lazos.html file has to be a .php file (not .html) if you need to use PHP inside this file.

Upvotes: 1

Petko Kostov
Petko Kostov

Reputation: 367

Try this: in controllerLoginUsu.php

<?php

require "dao/daoLoginUsu.php";  

class LoginUsuario{

    public function setDatos($aInput) {

        $obj = json_decode($aInput, true);

       $Dao = new daoLoginUsuario();
       $Dao->setDataDato($obj);

       $msj = $Dao->setDataDato($obj);


      if($msj === 'si'){
        if(empty(session_id())) //if not started we start it
             session_start();
         header('Location: http://localhost:8080/formulario_web/formulario/formulario_lazos.php' );     
      }else{
        return $msj;
      }
   } 
}
?>

Then make formulario_lazos a php instead html file as we will need to check in its beginning for the session. Just put this code at the very beginning of the file

if(empty(session_id()))
  exit('No direct url access');

So this way everytime someone is trying to open the formulario_lazos.php we will check if we have open session and show it only if we have one.

Upvotes: 0

Related Questions