表单
<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