13 June 2011

PHP fopen() Tricks

kemarin nemu blog tentang trick fopen

http://teknowfile.blogspot.com/2010/05/php-fopen-tricks.html

The fopen() function in PHP allows one to read a remote file, or a web page. I recently added a feature in the logmon module to read and parse a remote syslog event definition page by its URL in the Cisco web space, such as this one documenting Cisco NX-OS events.


Here are two tricks that I have learned implementing the above feature. In my case, the server on which my code is run is on a subnet not NATted from the internal network to the outside, which means that I can not directly access an external website. There are two ways to work around this issue:
Use an IP address that is NATted or accessible from the outside;
Go through a proxy server.

Multi-Homed Server

Re-addressing a server may be a bit more involved especially in networking where IP addresses do get directly used a lot. However, a server may have more than one IP addresses. So if we add an IP address that is NATted or public, we can go back to work. To use a different IP address on the server for fopen(), one needs to use the forth parameter to fopen(): the $context.

Here is a little snippet of PHP code specifying source interface for fopen():

$srcip = '192.168.20.30'; // Assuming this is a NATted IP address
$context = stream_context_create
(
array
(
'socket' => array
(
'bindto' => $srcip,
)
)
);
($fn = fopen($url, "r", false, $context)) || die("Can not open '$url'\n\n");

while (!feof($fn)) {
echo fgetss($fn);
}


disini saya cuma menambahkan di scripts tersebut seharusnya di tambahkan port pada Nated ip address.

contoh:

$srcip = '192.168.20.30:8000'; // Assuming this is a NATted IP address




1 komentar:

Mas Rubai said...

habis baca jadi pusing tingkat dewa... hadiew....xixixixixi

powered by irvian