#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    94
    Rep Power
    6

    Script fopen's a webpage.php file, but get 401 error through browser viewing


    Hi,

    I am writing a script which when called through the browser, uses fopen to create a webpage eg:

    index.php

    In index.php I have some HTML to create a normal website page, and in it I also have some PHP:

    PHP Code:
    <?php include_once('http://www.mysite.com/includes/footer.php'); ?>
    But when I go to look at the newly create webpage I cannot view it through the browser. I get an error:

    Warning: include_once(http://www.mysite.com/includes/footer.php) [function.include-once]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in /home/username/public_html/mysite.com/index.php on line 10

    Warning: include_once() [function.include]: Failed opening 'http://www.mysite.com/includes/footer.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/username/public_html/mysite.com/index.php on line 10
    I'm on Hostgator shared hosting, which uses 644 permissions by default, I tried changing to 755, but was still the same.

    To create the file I have a php file, say createpage.php and in it I have:

    PHP Code:
    $handle fopen($fname'w') or die("can't open file"); file_put_contents($fname$stemplate); fclose($handle); 
    I then view createpage.php through my normal webbrowser, and it starts the script.

    I remember then when I ran this same script on a localhost LAMP stack, in Ubuntu the GUI placed little locked padlock symbols over the files which were created by the script. I am feeling this is the problem, hence why I tried chmodding them to 755, but it didn't work.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    867
    Rep Power
    275
    I don't believe you can use a URL for the filepath when using include() or include_once().

    To get around this you could try using this
    PHP Code:
    include_once($_SERVER["DOCUMENT_ROOT"] . "includes/footer.php"); 

    Comments on this post

    • Triffic agrees : Thanks for taking the time to help :)
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    '401 Authorization Required' means you have password protection on the URL: http://www.mysite.com/includes/footer.php

    I assume that if you open this manually in your browser you would see a password prompt. Since your script is not supplying a username and password when it requests the URL using include, authorization fails.

    However, it sounds very much like you should not be using a URL include here.

    Comments on this post

    • DonR agrees : wow, i skipped right over that 401 error.
    • Triffic agrees : Thanks for taking the time to help :)
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    94
    Rep Power
    6
    Thank you yes that has fixed it.

    I have been developing the site whilst the domain is password protected behind .htaccess login.

    I am logged in, and have access to the files/pages which were all ready there, I have turned off password protection and it works fine

    Seems using fopen whilst logged in caused the 401 upon the newly created files.

    I was not expecting that. Thank you so much
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    That's not actually the right solution though; the right solution is not to use a URL for include. Using a filesystem path like DonR showed. Using a URL in that code is almost certainly not what you mean to do.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    If you use the URL to include the file, the file is executed BEFORE you include it. You don't want that, it forces your server to make a round-trip to ITSELF, wasting an open socket connection and changing the scope of the included file.

    I'm going to be the third person to say this and I'm going to be very explicit because it doesn't seem like you've heard: For the third time, you are doing this wrong. This is incorrect. Do not do it like this.

    Use filesystem includes, NOT URLs. For real.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo