0

Why page can not get the $temp_kt value? I tested $_SESSION['temp_kt'] and $_ENV['temp_kt'], neither worked.

<?php
$temp_kt=0;
if(isset($_POST['db']))
{
    if($_POST['db']=="feedback")
    {
        global $temp_kt;
    $temp_kt=$_POST['temp_kt'];
    }
    exit();
}
if(isset($_GET['q']))
{
    echo "temp_kt=".$temp_kt;
}
?> 
2
  • 2
    How could this ever print anything? The code that sets $temp_kt also exits! Commented Jul 20, 2013 at 10:55
  • 2
    First of all why you need a global keyword there? Commented Jul 20, 2013 at 10:56

1 Answer 1

3

You have exit in if(isset($_POST['db'])) which mean that you can't have both if statements. If you want to save that value in session you should use code like this:

<?php
session_start();
if (isset($_POST['db'])) {
    if ($_POST['db']=="feedback") {
         $_SESSION['temp_kt'] = $_POST['temp_kt'];
    }
    exit();
}
if (isset($_GET['q'])) {
    echo "temp_kt=" . $_SESSION['temp_kt'];
}
?> 

session_start function will enable session for you, you need it at the begining when you set and get session values (it will send cookie to the browser - using header - so you can't have any echo before).

Sign up to request clarification or add additional context in comments.

3 Comments

Point him out that he needs session_start() at the top else he will post another question in 10 mins saying udefined $_SESSION var
@Mr.Alien written info about session_start() function
@jcubic yea, better now ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.