简体   繁体   中英

How can I set my $_POST variables to be saved in $_SESSION forever?

I have just planned to make a program where I need to connect USER's database using DBHost, DBUser, DBPassword and DBName to save the future needed values on MySQL server. For that, I have written such codes:

 <?php session_start(); ?> <?php require('functions.php'); iq_add_style( 'DBConForm_Styles' , 'assets/scripts/css/iq_styles' , 'stylesheet' , 'css' ); function IQ_DB_Form(){ ?> <form id="iq-dbconform" method="post" action="<?php $_SERVER['PHP_SELF'] ?>"> <label>Server Host:&nbsp;<input type="text" maxlength="15" placeholder="localhost" name="host-name" /></label> <label>Database User:&nbsp;<input type="text" maxlength="15" placeholder="root" name="user-name" /></label> <label>Password:&nbsp;<input type="password" maxlength="15" placeholder="User Password" name="user-pass" /></label> <label>Name of the Database:&nbsp;<input type="text" maxlength="20" placeholder="developing_house" name="db-name" /></label> <input type="submit" value="Connect" name="dbconbtn" />&nbsp;<input type="reset" value="Reset" name="dbresetbtn" /> </form> <?php } IQ_DB_Form(); // Database connection $dbhost = $_POST['host-name']; $dbuser = $_POST['user-name']; $dbpass = $_POST['user-pass']; $dbname = $_POST['db-name']; $db_details = array( 'host' => $dbhost, 'user' => $dbuser, 'password' => $dbpass, 'dbname' => $dbname, ); $IQcon = mysqli_connect($db_details['host'],$db_details['user'],$db_details['password'],$db_details['dbname']); // Check connection if (mysqli_connect_errno()) { echo "Looks like an Error connecting IQubex Database: " . mysqli_connect_error(); } // Store Values in Session foreach ($_POST as $key => $value) { ${$key} = $value; $_SESSION[$key] = $value; } echo $_SESSION['db-name']; echo $_SESSION['user-pass']; echo $_SESSION['user-name']; // Perform queries if ($IQcon){ mysqli_query($IQcon,"CREATE TABLE `Persons` ( `FirstName` varchar(30) NOT NULL, `LastName` varchar(60) NOT NULL, `Age` int(255) NOT NULL, );"); mysqli_query($IQcon,"CREATE TABLE `MadBoys` ( `FirstName` varchar(30) NOT NULL, `LastName` varchar(60) NOT NULL, `Age` int(255) NOT NULL, );"); mysqli_close($IQcon); } else{ echo "There seems a problem with your server."; } ?>

I have saved the Database Configuration Form Values in session so that when I write echo $_SESSION['db-name'] , the inserted DB name is printed. But when the page is reloaded, going back coming again on the page, or something like that, all the values gets RESET! Thus, I don't want my users to end up connecting database again and again. I want it to be saved forever so that I can work for next program step.

You can check if values are saved in session. If they are do not connect to db else get the values and save them in DB:

See the example below:

session_start();
if(isset($_SESSION['stored-in-db']))
{
    //USE SESSION VALUES
}
else
{
$dbhost = $_POST['host-name'];
$dbuser = $_POST['user-name'];
$dbpass = $_POST['user-pass'];
$dbname = $_POST['db-name'];

$db_details = array(
    'host' => $dbhost,
    'user' => $dbuser,
    'password' => $dbpass,
    'dbname' => $dbname,
);
  $IQcon = mysqli_connect($db_details['host'],$db_details['user'],$db_details['password'],$db_details['dbname']);
  foreach ($_POST as $key => $value)
  {
      $_SESSION[$key] = $value;
  }
  $_SESSION['stored-in-db] = true;
 }

Try to add

session_start();

at top of your pages where you are using sessions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM