PHP has been learning about Session loading for a while. the machine tried to write the framework based on the MVC idea, but the session loading encountered some problems. could you please help me?
This is the SessionDBTool. class. php file code:
Db = MySQLDB: getInstance ($ GLOBALS ['config'] ['database']);} public function sess_close () {return true;} public function sess_read ($ sess_id) {$ SQL = "select sess_data from it_session where sess_id = '$ sess_id'"; return (string) $ this-> db-> fetchColumn ($ SQL );} public function sess_write ($ sess_id, $ sess_data) {$ expire = time (); $ SQL = "insert into it_session values ('$ sess_id', '$ sess_data', $ expire) on dupli Cate key update sess_data = '$ sess_data', expire = $ expire "; return $ this-> db-> query ($ SQL);} public function sess_destroy ($ sess_id) {$ SQL = "delete from it_session where sess_id = '$ sess_id'"; return $ this-> db-> query ($ SQL);} public function sess_gc ($ ttl) {$ last_time = time ()-$ ttl; $ SQL = "delete from it_session where expire <$ last_time "; return $ this-> db-> query ($ SQL) ;}}?>
This is the background controller file code:
InitSession (); // verify whether to log on to $ this-> checkLogin ();}/*** initialize session to database */protected function initSession () {new SessionDBTool ;} /*** verify whether the current user logs in */protected function checkLogin () {// determine whether to log on to if (CONTROLLER = "admin" & (ACTION = "login" | ACTION = "signin ")) {// not required} else {// if (isset ($ _ SESSION ['is _ login']) & $ _ SESSION ['is _ login'] = 'yes') {// load the background template require CURR_VIEW_DIR. 'index.html '; // continue execution} else { $ This-> jump ('index. php? P = back & c = Admin & a = login ', 'log on first', 2 );}}}}
However, when you open the login page, there is no session. when you log on, the system prompts "please log on first". it should be because SessionDBTool has not been initialized. after reading this for a long time, I don't know where to change it, A good person helps solve the problem
Reply to discussion (solution)
@ Xuzuning
// Enable session
$ This-> initSession ();
Protected function initSession (){
New SessionDBTool; // isn't the instance of this SessionDBTool object local?
}
What does it mean? ⊙ Not clear
/**
* Initialize session warehouse receiving
*/
Protected function initSession (){
New SessionDBTool;
}
This code does not make any sense.
The variables (objects) created in the function (method) cannot be accessed outside the function (method ).
You need to store it on the carrier of sustainable access, such
$ This-> pile = new SessionDBTool;
Otherwise, after the initSession is executed, the object will not exist.
The variables (objects) created in the function (method) cannot be accessed outside the function (method ).
You need to store it on the carrier of sustainable access, such
$ This-> pile = new SessionDBTool;
Otherwise, after the initSession is executed, the object will not exist.
Still not working. the system prompts "please log on first"
/**
* Initialize session warehouse receiving
*/
Protected function initSession (){
New SessionDBTool;
}
This code does not make any sense.
What should I do?
@ Lijpwsw @ xuzuning
Originally, my indexController did not inherit the background controller class.
By The Way
/*** Initialize the session database */protected function initSession () {new SessionDBTool ;}
There is no problem with this writing.
Thank you!