Thinkphpsession cannot obtain the verification code thinkphp session verification code
// Query the verification code
Function checklogin (){
$ Data = $ _ SESSION ['verifycode'];
Echo $ data;
}
// Generate the verification code
Public function verify (){
Import ("@. ORG. Image ");
$ Image = new Image ();
$ Image-> buildImageVerify (6, 5 );
}
The verification code is generated, but the session in the controller cannot obtain the verification code.
Static function showAdvVerify ($ type = 'PNG ', $ width = 180, $ height = 40, $ verifyName = 'verifycode '){
$ Rand = range ('A', 'z ');
Shuffle ($ rand );
$ VerifyCode = array_slice ($ rand, 0, 10 );
$ Letter = implode ("", $ verifyCode );
$ _ SESSION [$ verifyName] = $ verifyCode;
.......
Reply to discussion (solution)
You didn't add session_start ();
1. confirm that the program file does not have a BOM header
2. the methods provided by TP should be used to operate sessions.
3. confirm that your code and work process are consistent with the TP conventions.
TP provides the verification code generation class and form verification. You do not need to write the code by yourself. you only need to enter the parameters.
1. BOM has been exclusive.
2. the $ _ session [''] used to operate the SESSION is assigned and read. This should be correct.
3. the verification code generation method is provided by thinkphp.
Xuzuning's work process also seems a bit problematic.
This is my project structure
The content of index. php is as follows:
Define ('app _ name', 'myapp ');
Define ('app _ path', './Myapp /');
Require 'thinkphp/ThinkPHP. php ';
?>
Http: // localhost: 8080/huhaoweb/index. php/During access. the index. php does not need to be written because the url is rewritten.
In addition, if you assign values to and obtain sessions in a method, you can print them out, but skip to other methods and you will not be able to obtain them. It is like re-entering the server. The session is empty.
19.1 Session support
The system provides complete support for Session management and operations. all operations can be completed through a built-in session function.
Sessions are used to set, obtain, delete, and manage sessions.
Usage
Session ($ name, $ value = '')
Parameters
Name (required): If an array is input, session initialization is performed. If null is input, the current session is cleared. if it is a string, the session value is assigned, obtained, or operated.
Value (optional): The session Value to be set. If null is input, the session is deleted. the default Value is a null string.
Return value
See details (return different values based on specific usage)
The session function is a diverse operation function. you can call different parameters to complete different function operations, including the following functions.
Session initialization settings
If the name parameter of the session method is passed in an array, it indicates that the session initialization is set. for example:
Session (array ('name' => 'session _ id', 'expire '=> 3600 ));
The following session parameters are supported:
Parameter name
Description
Id
Session_id value
Name
Session_name value
Path
Session_save_path value
Prefix
Session localized space prefix
Expire
Session. gc_maxlifetime setting value
Domain
Session. cookie_domain setting value
Use_cookies
Session. use_cookies setting value
Use_trans_sid
Session. use_trans_sid setting value
Type
Session hander type, which can be extended using the hander driver
The Session initialization setting method does not need to be manually called. it is automatically called after the initialization of the App class ends. Generally, the project only needs to configure the SESSION_OPTIONS parameter. the SESSION_OPTIONS parameter is set as an array, the index names supported are the same as the previous session initialization parameters.
By default, the system automatically starts the session after initialization. if you do not want the system to automatically start the session, you can set SESSION_AUTO_START to false. for example:
'Session _ AUTO_START '=> false
After automatic start is disabled, you can start the session by manually calling session_start or session ('[start]') in the public file of the project or controller.
Session assignment
Session assignment is relatively simple and can be used directly:
Session ('name', 'value'); // sets the session
Session value
Session value usage:
$ Value = session ('name ');
Delete session
Session ('name', null); // delete name
To delete all sessions, you can use:
Session (null); // clear the current session
Session judgment
To determine whether a session value has been set, you can use
Session ('? Name ');
Used to determine whether the session value named name has been set
Session Management
The session method supports some simple session management operations. the usage is as follows:
Session ('[operation name]');
Supported Operation names include:
Operation Name
Description
Start
Start session
Pause
Pause session writing
Destroy
Destroy session
Regenerate
Regenerate session id
Example:
Session ('[pause]'); // pause session writing
Session ('[start]'); // start the session
Session ('[destroy]'); // destroy the session
Session ('[regenerate]'); // regenerate the session id
Localization support
If the prefix parameter is input during session initialization or the SESSION_PREFIX parameter is set separately, the local session management support can be enabled. After a localized session is started, all assignment, value, deletion, and judgment operations automatically support the localized session.
After the local session is enabled, the format of the generated session data is changed from
$ _ SESSION ['name'] becomes $ _ SESSION ['prefix'] ['name']
Session hander support
If the type parameter is input during session initialization, the corresponding hander Driver is automatically introduced. The Driver extension directory is located under the Extend/Driver/Session directory (see the extension section for details ).
This may be critical.
Each time a verification code is generated, the md5 string information of the verification code is recorded through the SESSION. Therefore, check whether the verification code is correct, we only need to use the following code in Action:
If ($ _ SESSION ['verify ']! = Md5 ($ _ POST ['verify ']) {
$ This-> error ('verification code error! ');
}
Note that the verify name here depends on the value of the verifyName parameter of your verification code.
Session_start ();
Session_start (); it is still available in php + mysql and is useless in thinkphp. it is automatically enabled by default.
Finally solved the problem. I used php mysql to use this computer .. Self-configured. I am using the wamp integrated development environment. Therefore, the session storage path is under php5, which leads to an error in session reading. So I deleted php5 and restarted the server session to finally pass the value.