0

I am trying to dynamically set database connection credentials based on who logs into a web page. I'm pretty sure it's not working because of the $connectdb variable not being defined. Can someone please check out my code and try to get it working? Thanks!

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$connectdb="";
class Main extends CI_Controller {
    function __construct() {
        parent::__construct();

        echo $connectdb;
        $this->load->database($connectdb);
        $this->load->helper('url');
        $this->load->library('grocery_CRUD');
    }

    public function index() {
        if ($_POST["username"] == "root") {
            global $connectdb="default";
        }

        if ($_POST["username"] == "user1") {
            global $connectdb="user1";
        }

        if ($_POST["username"] == "user2") {
            global $connectdb="user2";
        }

        $connect = @mysql_connect("localhost", $_POST["username"],      $_POST["password"]);//won't display the warning if any.
        if (!$connect) {
            echo 'Server error. Please try again sometime. CON';
        } else {
            print("<a href=\"http://v-admindb/ci/index.php /main/employees?username=".$_POST["username"]."\">Employees</a>");
            echo "<br>";
            print("<a href=\"http://v-admindb/ci/index.php/main/visitors?username=".$_POST["username"]."\">Visitors</a>");
        }//Just an example to ensure that we get into the function
    // LOAD LIBRARIES
    }

    public function employees() {
        $this->grocery_crud->set_table('employees');
        $output = $this->grocery_crud->render();
        $this->_example_output($output);
    }

    public function visitors() {
        $this->grocery_crud->set_table('visitors');
        $output = $this->grocery_crud->render();
        $this->_example_output($output);
    }

    function _example_output($output = null) {
        $this->load->view('our_template.php',$output);
    }
}

2 Answers 2

1

A quick read of THE MANUAL will show you it's pretty easy to have multiple database connections. You define the connection parameters in your database.php config file then call the database with the group name.

if($user == 'someguise'){
    $this->load->database('guiseDB');
}

HTH

Sign up to request clarification or add additional context in comments.

2 Comments

That won't work unfortunately. What I need to do is set a variable called $connectdb based on the username from the login screen (using $_POST["username"] ) then use $this->load->database($connectdb) I tried listing multiple if statements inside the constructor but it did not work.
I see now in your code you might be using grocery crud. I went ahead and took the 8 seconds to search google for you: grocerycrud.com/forums/topic/…
1

For something as important as this i would strongly suggest running the form input through CI form validation first. You really should be validating and doing things like limit the number of characters, make sure its letters only, trim whitespace and XSS cleaning - all before you do anything else. (this helps your user as well)

then to get the value from the form - do something like this

$username = $this->input->post( 'username', TRUE ) ;

and work with the one variable $username. the TRUE XSS cleans the value, and then instead of repeating

$_POST["username"] == 

over and over and over, you are just checking $username. also makes the code easier to read. if you need $username in different methods just use:

$this->username = $this->input->post( 'username', TRUE ) ;

and then $this->username will work in any method in the class.

finally consider having a table of users or config list -- and then use a different value to call your database. in other words maybe they log in with the user name: "root" but then its a different name like global $connectdb = "rootadmin"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.