Pavel Koryagin
Pavel Koryagin

Reputation: 1481

Can I open socket in PHP from a specific IP (if the machine has two IPs)?

I'm using PHPMailer and it uses fsockopen to access the SMTP server.

But the machine has two IPs with different reverse DNS records. So in email headers I got the following:

Received: from one-server.tld (HELO another-server.tld) ...

I need to hide one-server.tld in favor of another-server.tld. But I need both IPs with their current RDNS settings.

Upvotes: 3

Views: 1467

Answers (1)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

I think its not possible using fsockopen. But its possible in curl, fopen and stream functions. What you need is stream_socket_client() function.

Here are some ways to achieve it.

  1. Using context parameters which can be used in fopen function family and stream function family. See the example.

    $opts = array(
        'socket' => array(
            'bindto' => '192.168.0.100:0',
        ),
    );
    // create the context...
    $context = stream_context_create($opts);
    $contents = fopen('http://www.example.com', 'r', false, $context);
    

    Also stream_socket_client

    $fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $opts);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
        while (!feof($fp)) {
            echo fgets($fp, 1024);
        }
        fclose($fp);
    }
    
  2. Using socket_bind. PHP.NET got a simple example here.

Upvotes: 8

Related Questions