0

I'm trying to put a script together that will do some math for the user.
That works fine however when i try to put it in a session and try to show the value to the user it will only return 0 if its set to 0.

Does anybody know where i did wrong?

<?php
session_start();
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ){            
$class1 = filter_var($_POST['class1'], FILTER_SANITIZE_STRING);
$class2 = filter_var($_POST['class2'], FILTER_SANITIZE_STRING);
$class3 = filter_var($_POST['class3'], FILTER_SANITIZE_STRING);
$class4 = filter_var($_POST['class4'], FILTER_SANITIZE_STRING);
$class5 = filter_var($_POST['class5'], FILTER_SANITIZE_STRING);
$class1C = $class1 * 35;
$class2C = $class2 * 5;
$class3C = $class3 * 7.5;
$class4C = $class4 * 26;
$class5C = $class5 * 2.5;
$totaal1 = $class1C + $class2C + $class3C + $class4C + $class5C;

$res = array($class1C, $class2C, $class3C, $class4C, $class5C, $totaal1);
foreach($res as $name => $var) {
$_SESSION[$name] = $var;
}


$result = array("error" => false, "html" => null);
$result["error"] = false;
$result["html"] = "<h3>Session information: var_dump($_SESSION[$class1C]) ($_SESSION[$class2C]) ($_SESSION[$totaal1])</h3>";
} else {
$result["error"] = true;
$result["html"] = "<h3>Error</h3>";
}
echo json_encode($result);
exit;
?>
Justin
  • 49
  • 1
  • 1
  • 8
  • can you put session_start(); at the top of the page ? –  Nov 27 '14 at 11:23
  • Did that and doesn't work.. and it also is done already on a other page above this one so that should not be the problem i guess? – Justin Nov 27 '14 at 11:25
  • Yes that works. You always need to do this when you are using session. Also where did you declare `$_SESSION[$class1C]`? You only dump it? – S.Pols Nov 27 '14 at 11:27
  • Can you please print session array? i.e `print_r($_SESSION);`. Let me know what happened? –  Nov 27 '14 at 11:27
  • @s.pols, look @ the foreach loop. – Justin Nov 27 '14 at 11:30
  • Jup, that makes sense. Gues read over it. – S.Pols Nov 27 '14 at 11:31
  • Session information: print_r(Array); Does it do.. i guess that my $res = array(...); doesn't set the session value correctly? – Justin Nov 27 '14 at 11:35
  • Why sanitize string and not FILTER_SANITIZE_NUMBER_INT ? – Cups Nov 27 '14 at 12:00
  • I just copied the code knowing the it would work, didn't check what sanitize string would do. but it works. – Justin Nov 27 '14 at 15:25

1 Answers1

0

You cannot call var_dump inside the double quoted string, and var_dump does not return anything: it only display things.

Even if you could, $class1C is not a valid index for $_SESSION

Keeping the same logic as your code, you may change your line to the following:

$result["html"] = "<h3>Session information:";
ob_start();
var_dump($_SESSION[0]); // contains $class1C
echo $_SESSION[1]; // contains $class2C
echo $_SESSION[5]; // contains $totaal1
$result["html"] .= ob_get_clean();
$result["html"] .= "</h3>";

EDIT: If you want to use the indexes 'class2C', 'totaal1' etc.. you need to init $res as follow:

$res = array(
'class1C' => $class1C,
'class2C' => $class2C, 
'class3C' => $class3C,
'class4C' => $class4C, 
'class5C' => $class5C,
'totaal1' => $totaal1
);

Then, your loop to set $_SESSION will set correct indexes, and you will be able to use $_SESSION['class1C'] to get proper values.

Asenar
  • 6,732
  • 3
  • 36
  • 49
  • I use Json to return the $result["html"] to a DIV so i guess this one wont work ? it won't return the echo's ? – Justin Nov 27 '14 at 11:40
  • why not ? just try and you will see – Asenar Nov 27 '14 at 11:42
  • Funny, i thought everything needed to be between a $result["html"] = "blablahere"; – Justin Nov 27 '14 at 11:47
  • it has to be, but using `ob_start()` suppress the common output to store it in a buffer, until `ob_get_clean` or a similar function is called (see related link in the documentation). Using buffer is the only way to prevent `var_dump` from display things. but you can also use `print_r($var, true)` (see also the doc for that) doc: http://php.net/manual/en/function.ob-get-clean.php / http://php.net/manual/en/function.print-r.php – Asenar Nov 27 '14 at 12:19
  • Do you also know howto put a redirect between the $results. I tried it with meta refresh header location and js but none get processed at the right way. – Justin Nov 27 '14 at 15:30
  • well ... don't send them ! That's a totally other question and depends of your code (you shall not output anything before `session_start()`). Ask an other question if you can't fix it yourself. – Asenar Dec 04 '14 at 08:42
  • yeeh sure, look at the script and if i want to post a redirect to a user with a $result value then it just doesn't work. really good explaining.. smartass – Justin Dec 04 '14 at 08:44
  • After all the time I take to help you, that's not really nice to insult me (in particular if you want more help). I told you to ask an other question because that's a totally different subject (but, as I don't like to be insulted, I hope someone else will answer). – Asenar Dec 04 '14 at 09:02
  • You're just giving an answer that is totally bullcrap, Dont send them then, jezus man if i ask another question it will just be voted down cause nobody thinks it are good questions cause the most people here are coders with a focking masters degree. – Justin Dec 04 '14 at 09:07
  • Try to think a little: How can I answer to "what if the header is already sent" without any context (the code in your question is absolutely not related). If you think my answer is "totally bullcrap", what do you think about your question ? Did you read the link http://php.net/header (which will probably solve your problem) ? about your 2nd question "header already sent", maybe that link (found in a few second) can help you too http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php , but instead of searching by yourself you are aggressive and imperative. – Asenar Dec 04 '14 at 09:23
  • I'm a little tired to answer nicely to an angry people. I personally have no degree related to php, but I like to help (that's why I give answers on S-O when I know). But I don't like to be insulted. And I will not answer anymore for that subject here. If you still don't understand, I really cannot help you. – Asenar Dec 04 '14 at 09:24