Home  >  Article  >  Backend Development  >  PHP session_start() problem solving (detailed introduction)_PHP tutorial

PHP session_start() problem solving (detailed introduction)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:37728browse

本文,将这些问题,做一个简单的汇总,以便大家查阅。
1.
错误提示
Warning: Cannot send session cookie - headers already sent
Warning: Cannot send session cache limiter - headers already sent
分析及解决办法
这一类问题,的原因是你在程序中使用PHP session_start()时,之前已经有实际的html内容输出了。或许你说,我没有啊,我只不过是echo或print一条消息了。很抱歉,你的 echo或print语句所产生的输出,就是实际的html内容输出。解决此类问题的办法是,将你的session_start()调到程序的第一行。

2.
错误提示
Warning: open(F:/689phpsessiondatasess_66a39376b873f4daecf239891edc98b5, O_RDWR) failed
分析及解决方法
出现这样的错误语句一般是因为你的php.ini中关于session.save_path一项没有设置好,解决的方法是将 session.save_path和session.cookie_path 设置置为
session_save_path = c: emp
session.cookie_path = c: emp
然后在c:目录下建立一个temp目录,即可

3.
错误提示
Warning: Trying to destroy uninitialized session in
分析及解决方法
出类这样的提示,一般情况都是你直接调session_destroy()函数造成的。很多的朋友认为session_destroy()函数可以独立的 运行,其实不然。解决的方法是在你调session_destroy()函数之前,要用PHP session_start()开启session的功能。

4.问题:怎么获得当前session的id值呢?
最简单的方法是:
echo SID;
你会发现的。

5.问题:我的程序,在调用header函数之前没有任何的输出,虽然我include了一个config.php文件,但在config.php 文件中也没有任何的输出,为什么session还是会报出与问题1同样的错误呢,是不是因为我在header之前用了PHP session_start()的缘故呢?

答:或许你确实认真的检查了你的php程序,在引用header()之前确实也没有任何的输出,并且在你的include文件中也没有任何的输出! 但是你是否用光标键在?>这个PHP代码结束语句后移动检查呢?那么你会发现在?>这个后面,有一个空行或几个空格,你删除了这几个空行或空 格,那么问题就解决了。
注:此问题,会出PHP4.1.2中,更高版本,没有测试过。

6.问:用session做登录主页面后,其它页面怎么用session限制登录。。。
答:最简单的方法是
    session_start();  
    if(!session_registered
    ('login')
    ││ $login != true) {  
    echo "你没有登陆";  
    exit;  
    }

7.问:我用session_register()注册了session变量,可是当我用header或用javascript的重定向语句,那么 在一下页面中,我却访问不到session所注册的变量值。请问如何解决?
问题的程序片段:
    session_start();  
    $ ok  = 'love you';  
    session_register('ok');  
    header("location : next.php");  
    ?> 
    next.php  
    session_start();  
    echo $ok;  
    ?> 
解决的方法:
当你用header函数或window.location这样的功能后,你上一个页面所注册的session变量,就会容易的丢失,关于这个问题的原因, 至今仍没有一个详细的回答。
不过有解决的方法。如下所示
header("Location: next.php" ."?" . SID);
在跳转到下一页面的时候,将session的当前id做为一个参数,传到后一个页面。

8.session如何传数组
    session_register
    ('data');  
    $ data = array (1,2,3,4);
方法是先注册后赋值

9. Question 9: Can I access the session value using a method like $HTTP_GET_VARS['**']?
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:
session_start();
$ username = 'stangly.
wrong';
session_register('
username');
echo $HTTP_SESSION_VARS
['username'];
echo '
';
echo $_SESSION
['username'];
?>
Please refer to this routine to modify your own program.

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:
if(isset($_COOKIE[session_name()])) {
session_start();
session_destroy();
unset($_COOKIE[session_name()]);
}
The above is what some novices often encounter in PHP session_start( )question. 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/328063.htmlTechArticleThis 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...
Statement:
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