是否可以将PHP imap流存储为会话变量?

时间:2014-10-10 14:43:49

标签: php email imap

我有以下代码打开了一个imap流:

<?php
    session_name('mysession');
    session_start();

    $mailbox = "{my_mailbox_name}";

    $username = (isset($_POST['username'])) ? $_POST['username'] : null;
    $password = (isset($_POST['password'])) ? $_POST['password'] : null;
    $_SESSION['loggedin'] = false;
    if(isset($username) && isset($password)){
        if($_SESSION['imapStream'] = imap_open($mailbox,$username,$password)){
            $_SESSION['loggedin'] = true;
        } else {
            $_SESSION['imapStream'] = null;
        }
    }
    $errorArray = imap_errors();
    header("Location: ./");
    exit;
?>

但是,每当我尝试在代码中的其他地方使用$ _SESSION ['imapStream']变量时,我都会遇到错误:

Warning: imap_num_msg() expects parameter 1 to be resource, integer given in /www/...

Warning: imap_fetch_overview() expects parameter 1 to be resource, integer given in /www/...

Warning: imap_close() expects parameter 1 to be resource, integer given in /www/...

我尝试过序列化和反序列化变量,但这也不起作用。有什么建议吗?

编辑:在我的其他代码中添加了一个如何使用它的示例:

<?php
    session_name('mysession');
    session_start();

    if(isset($_SESSION['loggedin']) && isset($_SESSION['imapStream'])){
        $number_of_messages = imap_num_msg($_SESSION['imapStream']);
        $msgArray = imap_fetch_overview($_SESSION['imapStream'], "1:{$number_of_messages}");
        imap_close($_SESSION['imapStream']);
    }
?>

0 个答案:

没有答案