Home > Backend Development > PHP Tutorial > PHP session_start() problem solving_PHP tutorial

PHP session_start() problem solving_PHP tutorial

WBOY
Release: 2016-07-15 13:33:09
Original
778 people have browsed it

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>(&apos;login&apos;) <br>││ $login != true) {   </span></li>
<li class="alt"><span>echo "你没有登陆";   </span></li>
<li><span>exit;   </span></li>
<li class="alt"><span>} </span></li>
</ol>
Copy after login

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> = &apos;love you&apos;;   </span>
</li>
<li class="alt"><span>session_register(&apos;ok&apos;);   </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>
Copy after login

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>(&apos;data&apos;);   </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>
Copy after login

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> = &apos;stangly.<br>wrong&apos;;   </span>
</li>
<li class="alt"><span>session_register(&apos;<br>username&apos;);  </span></li>
<li><span>echo $HTTP_SESSION_VARS<br>[&apos;username&apos;];   </span></li>
<li class="alt"><span>echo &apos;  </span></li>
<li><span>&apos;;   </span></li>
<li class="alt"><span>echo $_SESSION<br>[&apos;username&apos;];   </span></li>
<li>
<span class="tag">?></span><span>   </span>
</li>
</ol>
Copy after login

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>
Copy after login

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446083.htmlTechArticleWe will give you a detailed introduction to the session function of PHP in the article, but we have never been able to find a suitable answer. , especially some errors, and some results without errors, the most terrible...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template