Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify source ip using fsockopen

Tags:

php

sockets

On a server with multiple IPs routed to it, I'd like to use PHP's fsockopen to open from a non-primary-interface ip (or a comparable method to be able to make fread and fwrites from a different ip)

like image 927
Professional Sounding Name Avatar asked Jan 22 '11 00:01

Professional Sounding Name


People also ask

Is similar to Fsockopen () in PHP?

The function stream_socket_client() is similar but provides a richer set of options, including non-blocking connection and the ability to provide a stream context.

How do I check Fsockopen?

This sample code checks to see if the fsockopen() function is available. To check for another function, change the $function_name variable's value: <? php $function_name = "fsockopen"; if ( function_exists($function_name ) ) { echo "$function_name is enabled"; } else { echo "$function_name is not enabled"; } ?>


1 Answers

This is not possible with fsockopen. You have to use the sockets wrapper:

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, '192.168.1.100');
socket_connect($sock, 'stackoverflow.com', 80);
like image 131
netcoder Avatar answered Sep 22 '22 20:09

netcoder