2

I have Created one php file on root of drupal7 and access it using URL.I have one form in that file which has dropdown values and submit button. Now in settings.php i am getting those value in POST. Based on That post value i want to select DB for drupal. But it Throws error because session is not getting maintain. This is my code in settings.php

session_start();
$_SESSION['data'] = $_POST['year'];
if(!empty($_SESSION['data'])) {
  $databases = array (
    'default' =>
       array (
        'default' =>
          array (
            'database' => 'mydbname'.$_SESSION['data'],
            'username' => 'root',
            'password' => '',
            'host' => 'localhost',
            'port' => '',
            'driver' => 'mysql',
            'prefix' => '',
          ),
      ),
  );
}
else {
  header("Location: welcome.php");
}
0

You can't start session_start() the second time, since Drupal is already started it.

So either remove it, or wrap it into the condition, e.g.:

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

Parsing input user variables is not a good idea, so it's better if filter it out, e.g.

$_SESSION['data'] = filter_input(INPUT_POST, 'year', FILTER_SANITIZE_STRING);

or better use db_escape_table().

0

Session records are stored in the database. Hence, it is simply impossible to select the database connection based on values stored in a session.

0

Using session it is not possible so I did it using Cookie.

    if (!isset($_COOKIE['yr'])|| $_COOKIE['yr'] == '') {
          if (isset($_POST['year'])) {
            setcookie("yr", $_POST['year']);
            $_COOKIE['yr'] = $_POST['year'];
          } else {
            header('Location: welcome.php');exit;
          }
        }
        elseif(isset($_POST['year'])) {
            setcookie("yr", $_POST['year']);
            $_COOKIE['yr'] = $_POST['year'];
        }
        $databases = array (
          'default' =>
          array (
            'default' =>
            array (
              'database' => 'db' . $_COOKIE['yr'],
              'username' => 'root',
              'password' => '',
              'host' => 'localhost',
              'port' => '',
              'driver' => 'mysql',
              'prefix' => '',
            ),
          ),
        )
;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.