开发者

PHP Session problems

开发者 https://www.devze.com 2023-02-01 02:39 出处:网络
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\'].

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'].

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?

Here is my unset $_SESSION['page'] code.

<?php
ob_start(); 
session_start();

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

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

ob_flush();
?>

I call this function from another script

<?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) && 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) && count($trp) != 0){ $tot = array_sum($trp); }

    if (!empty($tot) && !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 <= 5){ $rate = '5 star'; }

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

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

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

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

        echo $rate;

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

ob_flush();
?>

I solved this problem by placing the session_write_close(); inside the function script at the bottom. before ob_flush().


try executing unset($_SESSION['page']); if the pid is not valid:

if(isset($_GET['pid']) && is_numeric($_GET['pid'])) {
    ....
} else {
    unset($_SESSION['page']);
}


ob_start() and ob_flush() should only be used when you have having problem with http headers.

0

精彩评论

暂无评论...
验证码 换一张
取 消