如何嵌套if(isset($ _ POST ['submit']))(还有tokens)

时间:2017-11-11 00:37:27

标签: php submit form-submit isset

我有这个代码,它工作正常:

  if (isset($_POST['submit1']))
    {
        if($_SESSION['user_token'] == $_POST['user_token']) {
            unset($_SESSION['user_token']);
            include_once('./token.php');
            include_once('./my2page.php'); //**PAGE WITH SUBMIT2**
        } else {
          header("location: ./index.php");
        }
    } else {
        include_once('./token.php');
        include_once('./my1page.php'); //**PAGE WITH SUBMIT1**
    }

token.php

$form_token = uniqid();
$_SESSION['user_token'] = $form_token;

my1page.php中的表单包含:

<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token'];?>">

现在我需要嵌套第二个if isset submit(必须在上次提交中取消设置令牌)。

我没有成功的尝试

if(isset($_POST['submit'])){
    $_SESSION['submit']=true;
}

if (isset($_POST['submit']) || ( isset($_SESSION['submit']) && $_SESSION['submit'])) {
   if($_SESSION['user_token'] == $_POST['user_token']) {
       if (isset($_POST['submit1'])) {
           if($_SESSION['user_token'] == $_POST['user_token']) {
                unset($_SESSION['user_token']);
                $_SESSION['submit']=false;
                include_once('./script/token.php');
                include_once('./my3page.php');
           } else {
            header("location: ./3.php");
           }
       }
       include_once('./my2page.php');
   } else {
      header("location: ./index.php");
   }
} else {
    include_once('./token.php');
    include_once('./my1page.php');
}

4 个答案:

答案 0 :(得分:0)

HTTP无法正常工作。这意味着这里发生的事情如下:

  1. 用户首次调用此页面。他发送了一个GET请求,因此isset($_POST['submit1'])是假的。
  2. 现在他点击提交并发送第一个POST请求。 (我假设你在那里设置了submit1的值。)isset($_POST['submit1'])为真,并返回my2page.php
  3. 他发送第三个请求。同样是一个POST请求,但这次带有submit2的值。您的服务器模板引擎开始评估php。 isset($_POST['submit1'])为false,因此返回旧的my1page.php
  4. 基本上,不要嵌套支票,而是使用it else代替。 (把它想象成一个开关/案例

    isset($_POST['submit1']) → ./my2page.php
    isset($_POST['submit2']) → //end page
    none                     → ./my1page.php
    

答案 1 :(得分:0)

你不能同时提交2个提交,所以这里发生的是

if(condition){
   if(!condition){
      //do somthing
   }
}

这将永远不会尝试使用另一个页面或我建议保存$ _SESSION中的第一个提交;

添加此lign

$_SESSION['submit1'] = (isset($_POST['submit1']))? true: false;

而不是改变第一个条件

if (isset($_POST['submit1']) || $_SESSION['submit1']) {
   if($_SESSION['user_token'] == $_POST['user_token']) {
       if (isset($_POST['submit2'])) {
           if($_SESSION['user_token'] == $_POST['user_token']) {
                unset($_SESSION['user_token']);
                $_SESSION['submit1']=false; //or unset($_SESSION['submit1']);
                //DO SOMETHINGS
           } else {
            header("location: ./index.php");
           }
       }    
       include_once('./my2page.php'); //**PAGE WITH SUBMIT2** 
   } else {
      header("location: ./index.php");
   }
} else {
    include_once('./token.php');
    include_once('./my1page.php');
}

答案 2 :(得分:0)

应该是这样的;

if(isset($_POST['submit1'])){
    $_SESSION['submit1']=true;
}

if (isset($_POST['submit1']) || $_SESSION['submit1']) {
   if($_SESSION['user_token'] == $_POST['user_token']) {
       if (isset($_POST['submit2'])) {
           if($_SESSION['user_token'] == $_POST['user_token']) {
                unset($_SESSION['user_token']);
                $_SESSION['submit1']=false; //or unset($_SESSION['submit1']);
                //DO SOMETHINGS
           } else {
            header("location: ./index.php");
           }
       }    
       include_once('./my2page.php'); //**PAGE WITH SUBMIT2** 
   } else {
      header("location: ./index.php");
   }
} else {
    include_once('./token.php');
    include_once('./my1page.php');
}

答案 3 :(得分:0)

你的关闭你可以改变这个

if (isset($_POST['submit1'],$_POST['submit2'])) { //check isset on both
    if($_SESSION['user_token'] == $_POST['user_token']) {
       if (isset($_POST['submit2'])) {
//         if($_SESSION['user_token'] == $_POST['user_token']) { <--redundant check
           unset($_SESSION['user_token']);
           //DO SOMETHINGS
       }    
       include_once('./my2page.php'); //**PAGE WITH SUBMIT2** 
   } else {
      header("location: ./index.php");
   }
} else {
    include_once('./token.php');
    include_once('./my1page.php');
}

根据您是否需要ANDOR,上述内容与此相同

if (isset($_POST['submit1']) && isset($_POST['submit2'])) {

显然,如果你想要一个,或者只是把它放在这里

if (isset($_POST['submit1']) || isset($_POST['submit2'])) {

目前尚不清楚你是在谈论2个独立或同时发布的帖子