-1

I try with php sessions.
Create session:

<?php
function loginGeoserver($username, $password) {

$geoserverURL = "http://localhost:8080/geoserver/j_spring_security_check";

$post = http_build_query(array(
        "username" => $username,
        "password" => $password,
));

$context = stream_context_create(array("http"=>array(
    "method" => "POST",
    "header" => "Content-Type: application/x-www-form-urlencoded\r\n" .
            "Content-Length: ". strlen($post) . "\r\n",
    "content" => $post,
)));

$page = file_get_contents($geoserverURL, false, $context);


for($i = 0; $i < sizeof($http_response_header); $i++){

    $headerLine = $http_response_header[$i];

    $pos = strpos($headerLine, 'Set-Cookie');

    if ($pos === 0) {
            $str = explode("=",$headerLine);
            $value = explode(";",$str[1]);
            $cookieValue = $value[0];
            break;
    }

}

$cookieName = "JSESSIONID";
$cookieDomain = "http://localhost:8080";
$cookiePath = "/geoserver";
$cookieExpiration = 0;

setcookie($cookieName,$cookieValue,$cookieExpiration,$cookiePath);
return $cookieValue;
}
//loginGeoserver('new_user','123456');

This work fine.
Now i try call it from another file:

<?php
// newSession.php <user name> <password>
require_once "bootstrap.php";
require_once "geoserv.session.php";
$username = $argv[1];
$password = "plain:".$argv[2];
$user = $entityManager->find('Users', $username);
$pass = $user->getPassword();
if($pass == $password){
    echo "Auth successful\n";
    echo loginGeoserver($username,$password)."\n";
}else{
    echo "Access denied";
}

And get output:

D:\xampp\htdocs\doctrine2-tutorial>php geoserv.auth.php new_user 123456
Auth successful

Warning: Cannot modify header information - headers already sent by (output star
ted at D:\xampp\htdocs\doctrine2-tutorial\geoserv.auth.php:10) in D:\xampp\htdoc
s\doctrine2-tutorial\geoserv.session.php on line 41
12e19vgpggha2

Whats heppens? Why i get Warning?

Kliver Max
  • 5,107
  • 22
  • 95
  • 148

2 Answers2

2

Your script is outputting data prior to the header being set, which is a no-no.

You can see this as indicated by the error on line 10:

echo "Auth successful\n";

And then line 11, loginGeoserver() is called afterwards, which sets the header.

Try swapping line 10 and 11 around, and see what happens :)

David Houde
  • 4,835
  • 1
  • 20
  • 29
2

You output data in your code before you try to redirect the user.

if($pass == $password){
    echo "Auth successful\n";
    // ^^ this is the no-no line.
    echo loginGeoserver($username,$password)."\n";

you cannot user headers like redirect if you have sent ANYTHING to the client.

If you remove that line, it should work a charm.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80