我尝试使用 php 会话。
创建会话:
<?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');
这工作很好。
现在我尝试从另一个文件中调用它:
<?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";
}
并获得输出:
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
发生什么事了?为什么我会收到警告?