Code: Select all
if (!isset($this->config->get['secret_token'])) {
$random_number = bin2hex(random_bytes(14));
$data['secret_token'] = $random_number;
}
oc 3.0.3.8
Code: Select all
if (!isset($this->config->get['secret_token'])) {
$random_number = bin2hex(random_bytes(14));
$data['secret_token'] = $random_number;
}
Got an urgent question that’s keeping you up at night? There might just be a magical inbox ready to help: khnaz35@gmail.com
Enjoy nature
Code: Select all
// Check if the token exists in the database settings
$secret_token = $this->config->get('secret_token');
if (!$secret_token) {
// If it doesn't exist, generate a new one
$secret_token = bin2hex(random_bytes(14));
// Save the generated token in the database
$this->load->model('setting/setting');
$this->model_setting_setting->editSettingValue('your_module_name', 'secret_token', $secret_token);
}
$data['secret_token'] = $secret_token;
Got an urgent question that’s keeping you up at night? There might just be a magical inbox ready to help: khnaz35@gmail.com
Enjoy nature
I'm trying to use your code, but still secret token will be regenerated when the page.khnaz35 wrote: ↑Tue Oct 17, 2023 8:45 pmSave the token in the database:
If you're setting the value in the admin settings, you will typically save this in the setting table of OpenCart. If you're using the admin settings controller, there is usually a save function that saves all the settings into the database.
Retrieve the token from the database:
Use $this->config->get('your_setting_key') to get the value from the database.
Here's how you can adapt your code:
Code: Select all
// Check if the token exists in the database settings $secret_token = $this->config->get('secret_token'); if (!$secret_token) { // If it doesn't exist, generate a new one $secret_token = bin2hex(random_bytes(14)); // Save the generated token in the database $this->load->model('setting/setting'); $this->model_setting_setting->editSettingValue('your_module_name', 'secret_token', $secret_token); } $data['secret_token'] = $secret_token;
Dev. wrote: ↑Tue Oct 17, 2023 8:23 amI want to generate a random value for the password. I'm using below code:
Value will be generated But when the page is refreshed will generate a new one. How can save the first value and not change it when the page is refreshed?Code: Select all
if (!isset($this->config->get['secret_token'])) { $random_number = bin2hex(random_bytes(14)); $data['secret_token'] = $random_number; }
oc 3.0.3.8
Code: Select all
if (!isset($this->config->get['secret_token'])) {
$random_number = bin2hex(random_bytes(14));
$this->session->data['secret_token'] = $random_number;
}
if (isset($this->session->data['secret_token'])) {
$data['secret_token'] = $this->session->data['secret_token'];
unset ($this->session->data['secret_token']);
} else {
$data['secret_token'] = '';
}
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
still get same things, after refresh page will be regenerate new tokenstraightlight wrote: ↑Wed Oct 18, 2023 7:02 amDev. wrote: ↑Tue Oct 17, 2023 8:23 amI want to generate a random value for the password. I'm using below code:
Value will be generated But when the page is refreshed will generate a new one. How can save the first value and not change it when the page is refreshed?Code: Select all
if (!isset($this->config->get['secret_token'])) { $random_number = bin2hex(random_bytes(14)); $data['secret_token'] = $random_number; }
oc 3.0.3.8Code: Select all
if (!isset($this->config->get['secret_token'])) { $random_number = bin2hex(random_bytes(14)); $this->session->data['secret_token'] = $random_number; } if (isset($this->session->data['secret_token'])) { $data['secret_token'] = $this->session->data['secret_token']; unset ($this->session->data['secret_token']); } else { $data['secret_token'] = ''; }
Why not to do like?straightlight wrote: ↑Wed Oct 18, 2023 7:02 amCode: Select all
if (!isset($this->config->get['secret_token'])) { $random_number = bin2hex(random_bytes(14)); $this->session->data['secret_token'] = $random_number; } if (isset($this->session->data['secret_token'])) { $data['secret_token'] = $this->session->data['secret_token']; unset ($this->session->data['secret_token']); } else { $data['secret_token'] = ''; }
Code: Select all
if (!isset($this->config->get['secret_token']) && !isset($this->session->data['secret_token'])) {
$random_number = bin2hex(random_bytes(14));
$this->session->data['secret_token'] = $random_number;
}
if (isset($this->session->data['secret_token'])) {
$data['secret_token'] = $this->session->data['secret_token'];
} else {
$data['secret_token'] = '';
}
Got an urgent question that’s keeping you up at night? There might just be a magical inbox ready to help: khnaz35@gmail.com
Enjoy nature
It's working nowkhnaz35 wrote: ↑Wed Oct 18, 2023 12:37 pmWhy not to do like?straightlight wrote: ↑Wed Oct 18, 2023 7:02 amCode: Select all
if (!isset($this->config->get['secret_token'])) { $random_number = bin2hex(random_bytes(14)); $this->session->data['secret_token'] = $random_number; } if (isset($this->session->data['secret_token'])) { $data['secret_token'] = $this->session->data['secret_token']; unset ($this->session->data['secret_token']); } else { $data['secret_token'] = ''; }
Code: Select all
if (!isset($this->config->get['secret_token']) && !isset($this->session->data['secret_token'])) { $random_number = bin2hex(random_bytes(14)); $this->session->data['secret_token'] = $random_number; } if (isset($this->session->data['secret_token'])) { $data['secret_token'] = $this->session->data['secret_token']; } else { $data['secret_token'] = ''; }
Got an urgent question that’s keeping you up at night? There might just be a magical inbox ready to help: khnaz35@gmail.com
Enjoy nature
The goal was to use $this->session->data to answer the initial question. Then, to let the user manipulate the code the way he wanted.khnaz35 wrote: ↑Wed Oct 18, 2023 12:37 pmWhy not to do like?straightlight wrote: ↑Wed Oct 18, 2023 7:02 amCode: Select all
if (!isset($this->config->get['secret_token'])) { $random_number = bin2hex(random_bytes(14)); $this->session->data['secret_token'] = $random_number; } if (isset($this->session->data['secret_token'])) { $data['secret_token'] = $this->session->data['secret_token']; unset ($this->session->data['secret_token']); } else { $data['secret_token'] = ''; }
Code: Select all
if (!isset($this->config->get['secret_token']) && !isset($this->session->data['secret_token'])) { $random_number = bin2hex(random_bytes(14)); $this->session->data['secret_token'] = $random_number; } if (isset($this->session->data['secret_token'])) { $data['secret_token'] = $this->session->data['secret_token']; } else { $data['secret_token'] = ''; }
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Users browsing this forum: Amazon [Bot], Bing [Bot], Majestic-12 [Bot] and 64 guests