[dev] Modifi Login procedure

Stefan de Konink skinkie at xs4all.nl
Thu Jan 29 10:56:32 PST 2004


I did some other thing,

Send a post with username password to the login, capture the cookie, create the cookie on the client side. and send the location to the client after sending the cookie. If there is something that can do this natively i would be very pleased :) I use $_SERVER['PHP_AUTH_USER'] (Apache authentication) but you can change it to $_GET['user'] for example.

Greetings,

Stefan


the code:

<?php
/* sendToHost
 * ~~~~~~~~~~
 * Params:
 *   $host      - Just the hostname.  No http:// or 
                  /path/to/file.html portions
 *   $method    - get or post, case-insensitive
 *   $path      - The /path/to/file.html part
 *   $data      - The query string, without initial question mark
 *   $useragent - If true, 'MSIE' will be sent as 
                  the User-Agent (optional)
 *
 * Examples:
 *   sendToHost('www.google.com','get','/search','q=php_imlib');
 *   sendToHost('www.example.com','post','/some_script.cgi',
 *              'param=First+Param&second=Second+param');
 */

function sendToHost($host,$method,$path,$data,$useragent=0)
{
    // Supply a default method of GET if the one passed was empty
    if (empty($method)) {
        $method = 'GET';
    }
    $method = strtoupper($method);
    $fp = fsockopen($host, 80);
    if ($method == 'GET') {
      $path .= '?' . $data;
    }
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    if ($useragent) {
      fputs($fp, "User-Agent: MSIE\r\n");
    }    
    if ($method == 'POST') {
      fputs($fp,"Content-type: application/x-www-form-urlencoded\r\n");
      fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    }
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST') {
        fputs($fp, $data);
    }

    while (!feof($fp)) {
        $buf .= fgets($fp,128);
    }
    fclose($fp);
    return $buf;
}


        if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) 
{
                $terug=sendToHost('yourhostname.com','post','/horde/login.php','h
orde_user='.$_SERVER['PHP_AUTH_USER'].'&horde_pass='.$_SERVER['PHP_AUTH_PW']);
                if (ereg('Location: (.*)\n', $terug, $regs)) {
                        header('Location: '.$regs[1]);
                } else {
                        header ('Location: home.html');
                }
        } else {
                header ('Location: home.html');
        }
?>


More information about the dev mailing list