fsockopen(), fopen() and file()

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm trying to write a script that will connect to an external site so
that I can get access referer information in a database on another one
of my sites. I got errors, so I tried to write a script that will test
the three main forms of connecting externally: fsockopen, fopen and

I wrote a simple script that connects to my own domain
(http://www.kigurumi.co.uk ) and to another external domain
(http://www.yahoo.co.uk ), I do nothing other than connect.

This is the script:

if(fsockopen("Kigurumi.co.uk", 80)) {
  echo "Connect 1 OK";
else {
  echo "Connect 1 Failed";

echo "\n";

if(fsockopen("yahoo.co.uk", 80)) {
  echo "Connect 2 OK";
else {
  echo "Connect 2 Failed";

echo "\n";

if (fopen("http://www.kigurumi.co.uk ", "r")) {
  echo "Connect 3 OK";
else {
  echo "Connect 3 Failed";

echo "\n";

if (fopen("http://www.yahoo.co.uk ", "r")) {
  echo "Connect 4 OK";
else {
  echo "Connect 4 Failed";

echo "\n";

if (file("http://www.kigurumi.co.uk ")) {
  echo "Connect 5 OK";
else {
  echo "Connect 5 Failed";

echo "\n";

if (file("http://www.yahoo.co.uk ")) {
  echo "Connect 6 OK";
else {
  echo "Connect 6 Failed";

echo "\n";

When I run it, I get the following messages (for obvious security
reasons, I have hidden part of my directory):

Connect 1 OK
Warning: fsockopen(): unable to connect to yahoo.co.uk:80 in
[hidden]htdocs/test.php on line 11
Connect 2 Failed Connect 3 OK
Warning: fopen(http://www.yahoo.co.uk ): failed to open stream:
Permission denied in [hihdden]htdocs/test.php on line 29
Connect 4 Failed Connect 5 OK
Warning: file(http://www.yahoo.co.uk ): failed to open stream:
Permission denied in [hidden]htdocs/test.php on line 47
Connect 6 Failed

You can see that when I connect to my own domain (connects 1, 3 and 5),
it works fine, but whenever I connect to the external site, (connects
2, 4 and 6), it fails. It can't be to do with the argument format,
since I'm using the same format for both calls (e.g. "kigurumi.co.uk"
and "yahoo.co.uk" for example for fsockopen())

I have spoken to my ISP, and they have mentioned that they have
recently implemented a register_globals set to off policy, but this
doesn't affect my code, since I'm not using any variables, especially
global ones. They are not really helping me very much, much as I hate
to say it.

If I run this script on my own machine (WinXP, SP2, php 4.2.1,
register_globals off) using:

c:\php\php.exe test.php

I get Connect 1 OK through to Connect 6 OK, so there's obviously a
difference in configuration, but _what_?

Does anyone know why this is happening, or what I can do to allow
myself to connect to external machines.



Re: fsockopen(), fopen() and file()


Quoted text here. Click to load it

Regarding the fopen() and file() calls, it sounds like your host has
allow_url_fopen turned off. This doesn't explain fsockopen() failing,
though. The errors you're getting are not consistent with a safe mode
installation, either, so it's difficult to pinpoint one cause.

Is Kigurumi.co.uk hosted on the same machine you're running the script
on? If so, try a test subject other than Yahoo, like Google or BBC, and
see if it can connect to them. This will either show that Yahoo is
blocking your server, or that your server is blocking outbound
connections. If the latter, ask your host if they have a firewall
preventing egress on port 80.

You're correct in that register_globals would have nothing to do with


Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
<http://www.phplabs.com/ | PHP scripts, webmaster resources

Re: fsockopen(), fopen() and file()

Thanks for the information about that. I've forwarded it to my ISP and
I'll see what they say. They have tried to help me on numerous
occasions, but they keep saying it's to do with register_globals --
which it isn't, as you've just confirmed. I'm going to send them the
Google Groups URL and let them read this. :)


Site Timeline