duanke9540 2011-06-04 17:35
浏览 41
已采纳

PHP会话变量没有注册

I've been searching all day and found several similar issues, but none have resolved my problem. As the title states I'm having trouble with session variables not being "saved" - i.e they work only like local variables. What I'm trying to accomplish:

index page with function "ABC" not running by defualt - link to myscript.php to activate session variable of type boolean - myscript.php checks if session variable is set, if not turns it to true. Otherwise turns it to false: index page should now have function "ABC" activated.

So, here is the funny thing. This worked like a charm earlier today, but after a random refresh, now it don't. Thinking this was a session trouble, I added a session ID but found that the session ID is correct / the same on both the index page and myscript.php.

I've also tried to enable error reporting and found that I'm getting the "Undefined index: showAll in C:\xampp\htdocs\kelvin\ext\set_date.php on line 15". Which is weired, seing as i use the issset function to avoid just that.

myscript.php

<?php
session_start();
echo "Session ID: " . session_id(); //Is the same as on the index page where the script is being called.
---------------
//Sets speed mode on or off (simple or extensive listing).
if(!isset($_SESSION['showAll']))
{    
    $_SESSION['showAll'] == TRUE;
}
 else {
    if($_SESSION['showAll'] == TRUE)
    {
        $_SESSION['showAll'] = FALSE;
    }
    else
    {
        $_SESSION['showAll'] = TRUE;
    }
}
header('location:../index.php');
?>

In advance, thanks for any input :)

  • 写回答

1条回答 默认 最新

  • duanbi8529 2011-06-04 17:39
    关注

    Your problem might be here:

    if (!isset($_SESSION['showAll'])) {
      $_SESSION['showAll'] == TRUE;
    }
    

    The double equal sign (==) checks for equality, but does not set anything.

    Try replacing it with the single equal sign (=).


    Also, your logic can be condensed considerably:

    if (!isset($_SESSION['showAll'])) {    
      $_SESSION['showAll'] = true;
    } else {
      $_SESSION['showAll'] = !$_SESSION['showAll'];
    }
    

    You are just swapping the value of $_SESSION['showAll'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题