-1

I have a page with a form that needs to be sent via post. My htaccess has redirects in it causing the post data to be lost so I would like to put that data into session variables to be picked up by another page.

The session variable "favcolor" below on the form page is there just as a tester. The output on the second page should show the data from the form but it is not. It's an empty array. It is showing the color variable ok though.

I've cut the code right back here to the simplest few lines to test this out and can't get it to work. Can anyone help point me in the right direction here please?

Form page:

<?php
    session_start();
    $_SESSION['post-data'] = $_POST; 
?>
    <form action="zv.php" method="post">    
    Name:<br>   
    <input type="text" id="inputName" name="inputName"> 
    <br>
    Email:<br>
    <input type="text" id="inputEmail" name="inputEmail"> 
    <br>
    Telephone Number:<br>
    <input type="text" id="inputTel" name="inputTel"> 
    <input type="submit" id="submit" value="Submit">                    
    </form>
<?php   
    $_SESSION["favcolor"] = "green";
?>

Second page (zv.php):

<?php
    session_start();
    print_r($_SESSION['post-data']);
    echo "<br>";
    print_r($_SESSION);
?>

Output from second page (zv.php):

Array ( )
Array ( [post_data] => Array ( ) [post-data] => Array ( ) [favcolor] => green )
chipowski
  • 33
  • 4
  • Is zv.php the second page? – JensV Mar 22 '19 at 12:25
  • 1
    Possible duplicate of [Is it possible to redirect post data?](https://stackoverflow.com/questions/358263/is-it-possible-to-redirect-post-data) – Patrick Q Mar 22 '19 at 12:26
  • 1
    If zv.php is the second page, then it will not work as you expect. Once you click submit, `zv.php` is openened, and in `zv.php` the `$_POST` variable is filled with your form data. In your first page, $_POST will be empty as nothing has been written to it yet. – JensV Mar 22 '19 at 12:27
  • Also if you just load the first page normally, `$_SESSION['post-data']` will be emptied because `$_POST` is empty at this point in time – JensV Mar 22 '19 at 12:27
  • `my htaccess has redirects in it causing the post data to be lost` - Naw, when your page reloads after the form is submitted your emptying the value because you don't check if `$_SERVER['REQUEST_METHOD'] == 'POST'` so you change session value regardless if you actual need to. – ArtisticPhoenix Mar 22 '19 at 12:31
  • @JensV - yes sorry I should have said zv was the second page originally – chipowski Mar 22 '19 at 12:36
  • @PatrickQ I went through that post and it did not help with this thanks – chipowski Mar 22 '19 at 12:36
  • @chipowski Can you update your question with what exactly you tried related to that? Because this roundabout way you're going shouldn't even be necessary. – Patrick Q Mar 22 '19 at 12:37
  • @ArtisticPhoenix ok thanks for the explanation – chipowski Mar 22 '19 at 12:38
  • 3
    You will need to do `$_SESION['post-data'] = $_POST` in zv.php because the data is *posted/submitted to zv.php* and thus is only available there – JensV Mar 22 '19 at 12:49
  • 3
    _“My htaccess has redirects in it causing the post data to be lost so I would like to put that data into session variables to be picked up by another page.”_ - and why can’t you simply send the data to the _correct_ URL to begin with then? – 04FS Mar 22 '19 at 12:59
  • thanks for everyone's help. this was my first post here and I know now that I didn't give enough details to start and that I asked the wrong question. Sorry for not explaining myself properly. I should've explained the htaccess redirect for starters. That was just to remove the url extension. And the rules I had impacted my post request. So I should have asked how to allow the post request. I found the solution to that on a previous post: https://stackoverflow.com/questions/34565563/post-becomes-get-after-htaccess-directives I'll take much more care on any future questions. Thanks again all – chipowski Mar 23 '19 at 17:32

2 Answers2

-1

You need to set this ($_SESSION['post-data'] = $_POST) only if you have post request.

<?php
    session_start();
    if (isset($_POST['Submit'])) {
      $_SESSION['post-data'] = $_POST; 
      header('location: zv.php');
    }
?>
    <form action="<?=$_SEVER['PHP_SELF']?>" method="post">    
    Name:<br>   
    <input type="text" id="inputName" name="inputName"> 
    <br>
    Email:<br>
    <input type="text" id="inputEmail" name="inputEmail"> 
    <br>
    Telephone Number:<br>
    <input type="text" id="inputTel" name="inputTel"> 
    <input type="submit" id="submit" value="Submit">                    
    </form>

Then you can access the Session data in the second page.

-1
<?php
    if (isset($_POST['inputName'])) {
      session_start();
      $_SESSION['post-data'] = $_POST; 
      header('location: zv.php');
    }
?>
    <form method="post" action="<?php echo $_SEVER['PHP_SELF']; ?>" >    
    Name:<br>   
    <input type="text" id="inputName" name="inputName"> 
    <br>
    Email:<br>
    <input type="text" id="inputEmail" name="inputEmail"> 
    <br>
    Telephone Number:<br>
    <input type="text" id="inputTel" name="inputTel"> 
    <input type="submit" id="submit" value="Submit">                    
    </form>
Raza Shekh
  • 24
  • 4