user6580560
user6580560

Reputation:

$_SESSION wrong values transmitted

I've made a

<?php session_start(); ?>

on the top of my two pages. Seacond I've made some $_SESSION variables and gave them the values but I get wrong values back. On my original site are the values absolutely right I don't know...

On my original page i set:

$_SESSION["gesamtumsatz_kcal"] = $gesamtumsatz_kcal = round(($grund + $arbeit + $sonst), 0); 

But when I set this like up there I get on my seacond page the value 655 but it should be 2573.

Here is the strange thing I don't understand. When I change my code from the original to this here

$_SESSION["gesamtumsatz_kcal"] = round(($grund + $arbeit + $sonst), 0); 

it works fine?!

I've just removed $gesamtumsatz_kcal.

On my seacond page I use

 echo $_SESSION['gesamtumsatz_kcal'] . '</br>';

to get my value back.

Upvotes: 0

Views: 34

Answers (1)

Poiz
Poiz

Reputation: 7617

You don't pay for using an extra line of Code; do you? Why not try it the other way round like so:

<?php
    session_start();

    $gesamtumsatz_kcal              = round(($grund + $arbeit + $sonst), 0);
    $_SESSION["gesamtumsatz_kcal"]  = $gesamtumsatz_kcal;

Just for the sake of extra thoroughness (should any of the variables not contain valid Float, or Integer); try checking for the values first before the expression like so:

<?php
    session_start();

    // JUST FOR DEBUGGING... TRY TO SEE THE CONTENTS OF THE VARIABLES: 
    // $grund, $arbeit AND $sonst:
    var_dump("\$grund="  . $grund);
    var_dump("\$arbeit=" . $arbeit);
    var_dump("\$sonst="  . $sonst);

    $gesamtumsatz_kcal              = null;

    if($grund && $arbeit && $sonst){
        // ALL THESES A'INT NECESSARY BUT JUST BEING A LITTLE THOROUGH
        // EXPLICITLY CAST THE VALUES OF THESE VARIABLES TO FLOAT... 
        $grund                      = floatval($grund);
        $arbeit                     = floatval($arbeit);
        $sonst                      = floatval($sonst);

        $gesamtumsatz_kcal          = round(($grund + $arbeit + $sonst), 0);            
    }

    $_SESSION["gesamtumsatz_kcal"]  = $gesamtumsatz_kcal;

Upvotes: 2

Related Questions