fsockopen(), fopen() and file()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Loopy

    fsockopen(), fopen() and file()

    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
    file.

    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("K igurumi.co.uk", 80)) {
    echo "Connect 1 OK";
    }
    else {
    echo "Connect 1 Failed";
    }

    echo "\n";

    if(fsockopen("y ahoo.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.u k" for example for fsockopen())

    I have spoken to my ISP, and they have mentioned that they have
    recently implemented a register_global s 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_global s 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.

    Regards

    Loopy

  • Senator Jay Billington Bulworth

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

    "Loopy" <jjyooi@yahoo.c om> wrote in news:1110360731 .972876.295620
    @g14g2000cwa.go oglegroups.com:
    [color=blue]
    > 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.[/color]

    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_global s would have nothing to do with
    this.

    hth


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

    Comment

    • Loopy

      #3
      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_global s --
      which it isn't, as you've just confirmed. I'm going to send them the
      Google Groups URL and let them read this. :)


      Loopy

      Comment

      Working...