首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的$_SESSION变量消失了?

为什么我的$_SESSION变量消失了?
EN

Stack Overflow用户
提问于 2015-11-14 06:15:27
回答 1查看 39关注 0票数 0

我有一个函数

代码语言:javascript
运行
复制
$capdir = get_template_directory_uri() . '/assets/captcha/';
$capmap = array(
    'cat' => $capdir . 'Captcha_Cat.png',
    'dog'  => $capdir . 'Captcha_Dog.png',
    'fish' => $capdir . 'Captcha_Fish.png'
);

function set_animal_captcha ( )
{
    global $capmap;
    // returns image url of random animal and stores in session storage
    // a reference to that animal
    $randAnimal = array_rand($capmap, 1);
    $_SESSION['animal'] = $randAnimal;
    die($capmap[$randAnimal]);
}

这是在页面加载时触发的,我已经确认它工作成功。然后我有一个函数

代码语言:javascript
运行
复制
add_action('wp_ajax_nopriv_contact', 'contact');
add_action('wp_ajax_contact', 'contact');

// $nameMap is a map of input names to table cells in the email
// e.g. <input name="email" value="myemail@gmail.com"/> gets made into the row <tr><td><b>Email Address:<b></td><td>myemail@gmail.com</td></tr>
// by the mapping 'email' => 'Email Address' in the array 
$nameMap = array(
    'name' => 'Name', 
    'email' => 'Email Address', 
    'phone' => 'Phone Number', 
    'comments' => 'Stuff in text box'
);

// https://css-tricks.com/sending-nice-html-email-with-php/
$headers = "MIME-Version: 1.0\r\nContent-Type: text/html; charset=ISO-8859-1\r\n"; 
function contact ( )
{
    global $nameMap, $headers;

    $info = array(
        // name good as long as its not empty
        'validName' => (strlen(trim($_POST['name'])) > 0 ? true : false), 
        // http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address            
        // http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation
        'validEmail' => (preg_match("!^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$!", trim($_POST['email'])) == 1 ? true : false), 
        'validPhoneNumber' => (preg_match("!^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$!",trim($_POST['phone'])) == 1 ? true : false), 
        // if answer to captch equals animal in session storage
        'correctAnimal' => ($_SESSION['animal'] === trim(strtolower($_POST['capanswer']))) 
    );
    // see http://stackoverflow.com/questions/482377/php-regex-delimiter-whats-the-point about why "!" is around the regexes
    if (!$info['validName'] || !$info['validEmail'] || !$info['validPhoneNumber'] || !$info['correctAnimal'] ) // if invalid name, email or phone number
    {
        $info['sentEmail'] = false;
    } else {
        // inputs contained valid values
        // contstruct email body
        $emailMsg = '<html><body><h3>Someone submitted a contact form ...</h3><table>';
        foreach ($_POST as $key => $value) if (array_key_exists($key, $nameMap)) $emailMsg .= '<tr><td><b>' . $nameMap[$key] . ':</b></td><td>' . $value . '</td></tr>';
        $emailMsg .= '</table></body></html>';

        // attempt to send email and set 'sentEmail' key accordingly
        $info['sentEmail'] = mail("myemail@gmail.com", "A Comment Was Submitted",$emailMsg,$headers) ? true : false;
    }

    echo json_encode($info); 
    die();
}

它会在页面上提交表单时触发,但我已经计算出

代码语言:javascript
运行
复制
$_SESSION['animal']

在触发该函数时未定义。造成这种情况的可能原因是什么?

(哦,是的,正如你所看到的,我总是注释掉对Stack Overflow帖子的引用,并将其复制粘贴到生产代码中哈哈)

EN

回答 1

Stack Overflow用户

发布于 2015-11-14 06:18:21

我要检查的第一件事是客户端上是否禁用了cookies。

如果没有cookie,我相信会话将驻留在服务器的内存中,以便在最初请求时完全执行脚本--然后在发出下一个页面请求之前消失。

你的整个$_SESSION正在消失,还是只是“动物”?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33702657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档