Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中启动会话和重定向?_Php_Session_Login - Fatal编程技术网

如何在php中启动会话和重定向?

如何在php中启动会话和重定向?,php,session,login,Php,Session,Login,嗨,我有控制器loginusu.php: <?php require "dao/daoLoginUsu.php"; class LoginUsuario{ public function setDatos($aInput) { $obj = json_decode($aInput, true); $Dao = new daoLoginUsuario(); $Dao->setDataDato($obj);

嗨,我有
控制器loginusu.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;
      }
   } 
}
?>
我不知道我是怎么做的。对不起,我的英语不好


编辑:我总是需要返回$msj。

首先,您需要将formulario_lazos.html设置为
.php
文件,以便在其中使用php代码

在if条件下,
$msg==“si”
需要使用
会话启动()
启动会话,然后使用
标题(“位置:…”
重定向到页面。您可以将
$msg
附加到位置中的链接,然后在登录页中使用
$\u 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');
  }
      } 
     }
  ?>

在formulario_lazos.php中,通过在html代码顶部添加一个php块来检查会话是否已启动,确保会话状态不是NONE,如果是,则重定向到另一个页面:

       <?php

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

echo $_GET['msg'];
  ?>

试试这个: 在controllerLoginUsu.php中


因此,每次有人试图打开formulario_lazos.php时,我们都会检查是否有打开的会话,只有在有打开会话时才会显示它。

一个示例,说明如何执行此操作:

<?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
}
请记住,当您使用
标题(位置:…)时
标题
之前不能有任何输出(空格、HTML或PHP中的echo),否则它将不起作用-并在
错误日志
中添加PHP警告。如果需要在输出后重定向,则需要另一种重定向用户的方法。这同样适用于
session_start()-必须在任何输出之前调用它(这很好,没有理由以后再放置它-只需在打开PHP标记后放置)


此外,如果您需要在该文件中使用php,那么您的
formulario_lazos.html
文件必须是.php文件(而不是.html)。

Hey。。如果我改为formulario_lazos.php,它就可以工作,如果我不启动会话,它就不工作,但我还不能重定向。。我还不太明白,我有一个问题:session_start();如果($msj=='si'){$\u SESSION['msj']=“si”;返回$msj;header(“Location:);exit;}当然,您必须在这两个文件中
SESSION\u start();
。此外,您的
header(“Location:”);
必须在任何输出之前被调用(正如我在帖子中所写的那样)。没有HTML代码,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;
      }
   } 
}
?>
if(empty(session_id()))
  exit('No direct url access');
<?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
}
<?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!";
    }
?>