dpk20361
2014-11-07 20:55
浏览 36
已采纳

我可以在WordPress自定义页面中设置会话

I am trying to set up a login system and a contact form for front-end users of my custom WP theme. I need to create an ajax post method and send the input values to the server (Captcha and input values like name, phone, email, ... ). Now my question is, do I have to create a php session to secure unwanted form submission or passing the captcha value between two pages?

If so, is this a correct page template for a contact page and captcha page

<?php
session_start();
/*
 Template Name: Contact Page
*/

The captcha.php

<?php
session_start();

图片转代码服务由CSDN问答提供 功能建议

我正在尝试为自定义WP主题的前端用户设置登录系统和联系表单。 我需要创建一个ajax post方法并将输入值发送到服务器(Captcha和输入值,如名称,电话,电子邮件......)。现在我的问题是,我是否必须创建一个php会话来保护不需要的东西 表单提交或在两个页面之间传递验证码值?

如果是,这是联系页面和验证码页面的正确页面模板

 < 代码>&lt;?php 
session_start(); 
 / * 
模板名称:联系页面
 * / 
   
 
 

captcha.php

 &lt;?php 
session_start(); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzha5990 2014-11-07 21:06
    已采纳

    You do not need to start a session in order to send a captcha, name, phone, or email. However, if you want to remember this information as the user clicks on different pages you will need to start a session.

    It is a common convention in WordPress to start the session in your functions.php file.

    if (!session_id()) {
        session_start();
    }
    

    Now, on your contact page you can store the information after POST:

    $_SESSION['email'] = $_POST['email']
    

    And it will be available on different pages in the $_SESSION array.

    Note: validate and sanitize the $_POST variables. Look into storing a token in your database that is associated with a user id vs storing the users information in the $_SESSION array. Then store the name, phone, and email in your DB.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题