将函数的结果赋给变量,只有在第一次调用该变量时才调用PHP Codeigniter

将函数的结果赋给变量,只有在第一次调用该变量时才调用PHP Codeigniter,php,function,Php,Function,嗨,我有点受不了了 我有一个生成openssl_random_sudobytes()字符串的函数 public function generate_device_token() { $token = bin2hex(openssl_random_pseudo_bytes(64)); return $token; } 此函数由$token=generate_device_token()调用 代码正在运行,但第二次调用$token(在set_cookie($cookiedata))时,第二

嗨,我有点受不了了

我有一个生成openssl_random_sudobytes()字符串的函数

public function generate_device_token()
{
  $token = bin2hex(openssl_random_pseudo_bytes(64));
  return $token;
}
此函数由$token=generate_device_token()调用

代码正在运行,但第二次调用$token(在set_cookie($cookiedata))时,第二个字符串会附加到第一个字符串

如何使$tokens值在填充后保持静态


谢谢你的帮助。

请注意,我已经解决了这个问题

事实证明,bin2hex(openssl_random_sudo_bytes(64))实际上返回一个128字符的字符串,我的数据库字段的长度设置为64,因此字符串被修剪**Face Palm*

我更改了bin2hex(openssl_random_sudo_bytes(32)),这给了我想要的64字符字符串


:)

请注意,我已经解决了这个问题

事实证明,bin2hex(openssl_random_sudo_bytes(64))实际上返回一个128字符的字符串,我的数据库字段的长度设置为64,因此字符串被修剪**Face Palm*

我更改了bin2hex(openssl_random_sudo_bytes(32)),这给了我想要的64字符字符串

:)

$this->load->model('model_visitorbook_dashboard');
if (isset($_POST['add_this_device'])){
  $token = $this->model_visitorbook_dashboard->generate_device_token();
  $data = array(
    'owner_id' => $this->session->user_id,
    'name' => $this->input->post('devicename'),
    'devicetoken' => $token
  );
  if (!$this->model_visitorbook_dashboard->add_this_device($data))
  {
  $error['error'] = "we're sorry but this device could not be added at this time, please contact support for assitance.";
  $this->load->view('view_visitorbook_error', $error);

  }else{
  $time = intval(10 * 365 * 24 * 60 * 60);
  $cookiedata = array(
    'name' => 'vb_dev_token',
    'value' => $token,
    'expire' => $time,
    'path' => '/'
  );
  var_dump($cookiedata);
  set_cookie($cookiedata);
  redirect('dashboard/devices', 'refresh');
  }