We will introduce to you in detail about
For the session function of PHP, I have never been able to find a suitable answer, especially some errors, and Some error-free results, the most terrifying of which is the latter, have always been a embarrassment for many beginners. Even some veterans are confused sometimes. This article makes a simple summary of these issues for everyone’s reference.
1.
Error message
Warning: Cannot send session cookie - headers already sent
Warning: Cannot send session cache limiter - headers already sent
Analysis and solution
The reason for this type of problem is that when you use PHP session_start() in the program, actual html content has been output before. Maybe you say, I don't have it, I just echo or print a message. Sorry, the output generated by your echo or print statement is the actual html content output. The way to solve this kind of problem is to move your session_start() to the first line of the program.
2.
Error message
Warning: open(F:/689phpsessiondatasess_66a39376b873f4daecf239891edc98b5, O_RDWR) failed
Analysis and solution
Such an error statement is usually caused by your php. The session.save_path item in ini is not set properly. The solution is to set the session.save_path and session.cookie_path settings to
session_save_path = c: emp
session.cookie_path = c: emp
and then Create a temp directory under the c: directory, you can
3.
Error prompt
Warning: Trying to destroy uninitialized session in
Analysis and solution
A prompt like this appears , usually it is caused by you directly calling the session_destroy() function. Many friends think that the session_destroy() function can run independently, but this is not the case. The solution is to use PHP session_start() to enable the session function before you call the session_destroy() function.
4. Question: How to get the id value of the current session?
The easiest way is:
echo SID;
You will find out.
5. Question: My program does not have any output before calling the header function. Although I include a config.php file, there is no output in the config.php file. Why is the session still there? The same error as in question 1 will be reported. Is it because I used PHP session_start() before the header?
Answer: Maybe you really checked your php program carefully and quoted There is indeed no output before header(), and there is no output in your include file! But do you use the cursor keys to move the check after the end statement of the PHP code?>? Then you will find that after ?>, there is a blank line or a few spaces. If you delete these blank lines or spaces, the problem will be solved.
Note: This problem will occur in PHP4.1.2 and higher versions, and has not been tested.
6. Question: After using session to log in to the main page, how can I use session to restrict login on other pages? . .
Answer: The simplest method is
<ol class="dp-xml"> <li class="alt"><span><span>session_start(); </span></span></li> <li><span>if(!session_registered<br>('login') <br>││ $login != true) { </span></li> <li class="alt"><span>echo "你没有登陆"; </span></li> <li><span>exit; </span></li> <li class="alt"><span>} </span></li> </ol>
7. Question: I used session_register() to register the session variable, but when I use header or javascript redirection statement, then on the following page , but I cannot access the variable values registered by the session. How to solve it?
Program fragment of the problem:
<ol class="dp-xml"> <li class="alt"><span><span>session_start(); </span></span></li> <li> <span>$</span><span class="attribute">ok</span><span> = 'love you'; </span> </li> <li class="alt"><span>session_register('ok'); </span></li> <li><span>header("location : next.php"); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li><span>next.php </span></li> <li class="alt"><span>session_start(); </span></li> <li><span>echo $ok; </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
Solution:
When you use the header function or a function like window.location, what you registered on the previous page The session variable will be easily lost. There is still no detailed answer to the reason for this problem.
But there is a solution. As shown below
header("Location: next.php" ."?" . SID);
When jumping to the next page, use the current id of the session as a parameter and pass it to the next page. page.
8. How to pass an array in session
<ol class="dp-xml"> <li class="alt"><span><span>session_register<br>('data'); </span></span></li> <li> <span>$</span><span class="attribute">data</span><span>=</span><span class="attribute-value">array</span><span>(1,2,3,4); </span> </li> </ol>
The method is to register first and then assign the value
9. Question 9: Can I use something like $HTTP_GET_VARS['* *'] method to access the session value?
Answer: Yes, you can use the following global array to access the session to enhance the security of the web page
$HTTP_SESSION_VARS
$_SESSION
Routine:
<ol class="dp-xml"> <li class="alt"><span><span>session_start(); </span></span></li> <li> <span>$</span><span class="attribute">username</span><span> = 'stangly.<br>wrong'; </span> </li> <li class="alt"><span>session_register('<br>username'); </span></li> <li><span>echo $HTTP_SESSION_VARS<br>['username']; </span></li> <li class="alt"><span>echo ' </span></li> <li><span>'; </span></li> <li class="alt"><span>echo $_SESSION<br>['username']; </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
Please refer to this routine to modify the program to suit your own needs.
Question 10: What is the difference between session_unregister() and session_destroy()? The main function of the
session_unregister() function is to unregister the current session. (Translated from php.net)
Routine:
<ol class="dp-xml"> <li class="alt"><span><span>if(isset($_COOKIE[session_name()])) { </span></span></li> <li><span>session_start(); </span></li> <li class="alt"><span>session_destroy(); </span></li> <li><span>unset($_COOKIE[session_name()]); </span></li> <li class="alt"><span>} </span></li> </ol>
The above are for novices Frequently encountered PHP session_start() problems. Maybe the details are not clear, and there are bound to be mistakes. Please give some guidance and criticism from experts.