jimmynew jimmynew - 20 days ago 6
PHP Question

$POST data in $SESSIONS

Why it's not working?

index.php

<strong>Your name:</strong>
<form action="name.php" method"post">
<input type="text" name="username"/>
<input type="submit" name="Submit" value="Submit!" />
</form>

<?php
session_start();
$_SESSION['post-data'] = $_POST;
?>


name.php

<strong>Your name is:</strong>
<strong><?php
session_start();

echo $_SESSION['post-data'];
?></strong>


name.php print me "Your name is: Array" How i can fix this?

Answer

Firstly, session_start(); should always be placed at the top of your document before any HTML has been loaded.

index.php

//No need to start session here as no $_SESSION variables needed. 

<strong>Your name:</strong>
<form action="name.php" method="post">
<input type="text" name="username"/>
<input type="submit" name="Submit" value="Submit!" />
</form>

Secondly, assign your $_POST data after it's been posted to name.php and then assign it to a $_SESSION

name.php

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

<strong>Your name is:</strong>
<strong><?php echo $_SESSION['username'];?></strong>

If you want see whats inside an array for debug purposes then you can use var_dump().

Example of var_dump()

var_dump($_POST);

or

var_dump($_SESSION);