MySQL连接适用于Localhost,但不适用于Web服务器

时间:2014-05-09 17:30:52

标签: php mysql

美好的一天每个人......

我有一个令我困惑的问题,我似乎无法找到解决问题的方法。即使我的托管服务中的技术支持也无法解决它。

我创建了一个小脚本来完成一项简单的任务。我要求员工登录以执行任何所述任务。

我已经在开发服务器上测试了应用程序,并且登录脚本运行正常,但是当我将它放在Web服务器上时,连接永远不会建立。

我在dbcon.php文件中使用相同的用户名和passowrd来使用phpMyAdmin登录并且它可以工作,我运行查询并且它们也可以工作。

以下是文件:

1:dbcon.php

<?php
$connect = "mysql:host=localhost;dbname=mdchaara_draiwil_dms;charset=utf8";
$db_user = "dbusername";
$db_pass = "dbpassword";

$db = new PDO($connect,$db_user,$db_pass);
?>

2:login.php:

    <?php
session_start();
require "../../_dbcon/_dbcon.php";
//Timezone settings:
$timezone = "Asia/Kuwait";
                if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);

// check the username has only alpha numeric characters
if (ctype_alnum($_POST['username']) != true)
{
    //if there is no match
    $message = "Username must be alpha numeric";
}
//check the password has only alpha numeric characters ***/
if (ctype_alnum($_POST['password']) != true)
{
    //if there is no match ***/
    $message = "Password must be alpha numeric";
}
else
{
    // if we are here the data is valid and we can insert it into database
    $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
    $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);


    //SQL Injection Precaution:/
    $username   =   stripslashes($username);
    $password   =   stripslashes($password);


   try
    {


        //Select Statement:
        $stmt = $db->query("SELECT * 
                            FROM dms_gt_users 
                            WHERE username = '$username' AND password = '$password'");

        $result = $stmt->rowCount();
    }
    catch(PDOException $ex) {
        echo "An Error occured!"; //user friendly message
        some_logging_function($ex->getMessage());
    }

    // If result matched $username and $password, there will be one row
    if($result==1){
        // check if the account is active:

        $stmt = $db->query("SELECT id_status 
                            FROM dms_gt_users 
                            WHERE username = '$username' AND password = '$password'");
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    $id_status=  $row['id_status'];
                    } 

        $stmt = $db->query("SELECT employee_id 
                            FROM dms_gt_users 
                            WHERE username = '$username' AND password = '$password'");
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    $employee_id=  $row['employee_id'];
                    } 

        //Check if account is active:
        if($id_status == "A"){

            // Create Session ID:
            $session_id = "";
            $_SESSION['sid'] = "";

            $session_id = mt_rand(100000, 999999);
            $sid_update = $db->query("UPDATE    dms_gt_users 
                                SET `session_id`='$session_id'
                                WHERE username='$username' and password ='$password'");

            $_SESSION['sid']    =   $session_id;

            //Get last login details:
            $current_login = date("Y-m-d H:i:s");

            $stmt = $db->query('SELECT `last_log_in`
                                FROM dms_gt_users 
                                WHERE `employee_id` = '.$employee_id);

                while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                    $last_log_in = $row['last_log_in'];
                }   

                $_SESSION['last_log_in'] = $last_log_in;

            //get IP address:
            $ip = getenv('REMOTE_ADDR');

            //Add login details to Activity Log:
            $stmt = $db->query("INSERT INTO dms_activity_log
                     (`employee_id`, `activity_date_time`, `activity`, `ip_address`) 
                     VALUES ('$employee_id', '$current_login', 'Logged in', '$ip')");

            //Add login details to users table:
            $stmt = $db->query("UPDATE  dms_gt_users 
                                SET `last_log_in`='$current_login'
                            WHERE username='$username' and password ='$password'");

            //update session login
            $_SESSION['login']= 1;

            //save employee id to session
            $_SESSION['employee_id'] = $employee_id;

            // redirect to portal home:
                header ("Location:../../../home.php");
            }

        //Account is not Active:
        else{
            header ("Location:../../../index.php");
        }
    }

    //Username or password are incorrect
    else {
            header ("Location:../../../index.php");
    }
}

?>

我做错了什么?如果我的代码没问题,我应该告诉托管技术支持人员寻找什么?

谢谢!

修改

@ noc2spam:我已按照你的建议更新了连接字符串,我没有记录任何错误。我var_dump $ db,我得到object(PDO)#1 (0)

3 个答案:

答案 0 :(得分:1)

如果Roger Ng的答案没有解决,那么您可能会有防火墙阻止您的连接。检查你的mysql服务器端口......通常是3306。

答案 1 :(得分:1)

如果不调查服务器本身,很难说出为什么会发生这种情况。我建议您启用“异常”模式,以便查看问题所在。例如:

try {
   $connect = "mysql:host=localhost;dbname=mdchaara_draiwil_dms;charset=utf8";
   $db_user = "dbusername";
   $db_pass = "dbpassword";

  $db = new PDO($connect,$db_user,$db_pass);
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
}
catch (PDOException $e) {
    echo 'PDO Exception: '.$e->getMessage();
    die();
}

现在排除故障要容易得多。检查您是否收到任何错误,并在可能的情况下使用消息更新原始问题。之后我会编辑这个答案。

答案 2 :(得分:1)

检查数据库的网址。通常,在共享/专用托管环境中,DB服务器和App Server位于不同的计算机上。此外,许多服务提供商不在端口3306上提供mysql集群服务。因此,请从主机CPanel或技术支持团队获取正确的数据库URL和端口。

此外,将App服务器的IP地址添加到Remote MySQL Cpanel界面中允许的IP地址列表中。