PHP session_start()问题解疑
我们将会在文章中为大家具体介绍有关
对于PHP的session功能,始终找不到合适的答案,尤其是一些错误,还有一些没有错误的结果,最可怕的就是后者,一直为许多的初学者为难。就连有些老手,有时都被搞得莫名其妙。本文,将这些问题,做一个简单的汇总,以便大家查阅。
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限制登录。。。
答:最简单的方法是
<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.问:我用session_register()注册了session变量,可是当我用header或用javascript的重定向语句,那么在一下页面中,我却访问不到session所注册的变量值。请问如何解决?
问题的程序片段:
<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>
解决的方法:
当你用header函数或window.location这样的功能后,你上一个页面所注册的session变量,就会容易的丢失,关于这个问题的原因,至今仍没有一个详细的回答。
不过有解决的方法。如下所示
header("Location: next.php" ."?" . SID);
在跳转到下一页面的时候,将session的当前id做为一个参数,传到后一个页面。
8.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>
方法是先注册后赋值
9.问题9:我是不是可以用像$HTTP_GET_VARS['**']方式来访问session值呢?
回答:可以,你可以使用如下global数组来访问session,以加强网页的安全性
$HTTP_SESSION_VARS
$_SESSION
例程:
<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>
请参照此例程修改符合您自己的程序。
问题10:session_unregister() 和 session_destroy() 有何区别?
session_unregister()函数主要作用是注消当前的一sion.(译自于php.net)
例程:
<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>
以上,所述是一些新手经常遇到的PHP session_start()问题。或许是详述不清,难免有误所在,请高手指点批评。

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.
