1

Is it possible to pass PHP authorization by code? I also saw authorization in a jQuery ajax request, with ajax authorization is it possible to pass PHP authorization?

I get a page result by ajax request and i got the result, no if I add this code to the top of PHP script :

$valid_passwords = array ("admin" => "123456");
$valid_users = array_keys($valid_passwords);

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

if (!$validated) {
  header('WWW-Authenticate: Basic realm="My Realm"');
  header('HTTP/1.0 401 Unauthorized');
  die ("Not authorized");
}

Now it wants to put user and password. Is it possible to put username and password by code? In the ajax request there is something for authorization, can it be used for that?

4
  • How do you mean passing by code? With cURL? In a cookie? In the header? Commented Oct 13, 2014 at 16:00
  • i mean when header is header('WWW-Authenticate: Basic realm="My Realm"'); header('HTTP/1.0 401 Unauthorized'); is it possible to put user and pass by code when requesting a page? Commented Oct 13, 2014 at 16:01
  • you can send authentication with url http://user:[email protected] Commented Oct 13, 2014 at 16:05
  • I think this is the thing that i was looking for , let me give try Commented Oct 13, 2014 at 16:07

2 Answers 2

3

Sure it is. Look into cURL functions. PS, you'll get a better answer if you post more info.

EDIT...

In order to authenticate the user on the remote system you need to know how authentication is performed. Most commonly you'll need to use CURL to POST the username and password to a login page and save the cookie it sends back for future curl calls.

function Request($url,$params=array()){
    $cookiefile = "/path/to/textfile";
        if(!file_exists($cookiefile)){
            @fopen($cookiefile, "w");
            if(!file_exists($this->cookiefile)){
                echo 'Cookie file missing. '.$cookiefile; exit;
            }
        }else if(!is_writable($cookiefile)){
            echo 'Cookie file not writable. '.$cookiefile; exit;
        }
        $ch = curl_init();
        $curlOpts = array(
            CURLOPT_URL             => $url,
            CURLOPT_USERAGENT       => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0',
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_COOKIEJAR       => realpath($cookiefile),
            CURLOPT_COOKIEFILE      => realpath($cookiefile),
            CURLOPT_FOLLOWLOCATION  => true,
            CURLOPT_HTTPHEADER      => array(
                "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
                "Connection: keep-alive",
                "Content-Type: application/x-www-form-urlencoded")
        );
        if(!empty($params)){
            $curlOpts[CURLOPT_POST] = true;
            $curlOpts[CURLOPT_POSTFIELDS] = $params;
        }
        curl_setopt_array($ch,$curlOpts);
        $answer = curl_exec($ch);
        if (curl_error($ch)) {
            echo curl_error($ch); exit;
        }
        curl_close($ch);
        return (@gzdecode($answer)) ? gzdecode($answer) : $answer;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

thanks let me try this too just two thing one : what's ajax request authorization role? and why they voted down !
I didn't downvote you, but i imagine it's because "authorization" is an extremely general term.. That's like asking "how to use a computer" there's just no single answer the way you posed the question.
aha I got how was my question :) great Thanks
2

you can pass user & paas in url as fallows

http://username:[email protected]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.