This article mainly explains the solution of many problems in the case of using the V9 self-contained verification code in V9 and needing to use the session. :)。 If there is a problem or a better solution, I hope to advise.
1. Front-end Call verification code
Pc_base::load_sys_class (' form ', ', 0); {form::checkcode (' code_img ', ' 4 ', ' 14 ', 120, 26)}
2, the management side Verification Code verification
$code = isset ($_post[' code ') && trim ($_post[' code ')? Trim ($_post[' code '): Exit (Format_ajax_out_json ('-1 ', ' Enter Verification Code ')); if ($_session[' code ')! = Strtolower ($code)) {exit ( Format_ajax_out_json ('-1 ', "CAPTCHA Error! "));}
3, combined with the use of the Verification code session
A), PHP native session
Session_Start ();//note $output[' data ' [' area '] is an array, the other is a string, the native SESSION is able to store arrays $_session[' area '] = $output [' Data '] [' Area '];$_session[' yhid '] = $output [' Data '] [' Yhid '];
When you need to use the native session and verify the verification code, the verification code is not available from the session:
if (Isset ($_get[' Dosubmit ')) {session_start (); $code = isset ($_post[' code ') && trim ($_post[' code ')? Trim ($_post[' code '): Exit (Format_ajax_out_json ('-1 ', ' Please enter Verification Code ')); if ($_session[' code ']! = Strtolower ($code)) {exit (Format_ajax_out_json ('-1 ', "CAPTCHA error!) "));} -------------------------Omit Intermediate Code-------------------------------//Add session
Note that $output[' data ' [' area '] is an array, the other is a string, and the native session can store arrays
$_session[' area ') = $output [' Data '] [' area ']; $_session[' yhid ' = $output [' Data '] [' Yhid ']; Exit (Format_ajax_out_json ($output [' StatusCode '], $output));}
After analysis, the session does not get out of the reason, because the V9 code, the memory of the session, called the Session_mysql class.
api/checkcode.php:
$session _storage = ' session_ '. Pc_base::load_config (' System ', ' session_storage ');p C_base::load_sys_class ($session _ storage);
II), V9 Session_mysql
session_mysql.class.php: (V9)
<?php/** * Session MySQL Database storage class * * @copyright (C) 2005-2010 phpcms * @licensehttp://www.phpcms.cn/license/* @lastmodi Fy2010-6-8 */class Session_mysql {var $lifetime = 1800;var $db; var $table;/** * constructor * */Public Function __construct ( {$this->db = Pc_base::load_model (' Session_model '); $this->lifetime = pc_base::load_config (' System ', ' session_ TTL '); Session_set_save_handler (Array (& $this, ' Open '), Array (& $this, ' Close '), Array (& $this, ' read '), Array ( & $this, ' write '), Array (& $this, ' Destroy '), Array (& $this, ' GC ')); Session_Start (); }/** * Session_set_save_handler Open method * @param $save _path * @param $session _name * @return true */Public function op En ($save _path, $session _name) {return true; }/** * Session_set_save_handler Close method * @return BOOL */Public Function close () {return $this->GC ($this-& Gt;lifetime); }/** * Read session_id * Session_set_save_handler Read method * @return string Read session_id */Public function read ($iD) {$r = $this->db->get_one (Array (' SessionID ' + $id), ' data '); return $r? $r [' Data ']: '; }/** * Write session_id value * * @param $id session * @param $data value * @return Mixed query Execution result */Public function write ($i D, $data) {$uid = Isset ($_session[' userid ')]? $_session[' userid ']: 0; $roleid = isset ($_session[' Roleid ')? $_session[' Roleid ': 0; $groupid = isset ($_session[' GroupID ')? $_session[' GroupID ': 0; $m = defined (' Route_m ')? Route_m: '; $c = defined (' Route_c ')? Route_c: '; $a = defined (' Route_a ')? Route_a: "If (strlen ($data) > 255) $data ="; $ip = IP (); $sessiondata = Array (' SessionID ' + = $id, ' userid ' = = $uid, ' IP ' = $ip, ' lastvisit ' =>sys_time, ' roleid ' + $roleid, ' groupid ' + $groupid, ' m ' = = $m, ' c ' + = $c, ' a ' = > $a, ' data ' = $data,); return $this->db->insert ($sessiondata, 1, 1); }/** * Delete the specified session_id * * @param $id session * @return BOOL */Public function destroy ($id) {return $this->db-> ;d elete (Array (' SessionID ' =≫ $id)); }/** * Delete Expired session * * @param $maxlifetime Survival time * @return BOOL */Public Function GC ($maxlifetime) {$expiretime = SY s_time-$maxlifetime; return $this->db->delete ("' Lastvisit ' < $expiretime"); }}?>
How to use the session:
Private Function _session_start () { $session _storage = ' session_ '. Pc_base::load_config (' System ', ' session_ Storage '); $this->todo_session = Pc_base::load_sys_class ($session _storage);} Public Function Dlslogin () { $this->_session_start (); if (Isset ($_get[' Dosubmit ')) {
$code = isset ($_post[' code ') && trim ($_post[' code ')? Trim ($_post[' code '): Exit (Format_ajax_out_json ('-1 ', ' Please enter Verification Code ')); if ($_session[' code ']! = Strtolower ($code)) {exit (Format_ajax_out_json ('-1 ', "CAPTCHA error!) ")); }
-------------------------omit the middle code-------------------------------
Note that $output[' data ' [' area '] is an array, the other is a string, the native session is able to store the arrays, and the Session_mysql method cannot store the array.
$this->todo_session->write (' area ', $output [' Data '] [' area ']; $this->todo_session->write (' Yhid ', $output [' Data '] [' yhid ']); Exit (Format_ajax_out_json ($output [' StatusCode '], $output)); } }
Using this session is a good way to use the verification code + need to write the session data, but the method has a disadvantage. Then: the array cannot be stored in the session. Suppose that you need to deposit the data of the session array, and the size of the array is variable, and the use of V9 session_mysql its essence is to put the session value into the database, read the database when fetching, if the memory of an array in which, read out the data is a " Array "String.
After study, there are two kinds of solutions.
A, change the type, size of the database field (originally varchar type, and can only store 255 characters). and convert the array to a JSON string and store it in Session_mysql way.
b, using Session_files mode from storage.
III), V9 Session_files
It is important to note that the goal we need to implement is to use both the verification code and the session, and to store the array with the session. (Not recommended to re-write a set of verification code)
V9, in addition to using the database to store the session can also be used in the form of files.
session_files.class.php:
<?php class Session_files { function __construct () {$path = Pc_base::load_config (' System ', ' Session_n ') > 0 pc _base::load_config (' System ', ' Session_n '). Pc_base::load_config (' System ', ' Session_savepath ') : Pc_base::load_config (' System ', ' Session_savepath '); ini_ Set (' Session.save_handler ', ' files '); Session_save_path ($path); Session_Start ();} ? >
In order to use the verification code, it is necessary to re-write the checkcode.php in an API and add a method to the form.class.php.
New api/checkcode_files.php
<?phpdefined (' in_phpcms ') or exit (' No permission resources. '); Pc_base::load_sys_class (' session_files '); $checkcode = Pc_base::load_sys_class (' Checkcode ');// The code is then omitted------------------
form.class.php Add method
Verification Code session_file storage mode public static function checkcode_file ($id = ' Checkcode ', $code _len = 4, $font _size = 130 , $height =, $font = ', $font _color = ', $background = ') {return ' ";}
Front-end invocation:
{form::checkcode_file (' code_img ', ' 4 ', ' 14 ', 120, 26)}
Use this method when the session is read by the storage session as used in native PHP, using $_session directly. Using this method, the ability to read the $_session[' code ' value can also perfectly solve the problem of storing arrays in the SESSION.
========================
by Llicat
Reprint need to indicate source: http://www.cnblogs.com/llicat/
Phpcms V9 Two times development _ verification code combined with session development