Post by Dev. » Tue Oct 17, 2023 8:23 am

I want to generate a random value for the password. I'm using below code:

Code: Select all

        if (!isset($this->config->get['secret_token'])) {
            $random_number = bin2hex(random_bytes(14));
            $data['secret_token'] = $random_number;
        }
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?

oc 3.0.3.8
Last edited by Dev. on Thu Oct 19, 2023 7:24 am, edited 1 time in total.

New member

Posts

Joined
Sun Jul 17, 2022 10:02 pm

Post by khnaz35 » Tue Oct 17, 2023 2:17 pm

To save the value and ensure it doesn't change when the page is refreshed, you'll need to store it somewhere persistent, such as in the database or session.
Are you working on admin side, or catalog?

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 ;) :) :-*


User avatar
Active Member

Posts

Joined
Mon Aug 27, 2018 11:30 pm
Location - Malaysia

Post by Dev. » Tue Oct 17, 2023 8:00 pm

khnaz35 wrote:
Tue Oct 17, 2023 2:17 pm
To save the value and ensure it doesn't change when the page is refreshed, you'll need to store it somewhere persistent, such as in the database or session.
Are you working on admin side, or catalog?
Thank you for your reply
I'm working on admin side

New member

Posts

Joined
Sun Jul 17, 2022 10:02 pm

Post by khnaz35 » Tue Oct 17, 2023 8:45 pm

Save 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;

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 ;) :) :-*


User avatar
Active Member

Posts

Joined
Mon Aug 27, 2018 11:30 pm
Location - Malaysia

Post by Dev. » Wed Oct 18, 2023 5:55 am

khnaz35 wrote:
Tue Oct 17, 2023 8:45 pm
Save 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;
I'm trying to use your code, but still secret token will be regenerated when the page.

New member

Posts

Joined
Sun Jul 17, 2022 10:02 pm

Post by straightlight » Wed Oct 18, 2023 7:02 am

Dev. wrote:
Tue Oct 17, 2023 8:23 am
I want to generate a random value for the password. I'm using below code:

Code: Select all

        if (!isset($this->config->get['secret_token'])) {
            $random_number = bin2hex(random_bytes(14));
            $data['secret_token'] = $random_number;
        }
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?

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


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Dev. » Wed Oct 18, 2023 7:33 am

straightlight wrote:
Wed Oct 18, 2023 7:02 am
Dev. wrote:
Tue Oct 17, 2023 8:23 am
I want to generate a random value for the password. I'm using below code:

Code: Select all

        if (!isset($this->config->get['secret_token'])) {
            $random_number = bin2hex(random_bytes(14));
            $data['secret_token'] = $random_number;
        }
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?

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'] = '';
}
still get same things, after refresh page will be regenerate new token

New member

Posts

Joined
Sun Jul 17, 2022 10:02 pm

Post by khnaz35 » Wed Oct 18, 2023 12:37 pm

straightlight wrote:
Wed Oct 18, 2023 7:02 am

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'] = '';
}
Why not to do like?

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 ;) :) :-*


User avatar
Active Member

Posts

Joined
Mon Aug 27, 2018 11:30 pm
Location - Malaysia

Post by Dev. » Wed Oct 18, 2023 8:04 pm

khnaz35 wrote:
Wed Oct 18, 2023 12:37 pm
straightlight wrote:
Wed Oct 18, 2023 7:02 am

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'] = '';
}
Why not to do like?

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'] = '';
}
It's working now
Thank you a lot for All

New member

Posts

Joined
Sun Jul 17, 2022 10:02 pm

Post by khnaz35 » Wed Oct 18, 2023 9:19 pm

Glad to hear that. You can add word [SOLVED] in the post title.

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 ;) :) :-*


User avatar
Active Member

Posts

Joined
Mon Aug 27, 2018 11:30 pm
Location - Malaysia

Post by straightlight » Thu Oct 19, 2023 6:23 am

khnaz35 wrote:
Wed Oct 18, 2023 12:37 pm
straightlight wrote:
Wed Oct 18, 2023 7:02 am

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'] = '';
}
Why not to do like?

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'] = '';
}
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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 41 guests