0

Possible Duplicate:
“Warning: Headers already sent” in PHP

Why does this not work? I expect to see whatever is input into the text box in first.php displayed back out in test.php.

    <html>
<body>
<form action="test.php">
Test: <input type="text" name="test" />
<br> <input type="submit" name="submit" />
    <?php
    session_start();
    $_SESSION['test'] = $_POST['test'];
    ?>

</body>
</html>

Here is test.php

    <?php
    session_start();
    $check = $_SESSION['test'];
    echo $check; 
    ?>
Community
  • 1
  • 1
user1613223
  • 75
  • 2
  • 11

5 Answers5

2

session_start() must be called before outputing anything(even not white space) to the browser.

you are starting session after html start session on the very first line of page like

<?php
session_start();
print_r($_SESSION);
if(isset($_POST['submit'])){
    $_SESSION['test'] = $_POST['test'];
}
?>
<html>
    <body>
        <form method="post" action="addimageprocess.php">
            Test: <input type="text" name="test" />
            <br>
            <input type="submit" name="submit" />
        </form>
    </body>
</html>
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

session_start() should be set before any headers are sent (i.e. at the very top of the page)

Prash
  • 1,915
  • 3
  • 20
  • 32
0

The PHP code is executed before the actual POST is done, because PHP is a server-side language. At that point, there is actually no $_POST array.

This code, from your eg.

 <?php
  session_start();
  $_SESSION['test'] = $_POST['test'];
 ?>

The $_POST is posted (or exists), to the test.php page.

Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
0

The PHP in your main script is not doing what you want it to do, in fact is just saving junk and or null in best of the cases. You need to inverse the PHP scripts, put

<?php
session_start();
$_SESSION['test'] = $_POST['test'];
?>

in test.php and

<?php
session_start();
$check = $_SESSION['test'];
echo $check; 
?>

in you main php

KoU_warch
  • 2,160
  • 1
  • 25
  • 46
0

If you are using a webserver, and you do have

<?php 
 session_start();
 $_SESSION['test'] = $_POST['test'];
?>

but it still isn't storing the session then you will need to check the saved_path

  <?php
    phpinfo():
  ?>

check under the session header to see if the session:save_path is set and make sure that it exists on the server. if its a webserver then you will have to contact your host to set up the save path for you.

DWolf
  • 703
  • 1
  • 7
  • 20