Results 1 to 6 of 6

Thread: How do I use current login credentials?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    How do I use current login credentials?

    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:
    Code:
    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?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I use current login credentials?

    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    Re: How do I use current login credentials?

    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.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I use current login credentials?

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    Re: How do I use current login credentials?

    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.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How do I use current login credentials?

    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.

Posting Permissions

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



Click Here to Expand Forum to Full Width