Your question is a bit confusing but from my experience working with PHP here's how we $_POST
data and make use of $_SESSION
the easy way
EDIT : in your question we don't know whether you know how to use session_start()
or not. We also don't know how the files and folders in your program are set up to give proper answer. But usually (well me) I create a config.php
file which holds database information then I include that file in the header (header.php
) because usually header.php
is included everywhere in the program. I created file examples assuming you are using mysqli
.
config.php
if (session_status() == PHP_SESSION_NONE) {
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_DATABASE', 'my_database_name');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
}
// This tells the web browser that your content is encoded using UTF-8
// and that it should submit content back to you using UTF-8
header('Content-Type: text/html; charset=utf-8');
// This initializes a session. Sessions are used to store information about
// a visitor from one web page visit to the next. Unlike a cookie, the information is
// stored on the server-side and cannot be modified by the visitor. However,
// note that in most cases sessions do still use cookies and require the visitor
// to have cookies enabled. For more information about sessions:
// http://us.php.net/manual/en/book.session.php
session_start();
// Note that it is a good practice to NOT end your PHP files with a closing PHP tag.
// This prevents trailing newlines on the file from being included in your output,
// which can cause problems with redirecting users.
header.php (include this file everywhere you need or else include config.php)
header('Content-Type: text/html; charset=iso-8859-1');
ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);//handle php errors
ob_start();//prevent header from being sent twice
require_once('config.php');
form
require_once('header.php');
<form action="s2q2.php" method="POST">
<p>Enter your name:</p>
<input type="text" name="name"><br>
<button type="submit" name="s2q1"><span>Next </span></button>
</form>
s2q2.php
if (isset($_POST['s2q1'])) { // Retrieve form
$name = $_POST['name']; // retrieve data name
$_SESSION['name'] = $name; // pass data in session
}
Using $_SESSION
if (isset($_SESSION['name'])) {
echo $_SESSION['name'];
} else {
//do something else;
}