通过php调用url并检查是否已登录 [英] call url by php and checking if logged in

查看:265
本文介绍了通过php调用url并检查是否已登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出 url ( https://127.0.0.1:8443/example.html<-是的,它是安全连接)正在检查用户是否已登录(通过jboss服务器),如果有,他有权访问某些内容.

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.

当我登录并尝试在浏览器中运行此 url 时,一切正常,jboss logger显示该用户已登录,但是当我调用 file_get_content(url)时>从php脚本( http://127.0.0.1/test/check<-codeigniter url,下面的代码)中,用户始终在jboss中注销.不知道为什么.

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.

我也尝试过 curl 库,但没有成功.

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;
}

推荐答案

总之:网络不是这样的.

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

当您在浏览器中输入https://example.com并登录时,您的浏览器会收到cookie并登录.

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

当您从完全不相关的服务器(即使该服务器位于同一台计算机上)中获取URL https://example.com时,这是与新客户端完全不同的请求,您将相应地 not 登录.这与您打开另一个浏览器(例如Firefox和Chrome)相同;您不会自动登录.

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.

这篇关于通过php调用url并检查是否已登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆