会话变量只在php中偶尔改变

时间:2015-11-15 22:46:28

标签: php

我有一个日历,可以从数据库中检索表数据并将事件输入日历。我将其中一列存储到会话变量中以访问另一页面(用户可以单击的事件的更长描述)。我的问题是会话变量在循环的每次迭代中都没有变化。同月的每个事件都链接到同一会话变量,但如果我更改月份,则第一个事件是正确的,其余的都有它的数据。为什么此会话变量每月只更改一次?谢谢你的帮助!我在两个页面都有session_start()

/* print "blank" days until the first of the current week */
    for($x = 0; $x < $running_day; $x++):
        $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
        $days_in_this_week++;
    endfor;

    /* keep going with days.... */
    for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';
            /* add in the day number */
            $calendar.= '<div class="day-number">'.$list_day.'</div>';
            $event_day = $year.'-'.$month.'-'.$list_day;
            if(isset($events[$event_day])) {
                foreach($events[$event_day] as $event) {
                    $_SESSION['desc'] = $event['longDesc']; //this is the line I am referencing
                    $calendar.= '<div class="event">'.$event['title'].'<br>'.$event['time'].'<br>'.$event['shortDesc'].'<br><a href="https://cts.gruv.org/short/cal/description.php">More</a></div>';
                }
            }
            else {
                $calendar.= str_repeat('<p>&nbsp;</p>',2);
            }
        $calendar.= '</div></td>';
        if($running_day == 6):
            $calendar.= '</tr>';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<tr class="calendar-row">';
            endif;
            $running_day = -1;
            $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;
    endfor;

    /* finish the rest of the days in the week */
    if($days_in_this_week < 8):
        for($x = 1; $x <= (8 - $days_in_this_week); $x++):
            $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
        endfor;
    endif;

description.php page

<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

include  ('../dbinfo.php');
session_start();

$db=mysqli_connect($dbhostname,$dbuser,$dbpass,$dbname[0]);

$description =  $_SESSION['desc'];

print($description);

session_unset();

mysqli_close($db);//Close the DB Connection

?>

1 个答案:

答案 0 :(得分:0)

我知道您在这两个页面上都有session_start();,但您必须在任何正文输出发送到客户端之前调用session_start 。您是否尝试将session_start();放在网页顶部的非常处?也许include ('../dbinfo.php');正在吐出错误或空格或其他类型的输出。

编辑:你为什么打电话给session_unset?这会清除$_SESSION变量。这就像分配$_SESSION = array();你确定要这样做吗?当您转到另一页时,您分配给会话的任何内容都将被清除。