开发者

set variable _SERVER["PHP_AUTH_USER"] using php

I need a little help with php_auth I have used HTTP authentication with Php and it seems to working fine (when you go to the url it asks for username and password and is working fine) but what i need to do i开发者_Python百科s pass the username and password for the authentication from another page and authenticate automatically. Something like set variable _SERVER["PHP_AUTH_USER"] using php from another page.

Edit: I forgot to mention that My problem is that two pages are completely different application and both maintain separate sessions. Application "A" and Application "B" may or may not be hosted on the same domain.


I used a zend function setAuth(username, password); to get the work done.

$client = new Zend_Http_Client();
$client->setAuth($username, $password);
$response = $client->request('POST');

How the setAuth function works is it embeds/encodes the authentication params as header to the url

// Set HTTP authentication if needed
if (is_array($this->auth)) {
     $auth = self::encodeAuthHeader($this->auth['user'], $this->auth['password'],      $this->auth['type']);
       $headers[] = "Authorization: {$auth}";
}


You can pass around whatever data about the user you want between requests with sessions.

//on one page
session_start();
$_SESSION['logged_in_user'] = $_SERVER['PHP_AUTH_USER'];

//on the 'another page'
session_start();
$user = $_SESSION['logged_in_user'];

http://php.net/manual/en/features.sessions.php


You should use the $_SESSION for this. $_SESSION is meant to pass variable from pages.

on each script when you need to access a session variable :

session_start();
$_SESSION['user'] = $_SERVER['PHP_AUTH_USER'];

And on the other script :

session_start();
$user = $_SESSION['user'];

Have a look here : http://www.tizag.com/phpT/phpsessions.php and there for the php reference : http://www.php.net/manual/en/session.examples.basic.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜