mceldon Posted January 25, 2007 Share Posted January 25, 2007 Hi,I have access to a site via a password protected ASP based authentication. What I'd like to do is run a script which pulls text from pages within this site using PHP.I can log into the browser and call up pages nicely, but when I try to call the pages via script I am asked to log in again (even though I have the same page open in another tab).I'm using my localhost to call the pages - how is this done ?Thanks for the help,Keith M Quote Link to comment https://forums.phpfreaks.com/topic/35652-running-php-script-in-authenticated-session/ Share on other sites More sharing options...
HuggieBear Posted January 25, 2007 Share Posted January 25, 2007 Take a look at the [url=http://uk.php.net/manual/en/ref.curl.php]CURL[/url] libraryRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/35652-running-php-script-in-authenticated-session/#findComment-168884 Share on other sites More sharing options...
mceldon Posted January 25, 2007 Author Share Posted January 25, 2007 Hi,Thanks for the reply - I've taken a look at CURL , can you elaborate a bit ?I'm trying to understand why I can view a page by putting the url in the browser (whilst authenticated on another page) whilst I can't get at the page when I run a script calling the exact same page.Thanks for your help,Keith M Quote Link to comment https://forums.phpfreaks.com/topic/35652-running-php-script-in-authenticated-session/#findComment-168968 Share on other sites More sharing options...
HuggieBear Posted January 25, 2007 Share Posted January 25, 2007 The short answer is that your authenticated browser session isn't captured in the script. Maybe someone else can give you the technical details as to why, but I'm afraid I don't have the time. I can however give you an example using the CURL library:[code]<?php/** Initialise the CURL object and provide it the URL that we want to connect to* This is normally the page that a form is calling (so in your case, the page that* your login script is calling.*/$ch = curl_init('http://www.dizzie.co.uk/php/testform.php');/** Set the options, I'll comment each of these briefly as you can find out what* each means in detail on the curl_setopt() function page on php.net*/curl_setopt($ch, CURLOPT_HEADER, FALSE); // don't return the http header to me, I only want page contentscurl_setopt($ch, CURLOPT_POST, TRUE); // I want to use the post methodcurl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // I want the output returned as a string, not direct to browsercurl_setopt($ch, CURLOPT_POSTFIELDS, "name=Richard&hello=Hello"); // Here's the post fields/** This executes the curl object and assigns the returned page contents to the variable*/$output = curl_exec($ch);/** Close the curl object*/curl_close($ch);/** Output the returned data to the screen (Showing HTML)*/echo htmlentities($output);?>[/code]OK, if you visit http://www.dizzie.co.uk/php/testform.php you'll find a simple form (this is the equivalent of your login form) and you can test it by typing in any name and then submitting the form. You'll be taken to the same page that displays a greeting.Now try going to http://www.dizzie.co.uk/php/testcurl.php which is the exact script posted above. You should get output to the screen, the exact source code of the above page.That's using CURL in it's simplest form to submit a form on another site.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/35652-running-php-script-in-authenticated-session/#findComment-168987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.