PHP session_start() problem solving_PHP tutorial
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
