我正在使用openart 3浏览器关闭客户退出,如何阻止客户退出,即使浏览器关闭



我使用openart 3我的问题是,当客户关闭浏览器并再次打开客户注销,我想改变这种行为,我需要当客户一旦登录他/她不应该注销,直到他/她点击注销按钮,即使他/她关闭浏览器并再次打开他/她应该保持登录。

你必须在OpenCart中修改3个文件来完成这个。

/目录/控制器/账户/login。

在登录过程中,您必须在cookie中存储客户ID和email。加密存储它们是值得的。电子邮件是不够的,因为您必须检查存储的客户ID是否属于存储的电子邮件。

public function index() {
[...]
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
// Unset guest
unset($this->session->data['guest']);
// store customer ID and email encrypted
$my_customer_id = $this->customer->getId();
$my_customer_id_crypted = $this->encrypt($my_customer_id, "your_key_for_customer_id_encryption");
$my_email = $this->request->post['email'];
$my_email_crypted = $this->encrypt($this->request->post['email'], "your_key_for_email_encryption");
setcookie("MyCustomerID", $my_customer_id_crypted , time() + (365 * 24 * 60 * 60) , "/");
setcookie("MyEmail", $my_email_crypted , time() + (365 * 24 * 60 * 60) , "/");
[...]
}
[...]
}
[...]
// https://www.phpcluster.com/simple-two-way-encryption-in-php/
// you can use other encryption if you want, just an example
protected function encrypt($plainText, $key) {
$secretKey = md5($key);
$iv = substr( hash( 'sha256', "aaaabbbbcccccddddeweee" ), 0, 16 );
$encryptedText = openssl_encrypt($plainText, 'AES-128-CBC', $secretKey, OPENSSL_RAW_DATA, $iv);
return base64_encode($encryptedText);
}

/目录/控制器/账户/logout.php

在登出过程中,您必须删除客户ID和电子邮件cookie

public function index() {
if ($this->customer->isLogged()) {
$this->customer->logout();
// delete cookies
unset($_COOKIE['MyCustomerID']);
unset($_COOKIE['MyEmail']);
setcookie("MyCustomerID", "", 0, "/");
setcookie("MyEmail", "", 0, "/");
[...]
}
[...]
}

/目录/控制器/共同/footer。php

在这个文件中,如果一切正常,你可以自动登录客户并延长cookie的生存期,页脚在每次页面加载时使用所以这是一个好方法,我的意思是

public function index() {
[...]
$data['scripts'] = $this->document->getScripts('footer');
$data['styles'] = $this->document->getStyles('footer');
if (isset($_COOKIE["MyCustomerID"]) && isset($_COOKIE["MyEmail"]) && $_COOKIE["MyCustomerID"] != '' && $_COOKIE["MyEmail"] != '') {
$my_customer_id_crypted = $_COOKIE["MyCustomerID"];
$my_customer_id = $this->decrypt($my_customer_id_crypted, "your_key_for_customer_id_encryption");
$my_email_crypted = $_COOKIE["MyEmail"];
$my_email = $this->decrypt($my_email_crypted, "your_key_for_email_encryption");
$config = new Config();
$config->load('default');
if ( $my_customer_id != "" && $my_email != "" && $my_customer_id == (int)$my_customer_id ) {
if ( !$this->customer->isLogged() ) {          
if ( $my_customer_id == $this->getCustomerIdByEmailAddress( $my_email ) ) { // auto login, when customer ID belongs to this email address
$this->customer->login($my_email, "", true); // we use OpenCart override log in method
//$this->log->write('customer logged in automatically');
$this->load->model('account/address');
if ($this->config->get('config_tax_customer') == 'payment') {
$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
if ($this->config->get('config_tax_customer') == 'shipping') {
$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
// extend cookies lifetime
setcookie("MyCustomerID", $my_customer_id_crypted , time() + (365 * 24 * 60 * 60) , "/");
setcookie("MyEmail", $my_email_crypted , time() + (365 * 24 * 60 * 60) , "/");
$this->response->redirect($_SERVER['REQUEST_URI']);
}
}
}
}
[...]
}
// https://www.phpcluster.com/simple-two-way-encryption-in-php/
// decrypt function for previous used encryption
protected function decrypt($encryptedText, $key) {
$key = md5($key);
$iv = substr( hash( 'sha256', "aaaabbbbcccccddddeweee" ), 0, 16 );
$decryptedText = openssl_decrypt(base64_decode($encryptedText), 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
return $decryptedText;
}
protected function getCustomerIdByEmailAddress($email) {
$sql_txt = "";
$sql_txt .= "SELECT customer_id";
$sql_txt .= "  FROM ".DB_PREFIX."customer";
$sql_txt .= " WHERE LOWER(email) = '".$this->db->escape(utf8_strtolower($email))."'";
$customer_query = $this->db->query($sql_txt);
if ($customer_query->num_rows)
{
return $customer_query->row['customer_id'];
}
else
{
return -1;
}
}

如果你愿意,你可以改进这段代码,目前我使用这个方法来自动登录客户

相关内容

最新更新