dongyo1818 2014-10-05 23:35
浏览 37

为什么我在防止双重提交时得到同样的回声?

Hello i have my form and i want to implement this method for preventing double submitting on forms or going back The simple code:

 <?php
            // start session
            session_start();

            // create unique token
            $form_token = uniqid();

            // commit token to session
            $_SESSION['user_token'] = $form_token;
    ?>
    <!doctype html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Prevent multiple POSTs </title>
    </head>

    <body>

    <form action="submit.php" method="post">

        <input type="text" name="bar" />    
        <input type="hidden" name="user_token" value="<?php echo  $_SESSION['user_token'];  ?>" />
        <input type="submit" value="Submit" />

    </form>
    </body>
    </html>

And here the submit.php code:

       <?php 
        session_start();
    //We check if the token of the page and session match!
    if($_POST['user_token'] == $_SESSION['user_token']) {
        $message = 'Your download is <a href="foobar.xls"> Here </a> ';
    } else {
        $message = 'Your request has expired, please go back and resubmit!';
    }

    echo "We say: " . $message;


// invalidate the token so it expires on view, important!
unset($_SESSION['user_token']);
?> 

I uploaded this script to my server but whenever i click the submit button i get the same answer the We say: Your download is Here echo , why this i checked if my cookies are created and yes they exists as they should ,

i want that users that have submitted the form once , to cant do that another time until a refresh or setting a maximum time that these cookies can exists and than delete them so that the user after for eg 10 min can submit another form if he want so

Any help will greatly be welcomed. Thanks in advance. Updatet

  • 写回答

1条回答 默认 最新

  • dongshanya2008 2014-10-05 23:54
    关注
    session_start();
    

    Add this in the begining of second script which recive your data from form. More detailes in manual.

    But the only way to recieve msg 'Your request has expired' you need to keep recieved data in some form on server (file or data base) I suppose.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题