PDA

Click to See Complete Forum and Search --> : How do I use current login credentials?


Holywhippet
Aug 30th, 2010, 06:50 PM
I've been coding some PHP pages for data entry. One page is meant to be usable from a number of sources - both from other web pages and desktop applications. Basically you feed it some parameters, it enters a record in a database and returns a GUID value.

I need to call this page from another PHP page - both are on the same server in the same directory. The server requires the user to login before they can access the page. This works fine for accessing the PHP page where they enter the data. Problem is, when I try to make it call the PHP page where the data is saved I get authentication errors - presumably because it's running as a user on the server rather than the user they logged in as.

I can kind of get around this by using this function:
function curl_get_file_contents($URL)
{
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_USERPWD, "uname:pword");
$contents = curl_exec($c);
curl_close($c);

if ($contents) return $contents;
else return FALSE;
}


Problem is, I have to have my username and password manually entered in the code for this to work and I really don't want to do that. I want it to use the same authentication that is being used to access the first page. Any ideas please?

penagate
Aug 30th, 2010, 10:57 PM
when I try to make it call the PHP page where the data is saved

"Please explain"... PHP scripts don't store data; they manipulate and/or present it. I'm a bit confused why you want to invoke one script from another script. Why can't you just access the database using the same approach in both scripts?

In answer to (what I think is) your immediate problem, you can get the credentials from $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'].

If you could elaborate a little bit more we could advise a better approach.

Holywhippet
Aug 30th, 2010, 11:01 PM
As I said, I'm going to be calling some of the PHP I write from multiple locations - like from other web pages and other applications. So I've got a PHP page that will receive data, store it in a database and write out a GUID created for the stored record. I could write my own database storage routines into each application but that would mean adjusting all of them if something changes.

penagate
Aug 30th, 2010, 11:13 PM
Okay, but why invoke a PHP script from a PHP script?

Why not put the underlying functionality into methods in an include file which is then used by both scripts?

I appreciate that you should never have change the database routines in more than one place.

Holywhippet
Aug 31st, 2010, 05:08 PM
I was mainly just trying to get into the "invoke via the web" mentality mostly. Doing it via include did occur to me, I just felt it was kind of "cheating" since I won't be able to do it via other call methods. Might as well though, it should be a little bit faster as a result.

Thanks.

penagate
Aug 31st, 2010, 07:10 PM
Think of the PHP scripts as an interface to a single application (or service, or library, or whatever your preferred term). The scripts are analogous to methods: they do different things, but use the same underlying routines and work with the same data.