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 variable $msj
I get "si" or "no", that is, the request response.
If $msj
is "si" I need to start a session and redirect tohttp://localhost:8080/formulario_web/formulario/formulario_lazos.html
And I only need to see formulario_lazos.html
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 do not know how I do it correctly. sorry my english.
edit: I always need to return $ msj.
source to share
First, you need to create a formulario_lazos.html file .php
in order to use PHP code inside it.
In the if condition where $msg == "si"
you need to use session_start()
to start a session, then use header("Location: ...")
to redirect to your page. You can attach $msg
to a link in a location and use it later on the landing page with$_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 the session state is not NONE if you redirect to another page:
<?php
if (session_status() == PHP_SESSION_NONE) {
header('Location: another_page.php');
}
echo $_GET['msg'];
?>
source to share
An example of how you can do this:
<?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 yours formulario_lazos.html
you will 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, when you use header(Location: ...);
, before, header
you cannot have any outputs (spaces, HTML or echo in PHP), otherwise it won't work - and add PHP Warning to your error_log
. If you need to redirect after exits, you'll need another way to redirect the user. The same goes for session_start();
- it needs to be called before any exit (which is good, there is no reason you could put it later, just put it after opening your PHP tag).
Also, your file formulario_lazos.html
must be a .php file (not .html) if you need to use PHP inside that file.
source to share
Try this: in your 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 create formulario_lazos a php instead of html , as we will need to check its start for the session. Just put this code at the very beginning of the file
if(empty(session_id()))
exit('No direct url access');
This way, every time someone tries to open formulario_lazos.php, we will check if we have an open session and only show it if we have one.
source to share