2017-05-30 72 views
0

我已經在另一個主題中詢問了這個主題,但是這個主題已經很冷了,而且我已經將這個主題指向了這個問題的核心。如何使用CodeIgniter將會話數據合併到函數中?

我已經在上一頁加載了會話庫,並且沒有在該頁面中工作的函數的問題。

但它是我遇到問題的下一個頁面。我收到錯誤「Unexpected 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。

但沒有任何工作。

是否有人理解會話如何工作以及如何將它們集成到一個函數中?

更新

這是上一頁的控制器編碼,效果很好。

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個控制器編碼和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); 
} 
} 
} 
+0

您可以發佈您創建會話的代碼嗎 – Astound

+0

剛剛完成。 – user225359

回答

0

確保會議庫被加載。你可以這樣做手工的:

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

但是如果你想它裝在任何時候,去你autoload.php文件,並確保會議在autoload['libraries']面積增加。

+0

請參閱最近的編輯。 – user225359

0

你的代碼看起來像越野車。請檢查以下內容。

  1. 在PHP語句由分號(;
  2. $this->session->email結束:返回來自會話陣列email元素的值和它應該分配,象下一行作爲在代碼$email = $this->session->email
  3. 檢查email是否在會話中設置或不print_r($this->session->all_userdata());

祝您好運!

+0

我沒有完全理解你的批評。如果我在末尾放分號(;),我仍然會出錯。我插入了print_r($ this-> session-> all_userdata());在問題行後註釋掉代碼的平衡,但得到這個錯誤 - 語法錯誤,意外的'print_r'(T_STRING) - 你能否按照你認爲應該的方式重寫代碼來回答? – user225359

+0

您能否將完整的控制器和型號代碼放在這裏?這將幫助我們找到實際的錯誤。 –

+0

好的,謝謝,我剛剛插入了該代碼。 – user225359

相關問題