Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过php登录_Php_Curl_Login - Fatal编程技术网

通过php登录

通过php登录,php,curl,login,Php,Curl,Login,我正在尝试使用我的大学帐户登录到科学数据库。我还想使用我的php代码访问数据库。登录页面为: 为了登录,它需要用户名和密码。 在此页面中,登录表单中的操作重定向到“/login”目录,我无法遵循该操作。 我使用以下代码要求php登录: <?php $failed = ""; $user = "myuser"; $pass = "mypass"; $url = "https://login.ezproxy.lib.rmit.edu.au/login";

我正在尝试使用我的大学帐户登录到科学数据库。我还想使用我的php代码访问数据库。登录页面为: 为了登录,它需要用户名和密码。 在此页面中,登录表单中的操作重定向到“/login”目录,我无法遵循该操作。 我使用以下代码要求php登录:

    <?php
    $failed = "";
    $user = "myuser";
    $pass = "mypass";
    $url = "https://login.ezproxy.lib.rmit.edu.au/login";

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

    //set the POST parameters

    $data = curl_exec($ch);
    curl_close($ch);    
    if ($data != $failed)   //analyze the returned data
    {
        echo "Successfully logged in";
    } 
    else{
        echo "not logged in";
    }
    ?>


但这种做法并不奏效。非常感谢您的帮助。

您必须对脚本进行这些更改

  • 你需要告诉CURL你的用户名和密码是什么
  • 把曲奇饼调成卷曲状

    //username and password of account
    $username = "";
    $password = "";
    
    //set the directory for the cookie using defined document root var
    $dir = DOC_ROOT."/ctemp";
    //build a unique path with every request to store 
    //the info per user with custom func. 
    $path ="";
    
    //login form action url
    $url="https://login.ezproxy.lib.rmit.edu.au/login"; 
    $postinfo = "user=".$username."&pass=".$password;
    
    $cookie_file_path = $path."/cookie.txt";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    //set the cookie the site has for certain features, this is optional
    curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
    curl_setopt($ch, CURLOPT_USERAGENT,
        "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
    curl_exec($ch);
    
    $html = curl_exec($ch);
    curl_close($ch);
    print_r($html);
    

  • 在打开PHP标记后立即在文件顶部添加错误报告,例如
    您永远不会告诉curl您的用户名是什么。您不发布任何字段。你所做的只是一篇没有数据的“裸体”帖子。为什么OP要“试试这个”?请对您所做的事情以及您为什么这样做进行解释,不仅是为了OP,也是为了SO的未来访客。如果有什么“不清楚”,请在“评论”部分添加“尝试此项”。-正如@JayBlanchard所说的那样。插入代码并不能指示OP应该做什么以及为什么要做。现在,在你告诉OP“试试这个”的地方修复你的所有答案@FaizRasool@JayBlanchard在我去的路上:)@FaizRasool谢谢你的回复,