dongluo9156 2016-01-21 16:25
浏览 5

php表单仅适用于邮寄的人

Im basically trying what I say in the title, that is:

I have a php file where you can put your email. After that, you receive an email with a link to other php file. This second file has a form and when you submit, a .txt is created with the information and I receive an email with that .txt.

What I want now is that only the users who click on the link that I send, get access to the form. Not just by writting on the nav http:myserverhost/mysite/myprivateform.php.

I did a research here and I guess that I have to work with sessions, like I use to, but now I need to avoid the information stored on the server.

How can I start a session for the user can access to the form for 1 hour for example, after clicking on the url.

Thanks,

  • 写回答

1条回答 默认 最新

  • dongmu1914 2016-01-21 17:13
    关注

    You can do it with the $_SESSION variable. Useful functions are: session_start and session_destroy. I would create a class like this if I were you:

    class SessionHandler {
    
        public static function start() {
            if (session_status() == PHP_SESSION_NONE) {
                session_start();
            }
            return true;
        }
    
        public static function destroy() {
            if (session_status() != PHP_SESSION_NONE) {
                session_destroy();
            }
        }
    
        public static function logIn($params) {
            start();
            $_SESSION["email"] = $params["email"];
            $_SESSION["token"] = $params["token"];
            $_SESSION["expiry"] = time() + 3600; //an hour later
        }
    
        public static function isValid() {
            return ((session_status() != PHP_SESSION_NONE) && 
                    (intval($_SESSION["expiry"]) > time()));
        }
    
        public static function validate() {
            $valid = self::isValid();
            if (!$valid) {
                session_destroy();
            }
            return $valid;
        }
    
    }
    

    So, you need to generate a token and use logIn to link it to email address as well. Use validate/isValid to determine whether the user should have access to something.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程