This code snippet shows how IMAP authentication can be used to give a user access to an application.
Usage
<?php
$imapAuth = new ImapAuthentication('ssl://imap.gmail.com', 993);
$auth = $imapAuth->authenticate('username', 'password');
if($auth){
echo 'Login Success';
}else{
echo 'Login failed';
}
?>
ImapAuthentication.php
<?php
class ImapAuthentication {
protected $con;
public function __construct($host, $port){
$this->con = fsockopen($host, $port, $errno, $errstr, 30);
$this->getResponse();
}
public function authenticate($username, $password){
fwrite($this->con, 'A1 LOGIN ' . $username . ' ' . $password . PHP_EOL);
return $this->getResponse();
}
protected function getResponse(){
while(true){
$line = fgets($this->con);
$segments = explode(' ', $line);
if($segments[1] =='OK'){
return true;
}elseif($segments[1] =='NO'){
return false;
}
}
}
}
?>
No comments:
Post a Comment