douji8033 2018-09-09 13:23
浏览 91
已采纳

当我在imap授权的登录表单中使用错误的密码和电子邮件时,如何避免警告?

How to avoid this warning and notices when i use wrong password and email in my login form in imap_open:

Warning: 'imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX' and

Notice: Unknown: IMAP protocol error: Client aborted AUTHENTICATE command. i145-v6mb122753825lfi (errflg=2) in Unknown on line 0

Notice: Unknown: IMAP Authentication cancelled (errflg=2) in Unknown on line 0

I need some check when i use wrong password and email, close imap connection and echo smth like this 'wrong password or email!'. I tried to evoid this, but it is not working. Please tell me, what i am doing wrong.

function imap_open:

function open_mailbox($auth_user, $auth_password) {
    $mailbox = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
    if($imap = imap_open($mailbox, $auth_user, $auth_password)){
        imap_open($mailbox, $auth_user, $auth_password);
        echo "success";
    } else {
        imap_close($imap);
        echo 'fail';
    }
}

function of login form

function display_login_form($action) { ?>
<form method="post" action="index.php?action=log-in">
    <div>
        <div>
            <div>
                <p>LOGIN</p>
            </div>
            </br>
            <div>
                <div>
                    <div>
                        <p>MAIL</p>
                    </div>
                    <div>
                        <input type="text" name="username" required>
                    </div>
                </div>
                </br>
                <div>
                    <div>
                        <p>PASSWORD</p>
                    </div>
                    <div>
                        <input type="password" name="password" required>
                    </div>
                </div>
                </br>
                <div>
                    <input type='submit' value='LOGIN'>
                </div>
            </div>
        </div>
    </div>
</form>

index.php

$username = "";
$password = "";
if(isset($_POST['username']) && !empty($_POST['username'])){
    $username = $_POST['username'];
}
if(isset($_POST['password']) && !empty($_POST['password'])){
    $password = ($_POST['password']);
}
if(null!==($username && $password)) {
    if(open_mailbox($username, $password)) {
        $action = 'view-mailbox';
        $_SESSION['auth_user'] = $username;
        $_SESSION['auth_password'] = $password;
    } else {
        $status .= "<p>Wrong password or email</p>";
   }
}
  • 写回答

1条回答 默认 最新

  • dongzhe3171 2018-09-09 15:59
    关注

    You can use imap_errors() to get rid of warnings and notices:

    When imap_errors() is called, the error stack is subsequently cleared.

    Source

    Simply call it before calling imap_close().


    Also note, that you call imap_open() twice in this block, once in the assignment inside the if condition, and once right after it:

    if($imap = imap_open($mailbox, $auth_user, $auth_password)){
        imap_open($mailbox, $auth_user, $auth_password);
        echo "success";
    }
    

    This is not necessary. If the $imap = imap_open($mailbox, $auth_user, $auth_password) assignment does not return FALSE, you do not need to open the imap once more.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题