Press "Enter" to skip to content

PHP Session的一个警告


PHP Warning:  Unknown: Your script possibly relies on a session side-effect
which existed until PHP 4.2.3. Please be advised that the session extension does
not consider global variables as a source of data, unless register_globals is enabled.
You can disable this functionality and this warning by setting session.bug_compat_42
or session.bug_compat_warn to off, respectively. in Unknown on

关于这个问题, 网上有多种解决办法, 但都是不知所以然的答案, 那么真正的原因是什么呢, 怎么解决呢?

请首先记住这一点. 在PHP4.2开始, register_globals默认设置为了OFF.

在4.2.3以后, 为了兼容以前的模式, PHP引入了bug_compat_42, 当启用这个选项以后(默认启用), PHP将容许自动将SESSION中的变量做为全局变量使用. 只不过如果bug_compat_warn选项开启的情况下, 会报告这个特性的被使用.


$name = 'laruence';
$_SESSION['name'] = null;

上面的代码, 在bug_compat_42开启, register_globals关闭的情况下, 俩次刷新页面的输出, 分别为:

	array(0) {}

	array(1) { ["a"]=> string(8) "laruence" }

为什么第二次不是NULL呢, 因为在bug_compat_42开启的情况下, PHP会认为变量a是$_SESSION[‘a’]的一个引用, 在session_close的时候, 会把变量a的值回写.

而在这个过程中, 如果bug_compat_warn开启, 则会抛出文章开头的警告.

So, that it is~

那么, 它具体给出警告的条件是什么呢? 知道了这些条件, 我们就可以避免这个警告了,

在PHPSRC/ext/session/session.c中, 有我们想要的一切答案:

static void php_session_save_current_state(TSRMLS_D) /* {{{ */
	int ret = FAILURE;

		if (PS(bug_compat) && !PG(register_globals)) {
			HashTable *ht = Z_ARRVAL_P(PS(http_session_vars));
			HashPosition pos;
			zval **val;
			int do_warn = 0;

			zend_hash_internal_pointer_reset_ex(ht, &pos);

			while (zend_hash_get_current_data_ex(ht
						, (void **) &val, &pos) != FAILURE) {
				if (Z_TYPE_PP(val) == IS_NULL) { //变量为null
					if (migrate_global(ht, &pos TSRMLS_CC)) {//变量回写
						do_warn = 1;
				zend_hash_move_forward_ex(ht, &pos);

			if (do_warn && PS(bug_compat_warn)) {
				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Your script possibly
 relies on a session side-effect which existed until PHP 4.2.3 ..........");

可见, 如果不开启bug_compat_42(现在很少用到这个特性, 开启的话有的时候反而会造成迷惑), 或者不开始bug_compat_warn, 或者在register_globals开启的情况下, 都不会看到这个警告.

另外, 如果开启bug_compat_42, 还可能遇到如下的NOTICE..

PHP Notice:  Unknown: The session bug compatibility code will not try to
locate the global variable $324324 due to its numeric nature in Unknown on line 0

这是当你在$_SESSION中使用数字索引的时候, 可能会引发的警告.


  1. MarvinBiggie
    MarvinBiggie 2018-07-08

    Hello webmaster, i’ve been reading your posts for some time and
    I really like coming back here. I can see that you probably
    don’t make money on your page. I know one cool method of earning money, I think you will
    like it. Search google for: dracko’s tricks

  2. Porfirio
    Porfirio 2014-09-25

    Right now MoneyVista consumers is completely impartial.
    According to Snowden, a Sikorsky Aerospace Services announced the indictments, suggested widespread crime.
    So everything is done because a bad job.

    C I am seeking. A mortgage is particularly practical for mutation and polymorphism
    detection, and that is the very basic legal principle regarding the dehumidifiers that you get what they are working on your home in respective areas.

    my site … webpage – Porfirio,

  3. Ali
    Ali 2014-09-08

    Here is my web site :: web site (Ali)

  4. Brook
    Brook 2014-06-22

    Do not give a deposit and not try to get paid, this may
    well then it will cost you that he can deliver the mail
    fraud in a civil case by Assistant U. You will want to check these
    credentials of suggested contractor either through someone in the house and provide you with an air conditioning!

    You might be some time to spend hard earned money.

    My web-site: homepage, Brook,

  5. TottyAndBaty
    TottyAndBaty 2009-08-19

    感谢站长的回答,这是我的那个源文件,代码在php5下测试没有问题,在 php4 下报错,第二个问题也是出在这里

    class cart
    function cart()
    function printcart()
    echo “”;
    echo “”;
    function clearcart()
    function iscart($cartid)
    foreach($_SESSION[“cart”] as $key=>$value)
    return $iscart;
    function addcart($cartid,$count,$price,$danwei,$name)
    $c= $_SESSION[“cart”][$iscart[“key”]][“count”];
    $price= $_SESSION[“cart”][$iscart[“key”]][“price”];
    function addMore($cartid,$count=1)
    $c= $_SESSION[“cart”][$iscart[“key”]][“count”];
    $price= $_SESSION[“cart”][$iscart[“key”]][“price”];
    function moveout($cartid)
    function delcart($cartid,$count)
    $c= $_SESSION[“cart”][$iscart[“key”]][“count”];
    $price= $_SESSION[“cart”][$iscart[“key”]][“price”];

  6. 雪候鸟
    雪候鸟 2009-08-14

    @TottyAndBaty 第一个问题, 你是否在PHP5中使用了auto_load机制?
    Session在反序列化你的对象的时候, 如果找不到对象的类定义, 那么这个对象的类型就会是_PHP_Incomplete_Class Object,

    解决方法是在,session_start之前, 载入你的类定义.

    至于,第二个问题, 我没遇到过, 你能给个重现代码么?

  7. TottyAndBaty
    TottyAndBaty 2009-08-14


    服务器的register_globals=off,我的购物车本身的类名叫cart,而我的购物车保存在$_SESSION[“cart”]中,在php5 下运行可以,但是php4下却报错了,系统提示:__PHP_Incomplete_Class Object,当然我把类名改一下或者改一下$_SESSION问题就可以解决,不知道为什么。

    这个问题解决了,但是后来又出现问题,我使用了一个 $_SESSION[“count”],这个也报错了,好像把这里的count理解为函数了,博主是否帮忙解释一下?谢谢

  8. panjinww
    panjinww 2009-07-14


  9. sky
    sky 2009-07-14


  10. keyvalue
    keyvalue 2009-07-14


    • 雪候鸟
      雪候鸟 2009-07-14

      你的文采真好, 夸的我开心的~ 嘿嘿

  11. xi2008wang
    xi2008wang 2009-07-13


Leave a Reply

Your email address will not be published. Required fields are marked *