1

I have a script that creates server session variables.

I need to insert 2 of these variables into a simple form (on a php page) using hidden form fields.

<?php
session_start();
?><pre><?php
print_r($_SESSION);
?> 

I created a simple php page with the code above. I can see the session variables easily on the page, as you can see in this part of the code I copied from the page:

[login] => andrew8855
[pass] => $P$BIsPUTQ/e4mJSlaDsRLA48mB20xxIC1
[email] => andrew@mysiteaddress.com
[name_f] => Andrew
[name_l] => 
[street] => 
[street2] => 
[city] =>

In the custom form, I need to get/print the variables from above into the form as so:

<input type="hidden" name="login_email" id="login_email" value="[email]" />
<input type="hidden" name="login_user" id="login_user" value="[login]" />

So when the form is submitted, the variables that exist in the session will be submitted in the hidden form fields.

After searching for hours, I found that I might be able to do this somehow with session_start();....but I'm not clear about how to accomplish this. Thank you for any suggestions.

Rajib Ghosh
  • 640
  • 3
  • 12
Andrew
  • 87
  • 3
  • 9
  • possible duplicate of [PHP Pass variable to next page](http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page) – Machavity Nov 20 '14 at 21:21

3 Answers3

2

Change your html template as such :

<input type="hidden" name="login_email" id="login_email" value="<?php echo $_SESSION['email'];?>" />
<input type="hidden" name="login_user" id="login_user" value="<?php echo $_SESSION['login'];?>" />

Note that if you have short_open_tags enabled you can do :

<?=$_SESSION['email']?>

instead of :

<?php echo $_SESSION['email'];?>
Loïc
  • 11,804
  • 1
  • 31
  • 49
  • I tried both of these in the form, and neither works yet. When testing, I checked the source, and neither of the server variables is showing up as a value in the form. Seems like variables would be inserted into hidden form fields when viewing the source. When I try to submit form, it just stalls. – Andrew Nov 20 '14 at 20:50
  • Would I need to somehow add `session_start();` before the form? – Andrew Nov 20 '14 at 20:54
  • Is your html code in a .php file? (it must be for this to work). – Loïc Nov 21 '14 at 00:56
1

A session is started with the session_start() function.

Session variables are set with the PHP global variable: $_SESSION.

if you are not using any automatic session handling framework ...if you use your own script then you have to must start session before it use.you can try

<?php
// Start the session<br/>
session_start();
?>

which is set beginning of your script.after that you can use session variables.

Change your html template as such :

<input type="hidden" name="login_email" id="login_email" value="<?php echo $_SESSION['email'];?>" />
<input type="hidden" name="login_user" id="login_user" value="<?php echo $_SESSION['login'];?>" />

Since PHP 5.4 the inline echo short tags are always enabled regardless of the short_open_tag (php.ini) setting.

if it's not enable then please Set

short_open_tag=On in php.ini

after that restart your Apache server then you can use

<?=$_SESSION['email']?>

instead of :

<?php echo $_SESSION['email'];?>

you can try http://www.w3schools.com/php/php_sessions.asp for session.i think it's helpful for you.

and How to enable PHP short tags? for enable php short tags.

Community
  • 1
  • 1
Rajib Ghosh
  • 640
  • 3
  • 12
0
<?php
 // I suppose (and I can tell by the code you wrote that it is so) that you already have some values 
 // in the $_SESSION array

 session_start();

 echo "<input type=\"hidden\" name=\"login_email\" id=\"login_email\" value=\"" . $_SESSION['email'] . "\"";

?>

Maybe there's something wrong with the "" and backslashes, but the idea is that you have to put as value $_SESSION['mail'] (or login)

Jackerbil
  • 140
  • 8
  • I'm not real clear about your solution. Would the code you provided be above the form, outside of the form? If so, what would the html of the hidden form be? Thanks. – Andrew Nov 20 '14 at 20:53
  • That is the form, I just wrote it in a PHP line, beacuse the value of the input is a var, and so instead of introducing php in the middle of the line () I wrote the whole line as a PHP line – Jackerbil Nov 20 '14 at 21:54