duanhui3759 2016-07-12 17:48
浏览 32

在if(){}中取消设置($ _ SESSION ['input_name'])是否清除了整个脚本中的变量?

I've made the code to prevent repeating send of form. According to this code the sequence of actions is followed:
1.User submits mail form.
2.Page sends form data. Session starts. All form data are assigned to global $_SESSION variables. Mail is sent by function mail(). Variable $_SESSION['sent'] = true; is created to echo "Thank you for contacting us!".
3.Then "if" check out $_SESSION['sent'] and get that it's true...
and then the problem is arisen!
Because as I understand the next action has to be echoing "Thank you for contacting us!". And then unset() has to destroy $_SESSION['sent'] to prevent the repeat of showing "Thank ..."
But it doesn't happen and the whole contact form is shown on screen, which has to be shown only the case if $_SESSION['sent'] is not set.
So, the question is does unset($_SESSION['sent']) in if(){} destroy $_SESSION['sent'] in whole script?

<?php 
/* code for mailing and redirecting to the same page */
session_start();
if ( !empty( $_POST['tosendmail'] ) ) {
    $_SESSION['admin_email'] = "mymail@mail.com";
    $_SESSION['subject'] = $_REQUEST['subject'];
    $_SESSION['email'] = $_REQUEST['email'];
    $_SESSION['message'] = $_REQUEST['message'];    
    $send = mail( $_SESSION['admin_email'], $_SESSION['who'], $_SESSION['message'], "From:" . $_SESSION['email'] );
    /* create variable $_SESSION['sent'] to use condition if ( isset( $_SESSION['sent'] ) ) below */
    if ( $send ) {
        $_SESSION['sent'] = true;
    }
    header( "Location: " . $_SERVER["REQUEST_URI"] );  
}

if ( isset( $_SESSION['sent'] ) ) {
    ?>
    <h3 class="page-title">Thank you for contacting us!</h3>
    <?php       
        unset( $_SESSION['sent'] ); 
} else {
?>

    <!-- CONTACT FORM -->
    <h3 class="page-title">Contact us:</h3>
    <form class="contact-form" method="post">
        Your name:<br>
        <input type="text" name="subject" value=""><br>
        E-mail:<br>
        <input type="email" name="email" value=""><br>      
        Your message:<br>
        <textarea type="text" name="message" rows="10" cols="40"></textarea><br><br>
        <input type="submit" name="tosendmail" value="Send">
        <input type="reset" value="Reset"> 
    </form>  
<?php }
 ?>
  • 写回答

2条回答 默认 最新

  • duanchuli5647 2016-07-12 17:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端