PHP Session problems
<p>Okay my code should grab the page id using $_GET['pid'] and check and see if it is equal to the $_SESSION['page']. If it is keep the $_SESSION['page'] if not destroy the $_SESSION['page']. </p>

<p>But for some reason when I call the function from another page using an include() the $_SESSION['page'] has a different value from another page when reloaded? What is causing this problem and how can I fix it?</p>

<p>Here is my unset $_SESSION['page'] code.</p>

<pre><code>&lt;?php
ob_start(); 
session_start();

if(isset($_GET['pid']) &amp;&amp; is_numeric($_GET['pid'])) {
    if(isset($_SESSION['page']) &amp;&amp; $_SESSION['page'] !== $_GET['pid']){
        unset($_SESSION['page']);
    }

    $page = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_GET['pid'])));
    $_SESSION['page'] = $page;
}

ob_flush();
?&gt;
</code></pre>

<p>I call this function from another script</p>

<pre><code>&lt;?php
ob_start(); 
session_start();
function getRatingText(){
    $dbc = mysqli_connect("localhost", "db", "pass", "members");
    $sql1 = "SELECT COUNT(ab_g.u_ab_id) FROM ab_g INNER JOIN u ON ab_g.user_id = u.user_id WHERE u_ab_id = '" . $_SESSION['page'] . "' AND u.active IS NULL AND u.deletion = 0";
    $result = mysqli_query($dbc,$sql1);

    if (!mysqli_query($dbc, $sql1)) {
            trigger_error(mysqli_error($dbc));
            return;
    } else {
        while($row = mysqli_fetch_array($result)) {
            $tr[] = $row[0];
        }
    }

    if(isset($tr) &amp;&amp; count($tr) != 0){ $rat = array_sum($tr); }

    $sql2 = "SELECT g.rate_points FROM g INNER JOIN ab_g ON g.id = ab_g.rate_id INNER JOIN u ON ab_g.user_id = u.user_id WHERE ab_g.u_ab_id = '" . $_SESSION['page'] . "' AND u.active IS NULL AND u.deletion = 0";
    $result = mysqli_query($dbc, $sql2);

    if(!mysqli_query($dbc, $sql2)) {
        trigger_error(mysqli_error($dbc));
        return;
    } else {
        while($row = mysqli_fetch_array($result)) {
            $trp[] = $row[0];
        }
    }

    if(isset($trp) &amp;&amp; count($trp) != 0){ $tot = array_sum($trp); }

    if (!empty($tot) &amp;&amp; !empty($rat)){
        $avg = round($tot / $rat, 1);
        $percent = round(($avg / 5) * 100, 1);
        $votes = $rat;
        if($rat == 1){
            echo $avg . "/5  (" . $votes . " rating casted)";
        } else {
            echo $avg . "/5  (" . $votes . " casted)";
        }

        if($avg &lt;= 5){ $rate = '5 star'; }

        if($avg &lt;= 4){ $rate = '4 star'; }

        if($avg &lt;= 3){ $rate = '3 star'; }

        if($avg &lt;= 2){ $rate = '2 star'; }

        if($avg &lt;= 1){ $rate = '1 star'; } 

        echo $rate;

    } else {
        echo 'not rating';
    }   
  return TRUE;
}

ob_flush();
?&gt;
</code></pre>

<p><br />
<br /></p>

<p><b>I solved this problem by placing the</b> <code>session_write_close();</code> <b>inside the function script at the bottom. before ob_flush().</b></p>
