duanpacan9388 2015-06-18 10:10
浏览 65
已采纳

html表单中的action属性是否会干扰$ _SESSION全局变量中存储的内容?

Suppose, I have two pages: page01.php and page02.php (their code is presented below).

If I leave action attribute in the form on page01.php empty (i.e. action=""), then access page01.php, fill in the form, press submit, then access page02.php - it all works fine (i.e. $_SESSION variable stores the data submitted on page01.php and can be accessed and viewed on page02.php as expected).

However, when I try to make the form send the user to page02.php (by changing the action atrribute to action="page02.php") it looks like the $_SESSION global variable doesn't store data from page01.php.

My question is: does this happen because the user is redirected to page02.php immediately upon submission of form and the code between php tags on page01.php does not get executed?

I'm aware I can use $_GET or $_POST on page02.php to achieve the desired behavior, but I'm just trying to understand the way action attribute and $_SESSION interact. Thank you.

Page01.php:

<html>
<head>
    <title>Page 01</title>      
</head>
<body>
    <h1>Please fill this form</h1>
    <form action="" method="post">
        Name:  <input name="username">
        <input type="submit" value="send">
    </form>

    <?php   
        session_start();
        if(isset($_POST['username'])) {
            $_SESSION['username']=$_POST['username'];
        }
    ?>
</body>

Page02.php:

<html>
<head>
    <title>Page 02</title>      
</head>
<body>
    <h1>Another temporary page is working</h1>
    <?php
        session_start();
        $expectedName = "Bob";
        if($_SESSION['username'] == $expectedName) {
            echo "Welcome, Bob!";
        }
        else {
            echo "Access denied. You are ". $_SESSION['username']  . ", not Bob.";
        }
    ?>
</body> 

  • 写回答

1条回答 默认 最新

  • dpe77294 2015-06-18 10:16
    关注

    action attribute is attribute you specify where the script that will need to be run after submitting.

    so if you want user to be redirected after form has been submitted, you use header() function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向