Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems with PHP session_start ()

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the PHP session_start () problems". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the PHP session_start () problems"?

1.

Error prompt

Warning: Cannot send session cookie-headers already sent

Warning: Cannot send session cache limiter-headers already sent

Analysis and solution

The reason for this kind of problem is that when you use PHP session_start () in the program, you already have the actual html content output. Maybe you say, I'm not, I'm just an echo or print message. Sorry, the output from your echo or print statement is the actual html content output. The solution to this problem is to change your session_start () to the first line of the program.

two。

Error prompt

Warning: open (F:/689phpsessiondatasess_66a39376b873f4daecf239891edc98b5, O_RDWR) failed

Analysis and solution

Such an error statement usually occurs because the session.save_path item in your php.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

Then create a temp directory under the c: directory, and you can

3.

Error prompt

Warning: Trying to destroy uninitialized session in

Analysis and solution

This kind of prompt is usually caused by you directly calling the session_destroy () function. Many friends think that the session_destroy () function can be run independently, but it is not. The solution is to turn on session with PHP session_start () before you call the session_destroy () function.

4. Question: how do I get the id value of the current session?

The easiest way is:

Echo SID

You'll 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 does session still report the same error as question 1? is it because I used PHP session_start () before header?

A: maybe you did check your php program carefully, and there was no output before referencing header (), and there was no output in your include file! But do you use the cursor key to move the check after the PHP code ends the statement? Then you will find that after this, there is a blank line or spaces. If you delete these blank lines or spaces, the problem will be solved.

Note: this issue will occur in PHP4.1.2, a later version, not tested.

6. Q: after using session as the login main page, how do other pages use session to restrict login.

A: the easiest way is

Session_start ()

If (! session_registered)

& apos;login')

││ $login! = true) {

Echo "you didn't log in"

Exit

}

7. Q: I registered the session variable with session_register (), but when I used header or javascript's redirect statement, I couldn't access the value of the variable registered by session on the following page. May I ask how to solve it?

Program snippet of the question:

Session_start ()

$ok = & apos;love you&apos

Session_register & apos;ok')

Header ("location: next.php")

? >

Next.php

Session_start ()

Echo $ok

? >

The solution:

When you use a function such as header or _ window.location, the session variable you registered on the previous page is easily lost. There is still no detailed answer to the reason for this question.

But there is a solution. As shown below

Header ("Location: next.php". SID)

When you jump to the next page, pass the current id of session as a parameter to the next page.

How to pass an array by 8.session

Session_register

& apos;data')

$data = array (1, 2, 3, 4)

The method is to register first and then assign a value

9. Question 9: can I access session values in ways like $HTTP_GET_VARS [& apos;**']?

Answer: yes, you can use the following global array to access session to enhance the security of the web page

$HTTP_SESSION_VARS

$_ SESSION

Routine:

Session_start ()

$username = & apos;stangly.

Wrong&apos

Session_register & apos

Username')

Echo $HTTP_SESSION_VARS

[& apos;username']

Echo & apos

& apos

Echo $_ SESSION

[& apos;username']

? >

Please refer to this routine to modify your own program.

What is the difference between question 10:session_unregister () and session_destroy ()?

The main function of the session_unregister () function is to cancel the current sion. (translated from php.net)

Routine:

If (isset ($_ Cookie [session _ name ()])) {

Session_start ()

Session_destroy ()

Unset ($_ Cookie [session _ name ()])

}

Thank you for your reading, the above is the content of "what is the PHP session_start () problem". After the study of this article, I believe you have a deeper understanding of the PHP session_start () problem, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report