简体   繁体   English

如何使用在线cPanel数据库连接php中的动态数据库

[英]How to connect on dynamic database in php using online cPanel database

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. 在我的Web应用程序中,我有2个不同的连接:主连接和动态数据库。我使用主连接来创建带有表的新数据库,并在用户已经登录时使用dynamic来切换数据库。 。但是我的问题是当我将其更改为在线服务器cPanel时。 I can't login and connect to dynamic database. 我无法登录并连接到动态数据库。 Here is my code below: 这是我的代码如下:

create_database.php: 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: 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 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 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 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. 谢谢和前进。

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. 我有一个类似的问题,因为他们自己的设置不允许远程访问而没有他们提到特定的IP(在我的情况下,我将Rasperry Pi作为应用程序的第二部分来推送数据)...并且设备必须已修复在这种情况下为IP,否则它将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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