call url by php and checking if logged in [closed]

2019-08-17 05:23发布

问题:

Given url (https://127.0.0.1:8443/example.html <- yes it's secure connection) is checking if user is logged in (by jboss server) and if so, he has permissions to some content.

When I logged in and try to run this url in browser, everything is ok, jboss logger shows that user is logged in, but when I call file_get_content(url) from php script (http://127.0.0.1/test/check <- codeigniter url, code below) user always is logged out in jboss. Don't know why.

I've tried curl library, as well without success.

public function check(){
    echo 'begin';
//      $total_rows = file_get_contents('https://127.0.0.1:8443/example.html?shopId=121');
    $total_rows = $this->getUrl('https://127.0.0.1:8443/example.html', '121');
    print_r($total_rows);
    echo 'end';
}

function getUrl($url, $shopId ='') {
    $post = 'shopId=' . $shopId;

    $ch = curl_init();
//      curl_setopt($ch, CURLOPT_PORT, 8443);  
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//      curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/../../files/cacert.pem");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
//        curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Secure Content-Type: text/html; charset=UTF-8")); 
//        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: 127.0.0.1:8443'));

    $ret = curl_exec($ch);
    curl_error ($ch );
    curl_close($ch);
    return $ret;
}

回答1:

In short: that's not how the web works.

When you enter https://example.com into your browser and log in there, your browser receives a cookie and is logged in.

When you fetch the URL https://example.com from a completely unrelated server (even if that server is on the same machine), it's an entirely different request from a new client, and you will correspondingly not be logged in. It's the same as if you open another browser (say, Firefox and Chrome); you're not automagically logged in with both.