How to connect on dynamic database in php using online cPanel database

711 views Asked by At

In my web app I have 2 different connections the main connection and the dynamic database.I used the main connection to create new database with tables and I used dynamic to switch database when user is already login.I already did this in localhost it works perfectly.But my problem is when I change it to Online server cPanel. I can't login and connect to dynamic database. Here is my code below:

create_database.php:

$curl_path = "";

function createDatabase($dbName, $dbUser, $dbPass,$userID,$userFullname,$userEmail,$userClinicname,$userAddress,$userCliniclogo,$username,$userpass) {

    $cpanel_user = "name";
    $cpanel_password = "pass";
    $cpanel_host = "mocha3020.mochahost.com";
    $cpanel_skin = "paper_lantern";

    $db_name = $dbName;
    $db_username = $dbUser;
    $db_userpass = $dbPass;

    $user_id = $userID;
    $user_full_name = $userFullname;
    $user_email = $userEmail;
    $user_clinic_name = $userClinicname;
    $user_address = $userAddress;
    $user_clinic_logo = $userCliniclogo;
    $user_name = $username;
    $user_pass =$userpass;

    $result = execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/addb.html?db=$db_name");
    // create user
    $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/adduser.html?user={$db_username}&pass={$db_userpass}");
    // assign user to database

    $result.= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/addusertodb.html?user={$cpanel_user}_{$db_username}&db={$cpanel_user}_{$db_name}&privileges=ALL");

    include ('dynamicConfigs.php'); 
    $DB_con = dynamicDatabase($db_username,$db_userpass,$db_name);
    include('create_table.php'); 
}

function execCommand($command) {
    global $curl_path;

    if (!empty($curl_path)) {
        return exec("$curl_path '$command'");
    }

    else {
        return file_get_contents($command);
    }
}

In class.user.php:

public function login($uname,$upass) {

    try {
        $stmt = $this->db->prepare("SELECT user_id, user_name, user_pass, user_clinic_name, db_username, db_pass FROM users WHERE user_name=:uname LIMIT 1");
        $stmt->execute(array(':uname'=>$uname));
        $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
        if($stmt->rowCount() > 0) {  

            if(password_verify($upass, $userRow['user_pass'])) {

                if($userRow['user_Status'] != "Y"){
                    header('Location: index.php?inactive');     
                    exit;
                }

                $_SESSION['user_session'] = $userRow['user_id'];
                $_SESSION['username'] = $uname;

                $registrationDate = date_format('Y-m-d');
                $currentDate = new DateTime($registration_Date);
                $editedcurrent = date_format($currentDate, 'Y-m-d');
                $expDate = $userRow['expiration_Date'];

                if ($expDate < $editedcurrent) {
                    header('Location:index.php?expiredUser');
                    exit;
                }

                $dbUsername = $userRow['user_name'];                        
                $dbPassword = $userRow['user_pass'];
                $dbName = $userRow['user_clinic_name'];
                //update database   
                include('dynamicConfigs.php');                       
                $DB_con = dynamicDatabase($uname,$upass,$dbName); 

                /*$random = password_hash(rand(1,20), PASSWORD_DEFAULT);
                setcookie('logged_in' , $random, strtotime('+1 years'));
                $stmt =  $this->db->prepare('UPDATE users  SET logged_in = 1, Cookie_Value = :cookie WHERE user_id = :user_id ');
                $stmt->execute(array(':cookie' => $random, ':user_id'=> $userRow['user_id']));
                $editedClinic = strtolower(str_replace(' ', '_', $userRow['user_clinic_name']));  
                */
                header('Location: Home.php');                                          
             } 

             else {
                header('Location: index.php?error'); 
                exit;
             }
        }

        else {
            header('Location: index.php?error'); 
            exit;
        }
    }

    catch(PDOException $e) {
        echo $e->getMessage();
    }
}

connection.php

$DB_host = "xxx.xx.xx.x2.";
$DB_user = "prophics_dentAid";
$DB_pass = "password";
$DB_name = "prophics_dentiaide";    

try {   
    $DB_con = new PDO("mysql:host=$DB_host;dbname=$DB_name",$DB_user,$DB_pass);
    $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}

catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}

include_once 'class.user.php';
$user = new USER($DB_con);

dynamicConfig.php

session_start();
function dynamicDatabase($user_Db,$user_Pass,$db_name) {

    $DB_host = "xxx.xx.xx.x2";
    $DB_user = 'prophics_'.$user_Db;
    $DB_pass =  $user_Pass;
    $DB_name = 'prophics_'.$db_name;

    try {
            $DB_con = new PDO("mysql:host=$DB_host;dbname=$DB_name",$DB_user,$DB_pass);
            $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $DB_con;

    }  

    catch(PDOException $e) {    
        echo "Connection failed: " . $e->getMessage();
    }
}

index.php

require 'connection.php';
if (isset($_COOKIE['logged_in'])) {
    $cookieValue = $_COOKIE['logged_in'];
    $sth = $DB_con->prepare("SELECT user_id FROM users WHERE Cookie_Value = 
:cookieValue " );
    $sth->execute(array(':cookieValue' => $cookieValue));
    $userRow=$sth->fetch(PDO::FETCH_ASSOC);
    if ($userRow != "") {
        $user -> checkCookies($userRow['user_id']);
        exit;
    } 

    else {
        DisplayIndex();
    }
} 

else {
    DisplayIndex();
}

function DisplayIndex() {
    require 'connection.php';
    if(isset($_POST['btn-login'])) {
        $uname = $_POST['txt_uname'];
        $upass = $_POST['txt_password'];
        $user -> login($uname, $upass);   
    }

Thank's and advance.

1

There are 1 answers

0
Andreea Craciun On

You have the hosting from a specific provider or what do you mean by online? I had a similar problem because of their own settings which did not allow remote access without them mentioning the specific IP (in my case I had a Rasperry Pi as second part of application to push data)... and the device had to have fixed IP in this case, otherwise it did not work.