Jump to content

Running PHP script in Authenticated session


mceldon

Recommended Posts

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
Link to comment
Share on other sites


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
Link to comment
Share on other sites

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 contents
curl_setopt($ch, CURLOPT_POST, TRUE); // I want to use the post method
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // I want the output returned as a string, not direct to browser
curl_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.

Regards
Huggie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.