如何使用CodeIgniter将会话数据合并到一个函数中?

时间:2017-05-30 08:10:45

标签: php codeigniter session

我在另一个主题中问过这个话题,但这个话题已经冷淡了,我已经将这个话题引导到了问题的核心。

我已经在上一页加载了会话库,并且对该页面中的功能没有任何问题。

但这是我遇到问题的下一页。我收到了错误"意外的T_VARIABLE"。

我已阅读有关如何解决语法错误的主题。该主题表明之前的行通常是问题行,通常是缺少分号或括号。

这是编码;

public function index()
{
$this->load->model('Code_model', 'code_model');
$this->session->email //This is the problem line
$email = $this->session->email
$code = $this->input->post('code');
if ($this->code_model->find_code($email, $code))
{
$this->load->view('username');
}
else
{
$this->load->view('codeincorrect');
}
}

我试过在最后加一个分号。并尝试添加 - userdata(' email'); 并尝试使用包含问题行的单独函数及其自己的括号。并尝试删除问题行&下面这一行。删除时无法找到$ email。

但没有任何作用。

是否有人了解会话的工作方式以及如何将其集成到函数中?

更新

这是上一页的Controller编码,效果很好。

function __construct()
{
parent::__construct();
$this->load->library('session');
}

public function index()
{
$this->load->model('Email_model', 'email_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'required|min_length[10]|max_length[40]|valid_email|is_unique[tbl_members.email_address]', array(
'required' => 'You have not entered an %s address.', 'min_length' => 'Your %s address must be a minimum of 10 characters.',
'max_length' => 'Your %s address must be a maximum of 40 characters.', 'valid_email' => 'You must enter a valid %s address.',
'is_unique' => 'That %s address already exists in our Database.'));
if ($this->form_validation->run() == FALSE) // The email address does not exist.
{
$this->load->view('email');
}
else
{
$email = $this->input->post('email');
$random_string = chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . chr(rand(65,90));
$code = $random_string;
$this->email_model->insert_email($email, $code);
}
}

第二次更新 - 这是2个控制器和2个模型的编码

电子邮件控制器

class Email extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function index()
{
$this->load->model('Email_model', 'email_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'required|min_length[10]|max_length[40]|valid_email|is_unique[tbl_members.email_address]', array(
'required' => 'You have not entered an %s address.', 'min_length' => 'Your %s address must be a minimum of 10 characters.',
'max_length' => 'Your %s address must be a maximum of 40 characters.', 'valid_email' => 'You must enter a valid %s address.',
'is_unique' => 'That %s address already exists in our Database.'));
if ($this->form_validation->run() == FALSE) // The email address does not exist.
{
$this->load->view('email');
}
else
{
$email = $this->input->post('email');
$random_string = chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . chr(rand(65,90));
$code = $random_string;
$this->email_model->insert_email($email, $code);
$this->load->library('email'); // Not sure if this works - testing from localhost
$this->email->from('<?php echo WEBSITE_NAME; ?>', '<?php echo WEBSITE_NAME; ?>'); // Not sure if this works - testing from localhost
$this->email->to('$email'); // Not sure if this works - testing from localhost
$this->email->subject('Code.'); // Not sure if this works - testing from localhost
$this->email->message('Select & Copy this code, then return to the website. - ','$code'); // Not sure if this works - testing from localhost
$this->email->send(); // Not sure if this works - testing from localhost
$this->load->view('code');
}
}
}

电子邮件模型

class Email_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
public function insert_email($email, $code)
{
$data = array(
'email_address' => $email,
'pass_word' => $code
);
$this->db->insert('tbl_members', $data);
return $this->db->insert_id();
}
}

代码控制器

class Code extends CI_Controller
{
public function index()
{
$this->load->model('Code_model', 'code_model');
$this->session->email // Problem line - syntax error, unexpected '$email' (T_VARIABLE)
$email = $this->session->email
$code = $this->input->post('code');
if ($this->code_model->find_code($email, $code))
{
$this->load->view('username');
}
else
{
$this->load->view('codeincorrect');
}
}
}

代码模型

class Code_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
public function find_code($code,$email)
{
$this->db->select('user_id');
$this->db->where('email_address', $email);
$this->db->where('pass_word', $code);
$code = $this->db->get('tbl_members');
if ($code->result())
{
return $this->db->delete('pass_word', $code);
}
}
}

2 个答案:

答案 0 :(得分:0)

确保已加载会话库。您可以通过以下方式手动执行此操作:

$this->load->library('session');

但如果您希望随时加载,请转到autoload.php文件,确保会话已添加到autoload['libraries']区域。

答案 1 :(得分:0)

您的代码看起来像小车。请检查以下内容。

  1. 在PHP语句中以分号(;
  2. 结束
  3. $this->session->email:从会话数组中返回email元素的值,应该分配它,就像代码$email = $this->session->email中的下一行一样。
  4. 检查会话中是否设置了email print_r($this->session->all_userdata());
  5. 祝你好运!