donglu4633 2013-08-22 09:06
浏览 49
已采纳

不能用PHP做POST

I found function:

function loginGeoserver($username, $password) {

$geoserverURL = "http://localhost:8080/geoserver/j_acegi_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
" .
            "Content-Length: ". strlen($post) . "
",
    "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 = "localhost:8080";
$cookiePath = "/geoserver";
$cookieExpiration = 0;

setcookie($cookieName,$cookieValue,$cookieExpiration,$cookiePath);

return $cookieValue;
} 
loginGeoserver('admin', 'geoserver');

But when try it i get an error:

Notice: Undefined variable: cookieValue in D:\xampp\htdocsest\index.php on line 87

So how i understand that mean that this line $page = file_get_contents($geoserverURL, false, $context); not work.
Im new with php. Give me any ideas why its not work.
And more i dont see that something posted on url http://localhost:8080/geoserver/j_acegi_security_check.

  • 写回答

1条回答 默认 最新

  • duanruanxian5028 2013-08-22 09:08
    关注

    If $pos is different than 0 the variable $cookieValue is not defined:

    You only define it in the case is 0:

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

    You should define it at the start of the function with some default value for the case $pos is not 0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度