Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.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_Html_Forms_Session Variables - Fatal编程技术网

Php 限制对第二个表单页的访问

Php 限制对第二个表单页的访问,php,html,forms,session-variables,Php,Html,Forms,Session Variables,你好,我有一个多步骤表单功能 在“Step1”(第1页)中,我有一个带有三个单选按钮的表单 在第2步(第2页),我有另一个表格。 所以,我的问题是,我想通过URL限制对page2的访问,就像我在浏览器地址栏中键入:localhost/page1.php,那么它应该加载page1.php,但当我编写localhost/page2.php时,它应该限制用户对page2的访问,我想被重定向到localhost/page1.php 所以我试了一下: 第1页: <form class="type

你好,我有一个多步骤表单功能 在“Step1”(第1页)中,我有一个带有三个单选按钮的表单

在第2步(第2页),我有另一个表格。 所以,我的问题是,我想通过URL限制对page2的访问,就像我在浏览器地址栏中键入:localhost/page1.php,那么它应该加载page1.php,但当我编写localhost/page2.php时,它应该限制用户对page2的访问,我想被重定向到localhost/page1.php

所以我试了一下:

第1页:

  <form class="type" action="page2.php" method="post">
   <label style="text-align:center;"> Type de compte :  </label>
                </br> </br>
   <input  type="radio" name="typeCompte" value="enseig"  required> 
                 Enseignant </br>
   <input type="radio" name="typeCompte" value="etud" required> 
                  Etudiant </br>
   <input type="radio"  name="typeCompte" value="admin" required> 
                  Adminstrateur </br>
    </br></br>
   <input type="submit" class="make" name="submit" value="Valider">
   </form>

   <?php
    if(isset($_POST['submit']))
    {
       $_SESSION['log']=1;
    }
   ?>

完成类型:


感应剂
学生
行政官


第2页:

 <?php 
      session_start();
      if($_SESSION['log']!=1)
          header("location: page1.php");//redirecting to first page
 ?>


尝试将会话变量设置为bool-like

Page1.php中

<?php
    session_start();
    if($_POST['submit']){
        $_SESSION['log'] = "true";
         header("Location: page2.php");
  }
  ?>
 if(!isset($_SESSION['log']) && $_SESSION['log'] == "false"){
     //send them back
     header("Location: page1.php");
  }
 else{
     //your logic if any.
  }

我认为在Php会话中,可能只在真-假基础上工作。

要检查会话,可以使用isset命令

<?php session_start();

//if ! means not isset check if the session is active
if(!isset($_SESSION['log']))
header("location:page1.php");
?>

test2.php

<?php
session_start();
    if (!isset($_session['log'])){
        echo 'you are not authorised';
        header("location:test1.php");
    }
    if (isset($_session['log'])){
        echo 'you are authorised';
        }
?>
<html>
    <body>
        <form action="" method="post">      
            <input type="submit" name="destroy" value="destroy">
        </form>
    </body>
</html> 
<?php   
    if(isset($_POST['destroy'])){
        session_destroy();
            include_once("test2.php");
        exit;
    }


?>


它还向您展示了当我在page1.php中尝试此操作时如何使用会话注销用户。当我键入localhost/page2.php时,我可以访问第2页。因为您的会话值可能在浏览器中设置,所以请尝试重新启动浏览器,然后在浏览器中直接键入page2.php。别忘了把会话检查逻辑放在页面顶部。同样的问题:/但在第1页$\u会话['log']=“true”;必须在if(isset($_POST['submit'])@kahinakahina你需要以代码形式查看他的答案,如果你不理解,那么研究一下。当我尝试这项it工作时,我无法通过URL访问第2页,但即使我点击提交,我也会被重定向到第1页,而不是第2页。同样的问题(当我点击提交时,甚至无法访问第2页)尝试我编写的示例并以相同的格式实现它,注意哪里是我的php,哪里是我的htmlPut在您的第一个php页面上的一个简单条件,并检查满足的条件,如果没有填写,则只需使用php重定向到您想要的页面。看来你来stackoverflow之前还没做过研发。
session_start();
<?php
session_start();
if(isset($_POST['set'])){
    $log='1';
    $_session['log']=$log;
    include_once("test2.php");
    exit;
    }
if(isset($_POST['dontset'])){
    include_once("test2.php");
    }
?>
<html>
    <body>
        <form action="" method="post">      
            <input type="submit" name="set" value="set">
            <input type="submit" name="dontset" value="dontset">
        </form>
    </body>
</html>
<?php
session_start();
    if (!isset($_session['log'])){
        echo 'you are not authorised';
        header("location:test1.php");
    }
    if (isset($_session['log'])){
        echo 'you are authorised';
        }
?>
<html>
    <body>
        <form action="" method="post">      
            <input type="submit" name="destroy" value="destroy">
        </form>
    </body>
</html> 
<?php   
    if(isset($_POST['destroy'])){
        session_destroy();
            include_once("test2.php");
        exit;
    }


?>