douzhuan4406 2017-09-19 05:33
浏览 52

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

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条回答 默认 最新

  • dsxsou8465 2017-09-19 07:28
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入