简体   繁体   English

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

[英]How to merge session data into a function using CodeIgniter?

I've asked this subject in another topic but that topic has gone cold and I've directed this topic to the core of the problem. 我在另一个主题中询问了该主题,但是该主题变得很冷,并且已经将该主题引向了问题的核心。

I've loaded the session library in the previous page and have no issue with the function working in that page. 我已经在上一页中加载了会话库,并且该页面中的功能没有问题。

But it is the next page where I'm having problems. 但这是我遇到问题的下一页。 I get the error "Unexpected T_VARIABLE". 我收到错误“意外的T_VARIABLE”。

I've read the topic on How to solve syntax errors. 我已经阅读了有关如何解决语法错误的主题。 That topic suggests the line before is usually the problem line, usually by a missing semicolon or bracket. 该主题表明,前一行通常是问题行,通常是缺少分号或括号。

This is the coding; 这是编码;

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');
}
}

I've tried putting a semicolon at the end. 我试过在最后加上分号。 And tried adding - userdata('email'); 并尝试添加-userdata('email'); And tried having a separate function containing the problem line with its own brackets. 并尝试有一个单独的函数,该函数包含带有自己的括号的问题行。 And tried deleting the problem line & the line below. 并尝试删除问题行和下面的行。 When deleted $email cannot be found. 删除后,找不到$ email。

But nothing works. 但是什么都行不通。

Is there somebody who understands how sessions work and how they can be integrated into a function? 是否有人了解会话如何工作以及如何将其集成到功能中?

Update 更新

This is the Controller coding of the previous page, which works good. 这是上一页的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);
}
}

2nd Update - This is the coding for the 2 Controllers and 2 Models 第二次更新-这是2个控制器和2个模型的编码

Email Controller 电子邮件控制器

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');
}
}
}

Email Model 电子邮件模式

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();
}
}

Code Controller 代码控制器

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');
}
}
}

Code Model 代码模型

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);
}
}
}

Make sure the sessions library is loaded. 确保会话库已加载。 You can do this manually by saying: 您可以这样说来手动完成此操作:

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

But if you want it loaded at all times, go to your autoload.php file and make sure sessions is added in the autoload['libraries'] area. 但是,如果您希望一直加载它,请转至autoload.php文件,并确保在autoload['libraries']区域中添加了会话。

Your code looks like the buggy. 您的代码看起来像越野车。 Please check followings. 请检查以下内容。

  1. In PHP statements are ended by semi-colon ( ; ) 在PHP中,语句以分号( ; )结尾
  2. $this->session->email : returns the value of email element from session array and it should be assigned, like the next line as in your code $email = $this->session->email . $this->session->email :从会话数组返回email元素的值,应该对其进行分配,就像代码$email = $this->session->email的下一行一样。
  3. Check whether the email is set in the session or not print_r($this->session->all_userdata()); 检查是否在会话中设置了email print_r($this->session->all_userdata());

Best of luck! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM