You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP跨服务器调用DOCUMENT_ROOT:连接MDB文件及双向传输方案问询

Hey there! Let's tackle your two questions one by one since they're focused on different aspects of cross-server communication in PHP.

获取服务器B的$_SERVER[DOCUMENT_ROOT]

You can't directly access $_SERVER['DOCUMENT_ROOT'] from Server A on Server B—since that superglobal is specific to the server running the PHP script. Instead, you'll need to expose that value from Server B via a simple API endpoint, then fetch it from Server A. Here's how:

  • Step 1: Create an endpoint on Server B
    Make a new PHP file (e.g., get_doc_root.php) in a web-accessible directory on Server B with this code:

    <?php
    // Add basic security to prevent unauthorized access
    $allowed_ips = ['192.168.1.100']; // Replace with Server A's public IP
    if (!in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)) {
        http_response_code(403);
        die("Access denied");
    }
    
    // Output the DOCUMENT_ROOT value
    echo $_SERVER['DOCUMENT_ROOT'];
    ?>
    

    The IP whitelist is crucial here—you don't want random people fetching your server's internal file path details.

  • Step 2: Fetch the value from Server A
    On Server A, use file_get_contents() or cURL to call the endpoint:

    <?php
    $server_b_endpoint = 'https://your-server-b-domain.com/get_doc_root.php';
    $server_b_doc_root = file_get_contents($server_b_endpoint);
    
    if ($server_b_doc_root === false) {
        die("Failed to fetch Server B's document root");
    }
    
    // Now build your MDB file path (note: you still can't read the file directly over HTTP—see the next section)
    $mdb_file_path = $server_b_doc_root . "/sapfiles/web_supp/PEB_MDB/dbPEB.mdb";
    ?>
    
实现服务器A与服务器B之间的双向数据传输

Just having the file path won't let Server A read the MDB file directly (unless you have a shared filesystem set up). Here are the most practical ways to handle bidirectional data transfer between the two servers:

1. HTTP/RESTful APIs (Most Common for Simple Use Cases)

Build endpoints on both servers to send and receive data using standard HTTP methods (POST, GET, PUT, DELETE). This is straightforward and works for most small to medium-scale needs.

Example: Fetch MDB data from Server B to Server A

On Server B, create fetch_mdb_data.php:

<?php
// Add security layer (API key + IP whitelist)
$valid_api_key = 'your-super-secret-api-key';
$allowed_ips = ['192.168.1.100']; // Server A's IP

if (!isset($_GET['key']) || $_GET['key'] !== $valid_api_key || !in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)) {
    http_response_code(403);
    die("Unauthorized access");
}

$mdb_file = $_SERVER['DOCUMENT_ROOT'] . "/sapfiles/web_supp/PEB_MDB/dbPEB.mdb";
if (!file_exists($mdb_file)) {
    http_response_code(404);
    die("Database file not found");
}

// Connect to the MDB file (use PDO_ODBC—you'll need the Access driver installed)
try {
    $conn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdb_file;");
    $stmt = $conn->query("SELECT * FROM your_target_table");
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Return data as JSON for easy parsing on Server A
    header('Content-Type: application/json');
    echo json_encode($data);
} catch (PDOException $e) {
    http_response_code(500);
    die("Database error: " . $e->getMessage());
}
?>

On Server A, call this endpoint to get data:

<?php
$server_b_api = 'https://your-server-b-domain.com/fetch_mdb_data.php?key=your-super-secret-api-key';
$response = file_get_contents($server_b_api);
$data = json_decode($response, true);

if ($data === null) {
    die("Failed to retrieve data from Server B");
}

// Use the data as needed
print_r($data);
?>

To send data from Server A to B, create a POST endpoint on Server B that accepts JSON/form data, then use cURL from Server A to send the payload.

2. Shared Filesystem (For Same-Network Servers)

If your servers are on the same private network, set up a shared network drive (like NFS or SMB) that both can access. This lets you read/write the MDB file directly from Server A as if it's local. But keep in mind:

  • You'll need proper file permissions configured for both servers
  • Implement file locking to avoid race conditions when both servers access the file simultaneously

3. Message Queues (For Asynchronous/High-Volume Transfer)

For real-time sync or high-traffic scenarios, use a message queue system like RabbitMQ or Redis Pub/Sub. Both servers can publish messages to the queue and consume messages from it, enabling bidirectional communication without direct HTTP calls. This is ideal for tasks that don't need immediate responses.

4. Database Replication (For Persistent Data Sync)

If you're migrating away from MDB to a proper database (like MySQL), set up database replication (e.g., master-slave) to automate data sync between Server A and B. This requires more infrastructure setup but is highly reliable for ongoing data transfer.

Critical Security Reminders

  • Always use HTTPS for cross-server requests to encrypt data in transit
  • Never skip authentication (API keys, IP whitelisting, OAuth) for your endpoints
  • Validate and sanitize all incoming data to prevent injection attacks

内容的提问来源于stack exchange,提问作者Wolfzmus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:04:50