2015-06-15 146 views
0

我想用我的大学账号登录科学数据库。我想用我的php代码来访问数据库。登录页面是: https://login.ezproxy.lib.rmit.edu.au/login并且为了登录,它需要用户名和密码。 在此页面中,登录表单中的操作将重定向到“/ login”目录,我无法按照要执行的操作。 我用下面的代码要求PHP登录:通过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"; 
    } 
    ?> 

但是这并不好操作。感谢您的帮助。

+0

例如您打开PHP标签 '

+0

你永远不会告诉curl你的用户名是什么。你不会发布任何字段。你所做的只是一个“裸”的职位,** ** **数据。 –

回答

0

你必须做出这些改变你的脚本

  1. 你需要告诉卷曲什么是你的用户名和密码。
  2. 设置卷曲饼干

    //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); 
    
+1

为什么OP要“试试这个”?请添加一个解释,你做了什么,为什么你这样做,不仅是为了OP,而且是为了未来的访问者。 –

+0

*“试试这个”*属于评论部分,当某些东西“不清楚”时。 - 就像@JayBlanchard所说的那样。放在代码中并没有指示OP应该做什么以及为什么。 –

+0

@JayBlanchard我已经更新了答案:) –