cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Re: Need guidance using cURL and Apache authentication

From: Shanshui Liu <me_at_lx.sg>
Date: Thu, 20 May 2010 04:46:28 +0800

Hi Charles,

CURLOPT_FOLLOWLOCATION simply means that cURL would follow all Location:
headers, which are typically used for redirections. If that option is
disabled, when fetching a page with a Location: header, it will not continue
by fetching the URL it is redirected to and instead return a page with a
HTTP status message only.

The reason why your browser's address bar does not reflect the origin URL is
because all this cURL data fetching is happening on the server. Your best
option would still be using PHP to parse and correct the links before
outputting them.

If the files for download also reside in a directory requiring HTTP
authentication, your users will still be asked for an username and a
password when they try to download the files. In this case, you'll need
another PHP script that accesses the files directly and output them to your
users (eg. download.php?file=/downloads/dms/DMSv64Update.zip - don't forget
to perform data sanitization on the file path), then have your links to
point to this script instead.

With regards,
Liu Shan Shui
http://lx.sg/
"Life would be much easier if I had the source code." - Anonymous

On Wed, May 19, 2010 at 12:33 PM, Charles <cecrume_at_gmail.com> wrote:

> Hello;
>
> I'm have posted msgs to the curl-library mailing list, but am new to this
> list. I posted the message below and got some great help from Ralph
> Mitchell, but we were unable to actually get the URL transfer done. He
> suggested I post the msg here.
>
> So...
>
> I have a little membership system that I wrote in PHP and run on an Apache
> v2.0 web server. A couple of pages are protected with .htaccess files.
>
> When a user logins into my membership system I don't want them to have to
> enter the username/password that Apache normally requires to gain access.
>
> I have made some progress using cURL and can get the protected page to
> appear -- but cURL does not transfer to the actual URL specified. The
> browser shows the page, and it's contents, but the links (the page contains
> files to download) are bad because they are relative links.
>
> 1) Login to membership system.
> 2) Click on link "Download Updates" link on main page and it runs
> http://www.charlescrumesoftware.com/members/user_downloads.php
> 3) The above PHP script runs the following cURL code (among other stuff):
>
> $Cookie_Jar = "Cookie_Jar.doc";
> $c = curl_init("http://www.charlescrumesoftware.com/downloads/dms");
> // the actual page I want
> curl_setopt($c, CURLOPT_COOKIE, 1);
> curl_setopt($c, CURLOPT_COOKIEJAR, "$Cookie_Jar");
> curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); // -L
> curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
> curl_setopt($c, CURLOPT_USERPWD, "UserName:Password");
> // curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // save cURL
> output to file
> curl_setopt($c, CURLOPT_RETURNTRANSFER, 0); // allows us to
> FOLLOW
> $myDOWN_Page = curl_exec($c);
> curl_close($c);
> 4) the browser show the contents of the "download" page correctly -- but IE
> shows http://www.charlescrumesoftware.com/members/user_downloads.php in
> the address bar.
>
> 5) If I click on the "Parent Directory" link I am taken up one level in the
> directory structure -- as I should.
>
> 6) However... All the links to files that can be downloaded are relative
> links:
>
> <a href="DMSv64Update.zip">
>
> so I get taken to a page that does not exist.
>
> http://www.charlescrumesoftware.com/members/DMSv64Update.zip
>
> I thought about POSTing the username and password, but am not sure what the
> variable names are that Apache recognizes.
>
> I also realize I could save the page's contents, use PHP to alter all the
> links, then display the result -- but would prefer to just access the page
> directly, if possible.
>
> Can someone shed some light on what I am overlooking here? Perhaps I am
> not understanding how CURLOPT_FOLLOWLOCATION works.
>
> TIA.
>
> Charles...
> -----------------------
> The more adapted you become... the less adaptable you are!!
> Thanks.
>
> Charles...
> -----------------------
> MIST (Man In The Street) Law: The number of people watching you is directly
> proportional to the stupidity of your action.
>
> _______________________________________________
> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
>
>

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2010-05-19