• 【原】实验室签到PHP版本


    表单

    <html>
    <body>
    <h1>实验室自动签到测试</h1>
    <h2>输入学号和登录密码(建议自己改过密码后再来录入您的数据)</h2>
    <form action="mydb.php" method="post">
    学号: <input type="text" name="studentid" />
    登录密码: <input type="text" name="pw" />
    <input type="submit" value="OK" />
    </form>
    
    </body>
    </html>

    数据库mydb.php

    <html>
    <body>
    <?php 
    $host = "localhost";
    $user = "test";
    $pass = "123456";
    $db = "mydb";
    function execute_query($query) {
    
    	$r = mysql_query($query);
    
    	if (!$r) {
    		echo "Cannot execute query: $query
    ";
    		trigger_error(mysql_error());
    	} else {
    	echo "Query: $query executed
    ";
    	}
    	}
    
    	@$r = mysql_connect($host, $user, $pass);
    
    	if (!$r) {
    	echo "Could not connect to server
    ";
        trigger_error(mysql_error(), E_USER_ERROR);
    	} else {
    	//echo "Connection established
    ";
    	}
    
    $r2 = mysql_select_db($db);
    	if (!$r2) {
    	echo "Cannot select database
    ";
    	trigger_error(mysql_error(), E_USER_ERROR);
    	} else {
    	//echo "Database selected
    ";
    	}
    	$query = sprintf("INSERT INTO lab VALUES('%s','%s')",mysql_real_escape_string($_POST["studentid"]),$_POST["pw"]);
        //echo $query;
        @execute_query($query);
    mysql_close();
    
    ?>
    </body>
    </html>
    

    执行签到签离 loginandout.php

    <?php 
    //签到签离的主要函数$qingdao=1的时候执行签到,为0时执行签离
    function lablogin( $user, $password ,$qingdao=1)
    {
        if ( empty( $user ) || empty( $password ) )
        {
            return 0;
        }
        
    //     登录
        $ch = curl_init();
        define( "COOKIEJAR", tempnam( "/tmp", "cookie" ) );
        $url = 'http://www.buptcnc.cn/login/login';
        $post_data = 'user.VUserId='.$user.'&user.VUserPassword='.$password.'&x=24&y=16';
        curl_setopt( $ch, CURLOPT_REFERER, "http://www.buptcnc.cn/login/login" );
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 500 );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        define( "USERAGENT", $_SERVER['HTTP_USER_AGENT'] );
        curl_setopt($ch, CURLOPT_POST, 1);
        
        $output = curl_exec($ch);
        curl_close($ch);
        //echo  $output;
    //     签到
        $urlqingdao='http://10.104.5.63/LabManage/index/addattendance?vStuId='.$user;
        $curl2 = curl_init();
        curl_setopt($curl2, CURLOPT_URL,$urlqingdao);
        curl_setopt($curl2, CURLOPT_HEADER, false);
        curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl2, CURLOPT_COOKIEFILE, COOKIEJAR);
    //     $content = curl_exec($curl2);
        
        
        
    //     签离
        $urlqingli='http://10.104.5.63/LabManage/index/addattendanceleave.action?vStuId='.$user;
        $curl3 = curl_init();
        curl_setopt($curl3, CURLOPT_URL,$urlqingli);
        curl_setopt($curl3, CURLOPT_HEADER, false);
        curl_setopt($curl3, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl3, CURLOPT_COOKIEFILE, COOKIEJAR);
    //     $content = curl_exec($curl3);
    
    if ($qingdao) {
        $content = curl_exec($curl2);//$qingdao为真时执行签到,为假时执行签离
    }else{
        $content = curl_exec($curl3);
    }
        
    }
    $host = "localhost";
    $user = "test";
    $pass = "123456";
    $db = "mydb";
    
    $r = mysql_connect($host, $user, $pass);
    
    if (!$r) {
        echo "Could not connect to server
    ";
        trigger_error(mysql_error(), E_USER_ERROR);
    } else {
        echo "Connection established
    ";
    }
    
    $r2 = mysql_select_db($db);
    
    if (!$r2) {
        echo "Cannot select database
    ";
        trigger_error(mysql_error(), E_USER_ERROR);
    } else {
        echo "Database selected
    ";
    }
    
    $query = "SELECT * FROM lab";
    
    $rs = mysql_query($query);
    
    if (!$rs) {
        echo "Could not execute query: $query";
        trigger_error(mysql_error(), E_USER_ERROR);
    } else {
        echo "Query: $query executed
    ";
    }
    
    while ($row = mysql_fetch_assoc($rs)) {
    //     echo $row['Id'] . " " . $row['Name'] . " " . $row['Price'] . "
    ";
        $hour=date("H");
        if (in_array($hour,array(8,9,13,14))) {
    //         echo $row['stdId']."开始签到".$row['pw'];
    
            echo $row['stdId']."开始签到"."
    ";
            lablogin($row['stdId'],$row['pw'],'1');//执行签到
            
        }else {
    //         echo $row['stdId']."开始签离".$row['pw'];
    
            echo $row['stdId']."开始签离"."
    ";
            lablogin($row['stdId'],$row['pw'],'0');//执行签离
            
        }
        
    }
    mysql_close();

    linux crontab:

    35 8 * * 1-5   /usr/bin/php  /var/www/loginandout.php35 11 * * 1-5  /usr/bin/php  /var/www/loginandout.php35 13 * * 1-5  /usr/bin/php  /var/www/loginandout.php
    35 17 * * 1-5  /usr/bin/php  /var/www/loginandout.php
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    python面试的100题(2)
    面试题目和地址
    python面试的100题(1)
    no module named系列问题解决
    ubuntu16.04无法打开终端
    Reinforcement Learning,微信公众号:DRL学习
    Java中的I/O操作File
    Java中的Date时间转换【SimpleDateFormat (parse和format)】和Calendar日历表
    重写equals方法
    Java中栈,堆,常量池的简单理解
  • 原文地址:https://www.cnblogs.com/yuliyang/p/3616286.html
Copyright © 2020-2023  润新知