0

hi i know how to store post value into session but how can i store session value into post. post into session

   $_SESSION['name'] = $_POST['name'];
   $_SESSION['email'] = $_POST['email'];
   $_SESSION['mno'] = $_POST['mno'];
   $_SESSION['age'] = $_POST['age'];

I have an array stored in session and i want to store it into post.

Can i do this? If yes then how?

I want to store all array value in post from session

Vivek
  • 449
  • 18
  • 36
  • did you put session_start(); at the beggining of the file? – boroboris Apr 19 '17 at 10:09
  • also look at this answer: http://stackoverflow.com/questions/29725387/after-using-a-form-post-how-can-i-store-the-variable-in-session – boroboris Apr 19 '17 at 10:10
  • yes....thats why my value stored in session – Vivek Apr 19 '17 at 10:10
  • @boroboris they doing opposite.... – Vivek Apr 19 '17 at 10:11
  • @Vivek why are you planning to do this? Any specific reason? – Abhineet Verma Apr 19 '17 at 10:22
  • well i want to pass value to another function that have `post` data which comes from another function or tpl file. – Vivek Apr 19 '17 at 10:24
  • @Vivek You will end up with much better code if you decouple your functions to take their input as a parameter, and not directly access `$_POST`. See also [Why are Global Variables Evil?](http://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – IMSoP Apr 19 '17 at 10:58

2 Answers2

2

The opposite should work without any problems.

   $_POST['name'] = $_SESSION['name'];
   $_POST['email'] = $_SESSION['email'];
   $_POST['mno'] = $_SESSION['mno'];
   $_POST['age'] = $_SESSION['age'];

If you want to hold an array you can do it like this:

$datapost = array ( 'name' => $_SESSION['name'], 'email' => $_SESSION['email']);
$_POST['info'] = $datapost;
Simos Fasouliotis
  • 1,383
  • 2
  • 16
  • 35
1

$_POST is not a persistent store where you can put things. The point of $_POST is that it is populated at the start of the request, with the data that was passed to the server from the client (usually, web browser).

You can write into that array, but it won't have any special effect. It's just an array variable that's globally available in all scopes of your code. Normally, you'd just want to create a new array, and assign whatever you want in there:

$data = [];
$data['stored_foo'] = $_SESSION['foo'];
$data['submitted_foo'] = $_POST['foo'];

See also Why are Global Variables Evil?

If you want to send data back to the browser, you can:

  • put it in the output (using echo etc)
  • put it in an HTTP header (using the header() function)
  • put it in a cookie (which will be sent as an HTTP header, crafted for you by the setcookie() function)
Community
  • 1
  • 1
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • thankx for sharing this information. I usually think that we can insert `value` in `post` through `form` or `ajax` – Vivek Apr 19 '17 at 10:23
  • @Vivek Yes, when you submit a form, or send an AJAX request, the browser sends some data to the server. That data is parsed by PHP and put into a new array called `$_POST`. On the next request, there will be different data (or possibly none) so `$_POST` will be created with different data each time. – IMSoP Apr 19 '17 at 10:33
  • my page goed blank when i do this `$_POST['profile'] = $_SESSION['profile'];` – Vivek Apr 19 '17 at 10:43
  • @Vivek See this reference answer: http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851 – IMSoP Apr 19 '17 at 10:55