首页 > 解决方案 > 使用 curl 登录 PHP 站点

问题描述

我尝试登录 PV-Meter,它使用 php+cookies 进行登录。如果我用 Firefox 复制帖子,我会得到这个

curl 'http://192.168.96.68/shared/getDeviceData.php' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0' -H 'Accept: */*' -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' --compressed -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Origin: http://192.168.96.68' -H 'DNT: 1' -H 'Connection: keep-alive' -H 'Referer: http://192.168.96.68/home.php' -H 'Cookie: lang=de; PHPSESSID=tqoehqr3n9iclggolii9rc0mn2; lang=de' --data-raw 'showActiveDeviceStatus=true'

它工作,unitl cookie exprires 所以我想,我必须登录,保存 cookie 并再次调用页面,但我没有成功

如果我尝试:

curl --cookie-jar cookies.txt --data passphrase=pass.word --data username=admin http://192.168.96.68/home.php

之后 curl -b cookies.txt http://192.168.96.68/home.php

-> 没有成功

有什么想法我忘记了吗?

html页面代码

<!DOCTYPE HTML> <html lang="en"><head><title>MaxWeb XPN</title><meta charset="UTF-8" />
    <meta name="theme-color" content="#fcbf00">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="manifest" href="manifest.json">
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/font-awesome.css" /> 
    <link rel="stylesheet" href="css/unicorn-login.css" />

    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="css/xpn.css" />        
    <!--[if lt IE 9]>
    <script type="text/javascript" src="js/respond.min.js"></script>
    <![endif]-->
</head>    
<body>
    <div id="container">
        <div id="logo" >
        <img class='img-responsive' src='img/logo_xpn.png'/>            </div>
        <div id="loginbox">            
            <form id="loginform" action="home.php">
                <br>
                <div class="input-group input-sm">
                    <span class="input-group-addon"><i class="fa fa-user"></i></span><input class="form-control" type="text" id="username" placeholder="Benutzername" />
                </div>
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-lock"></i></span><input class="form-control" type="password" id="password" placeholder="Passwort" />
                </div>
                <div class="form-actions clearfix">
                    <input type="submit" class="btn btn-block btn-primary btn-default" value="Login"/>
                    <br>
                    <div class="pull-right">
                        <a href="#recoverform" class="flip-link to-recover grey"><div class='fa fa-question'></div> Support</a>
                    </div>
                </div>
            </form>
            <form id="recoverform" action="#">
                <p><b>Anlagenname:</b> SolarPlant
            <br> <b>Seriennummer:</b> 00000000
            <br><b>Firmware-Version:</b> 0.0.0
            <br><b>Hardware-Version:</b> 0.0.0
            <br><b>Ident:</b> XXXXXXXXXXXXXXX<br>
            <br><b>Hotline:</b><br>
            DE +49 3733 507840 <br>
            CH +41 315 281 165 <br>
            GB +44 20 38080346 <br>
            ES +34 93 2203859 <br>
            FR +33 820420684 <br>
            IT +39 0418520076 <br></p>
                <div class="form-actions clearfix">
                    <div>
                                                    <a href="mailto:hotline@solarmax.com?
                        subject=sxxxxxxxx%20Support%xxxxxxxx%20xxxxxxxxxx&amp;
                        body=%0A%0A--------------------------------%0AMaxWeb%20XPN%20Service-Info:%0A%20Serial:%xxxxxxxxx%0APlantname:%20xxxxxxx%0AFirmwareversion:%20x.x.x%0AHardwareversion:%20x.x.x%0AIdent:%20somaxxxxxxx%0A--------------------------------%0A" 
                        class="btn btn-block btn-inverse"><div class="fa fa-envelope"></div> E-Mail Support</a>
                    </div>
                    <br>
                    <div>
                     <a href="#loginform" class="btn btn-block btn-primary flip-link to-login"><div class="fa fa-arrow-left"></div> Login</a>
                    </div>
                </div>
            </form>
        </div> 
        <div>
            <img class='img-responsive' src='img/solarmax_logo.png'/>
        </div>
    </div>
            
    <script type="text/javascript" src="js/respond.min.js"></script>
    <script src="js/jquery.min.js"></script>  
    <script src="js/jquery-ui.custom.min.js"></script>
    <script src="js/unicorn.login.js"></script> 
    
</body>
<div style="display:none;" id="dialog-confirm" title="Service-Modus">
      <p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>Im Service-Modus sind umfassende Änderungen am System möglich. <br>Dadurch kann die Funktionsfähigkeit des Systems beeinträchtigt werden. <br>Für eventuelle Probleme sind Sie selbst verantwortlich! 
</div>

标签: phphtmlcurl

解决方案


要访问一个 url,然后发送数据(例如用户名/密码)并在您的客户端计算机上保存 cookie:

curl -c c:\Users\<your-name>\Desktop\cookie.txt -F "login=<your username>" -F "pwd=<your-pwd>" http://192.168.96.68/home.php

读取 cookie 并发送到 url

curl -b c:\Users\<your-name>\Desktop\cookie.txt -L http://192.168.96.68/home.php

推荐阅读