php为什么用$_SESSION会出错?解决思路
php为什么用$_SESSION会出错?
//session1.php:
//session1.php
session_start();
echo "欢迎:第一页
";
$_SESSION['myword']="我是张三";
$_SESSION["myage"]="18";
$_SESSION["mysex"]="男";
echo "Cookie中myword值是:".$_SESSION['myword']."
";
echo "下一页";
?>
//session2.php
//session2.php
session_start();
echo "欢迎:第二页
";
echo "姓名:".$_SESSION['myword']."
";
echo "年龄:".$_SESSION["myage"]."
";
echo "性别:".$_SESSION["mysex"]."
";
echo "下一页";
?>
打开session1.php,输出
------------------------------------
欢迎:第一页
Cookie中myword值是:我是张三
下一页
------------------------------------
点击“下一页”打开session2.php输出
------------------------------------
欢迎:第二页
Notice: Undefined index: myword in R:\Program Files\AppServ\www\session\session2.php on line 5
姓名:
Notice: Undefined index: myage in R:\Program Files\AppServ\www\session\session2.php on line 6
年龄:
Notice: Undefined index: mysex in R:\Program Files\AppServ\www\session\session2.php on line 7
性别:
下一页
------------------------------------
是不是配置的问题? 我的php.ini的session配置如下:
session.save_handler = files
session.save_path = "R:/Program Files/AppServ/php5/session_tmp"
; session.save_path = "N;MODE;/path"
session.save_path = "R:/Program Files/AppServ/php5/session_tmp"
(有三个session.save_path)
session.cookie_domain = Cookies
(其他的就不一一列出了)
------解决方案--------------------
session.cookie_domain =
修改为这样或者注释掉,然后重启服务器
我估计这种情况是没读取到cookie PHPSESSID导致的,你可以用print_r($_COOKIE) 来看看有没有PHPSESSID
------解决方案--------------------
见#1楼,session.cookie_domain不要乱设置,该怎么设置自己搜.你本地测试绑定域名做什么。
------解决方案--------------------
发个session问题总结:
对于PHP的session功能,始终找不到合适的答案,尤其是一些错误,还有一些没有错误的结果,最可怕的就是后者,一直为许多的初学者为难。就连有些老手,有时都被搞得莫名其妙。本文,将这些问题,做一个简单的汇总,以便大家查阅。
1.
错误提示
Warning: Cannot send session cookie - headers already sent
Warning: Cannot send session cache limiter - headers already sent
分析及解决办法
这一类问题,的原因是你在程序中使用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()函数之前,要用session_start()开启session的功能。
4.问题:怎么获得当前session的id值呢?
最简单的方法是:
echo SID;
你会发现的。
5.问题:我的程序,在调用header函数之前没有任何的输出,虽然我include了一个config.php文件,但在config.php文件中也没有任何的输出,为什么session还是会报出与问题1同样的错误呢,是不是因为我在header之前用了session_start()的缘故呢?
答:或许你确实认真的检查了你的php程序,在引用header()之前确实也没有任何的输出,并且在你的include文件中也没有任何的输出!但是你是否用光标键在?>这个PHP代码结束语句后移动检查呢?那么你会发现在?>这个后面,有一个空行或几个空格,你删除了这几个空行或空格,那么问题就解决了。

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

AI Hentai Generator
Generate AI Hentai for free.

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
