CURL to pass Username/Password from PHP
Results 1 to 7 of 7

Thread: CURL to pass Username/Password from PHP

  1. #1
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    CURL to pass Username/Password from PHP

    I found the following script (basic printer-friendly layout thing from vsbabu.org) and it is working great on my company's intranet. However, it does not work when attempting a printer-friendly format from a page contained inside a password protected directory (only returns a printer-friendly version of my 401 Auth Reqd page).

    Either the script has to pass a username/password to validate and display the page or the htaccess files has to somehow ignore requests coming from the localhost. I cannot figure out how to do either - am at roadblock can't think of anything else to try.

    I did get one suggestion to remove the header lines at the top, however, that just produced a whole lot of garbage on the resulting page. Can PHP pass a username/password string to the server to display the page or does this have to be done somehow from the .htaccess file itself?

    SCRIPT:
    <?
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modifieed: ". gmdate("D, d M Y H:i:s"). " GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Cache-Control: post-check=0,pre-check=0");
    header("Cache-Control: max-age=0");
    header("Pragma: no-cache");
    /*
    || Takes the referrer document, only prints the content
    || works only with documents on the same server. good enough :-)
    || V.Satheesh Babu (vsbabu@hotmail.com).
    ||
    || I put an HTML comment called "document_starts_here" before
    || content starts and another one "document_ends_here" after
    || the content. If you have been following some standard like
    || that, it could be useful.
    ||
    || Installation instructions
    || 1. Copy paste the code here to your favourite editor, and save it as some php3 file
    || 2. The file name doesn't matter. I call it printit.php3
    || 3. From your other web pages make a link to printit.php3
    || 4. Test it out by clicking on the links in those pages
    ||
    */
    if (empty($HTTP_REFERER)) {
    $myURL = 'http://opsweb.mts.mb.ca/';
    } else {
    $myURL = $HTTP_REFERER;
    }
    //if ($HTTP_REFERER)
    //*** $myURL = $HTTP_REFERER;
    //else
    //*** $myURL = "http://opsweb.mts.mb.ca/";

    // here is the starting comment for your content
    $START_CONT="document_starts_here";
    // here is the ending comment for your content
    $END_CONT = "document_ends_here";

    $parsed_url = parse_url($myURL);
    $myServer = $parsed_url['host'];
    $document = $parsed_url['path'];
    if($document[strlen($document)-1]=='/') { $document = "$document/index"; }
    $base_url = dirname($document);

    $fp=fsockopen($myServer,80,&$errno,&$errstr,30);

    // if you've name based virtual hosting
    //$request = "GET $document HTTP/1.1\r\n";
    //$request .= "Host: $myServer\r\n\r\n";
    /* if you've normal old type servers,
    comment out the previous two lines and
    use the next line instead
    */
    $request = "GET $document HTTP/1.0\r\n\r\n";


    if(!$fp) {
    echo "$errstr ($errno)<br>\n";
    } else {
    fputs($fp,$request);
    $content=0;
    $in_title=0;

    ?>

    <HEAD>
    <BASE HREF="<? echo "http://$myServer$base_url/"; ?>">
    <LINK REL="stylesheet" TYPE="text/css" href="http://opsweb.mts.mb.ca/includes/opscontent.css">
    <style type="text/css">
    <!--
    body {margin: 0px;}
    -->
    </style>
    <?
    while(!feof($fp)) {
    $line=fgets($fp,4096);
    // this is the stupid part - get the title
    // python makes this so easy :-)
    if(eregi("<title>",$line)){ $in_title=1; }
    if ($in_title==1) $title.=$line;
    if(eregi("</title>",$line)){
    $in_title=0;
    if(!eregi("<title>",$line))
    $title.=$line;
    }
    if(ereg($START_CONT,$line)) {
    $title = eregi_replace("</title>","",eregi_replace("<title>","",$title));
    echo "<TITLE>$title</TITLE>\n</HEAD>\n<BODY>\n";
    $content=1;
    }
    if(ereg($END_CONT,$line)) $content=0;
    if($content==1)echo $line;
    }
    fclose($fp);
    ?>

    </BODY>

    <?
    }
    ?>

    If anyone can help me to get this working I'd be very grateful!!!

    Thanks
    Debbie

  2. #2
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: CURL to pass Username/Password from PHP

    You can pass an .htaccess username & pass in a url:

    Say the username is user
    Password is pass

    http://user:pass@www.htaccessprotected.com/dir/

    Not sure if that's what you mean...

  3. #3
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: CURL to pass Username/Password from PHP

    No, not really.

    The original file is in /htdocs/dir/dir1/passwordprotdir/
    The printer friendly script is in /htdocs/dir/includes/

    When the original file is passed to the printer-friendly script page, for some reason it thinks it has to authenticate again and displays the AuthReqd error page.

    Does this help?

  4. #4
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: CURL to pass Username/Password from PHP

    Changing:

    $request = "GET $document HTTP/1.0\r\n\r\n";

    To:

    $request="GET $document HTTP/1.0\r\n";
    $request.="Authorization: basic ".base64_encode("$username:$password")."\r\n\r\n";

    should work.


  5. #5
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: CURL to pass Username/Password from PHP

    $request.="Authorization: basic ".base64_encode("$username:$password")."\r\n\r\n";

    should be

    $request.="Authorization: basic ".base64_encode('$username:$password')."\r\n\r\n";

    I really should test things before writing them :)

  6. #6
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: CURL to pass Username/Password from PHP

    Ok, ignore that - first one /was/ right after all!

  7. #7
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: CURL to pass Username/Password from PHP

    kybosh

    THANK YOU!!! This worked perfectly!!! I forgot to respond earlier on this.

    Debbie

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •