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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。