PHP是世界上最好的编程语言
由于微信第三方平台——微擎使用的语言是PHP,并且很多第三方集成平台都是使用PHP语言作为后台语言,所以我和PHP不得不接触
虽然很早以前就有听说过PHP的大名,可是因为专业课程上不涉及,自己懒没去研究,所以我和php一直都很陌生,上学期我们学习了另一个后台开发语言:Java Web,所以在知道要做一个成绩查询系统的时候,我首先想到的是用Java Web来写。
我在linux系统中安装了我最熟悉的Tomcat服务器,使用Java Web编写了成绩查询系统,因为有数据库文件,所以查询编写起来很容易,不过由于TomCat默认为8080端口,原来的80端口被nginx服务给占用了,微信对于非80端口网页又有限制,不能为了一个系统推翻之前搭建的其他平台,所以我决定重新研究PHP,用PHP来编写成绩查询系统。
微信静默授权登录,判断是否关注公众号
<?php session_start(); $code = $_GET['code']; $state = $_GET['state']; //换成自己的接口信息 $appid = 'APPID'; $appsecret = 'APPSECRET'; if (empty($code)){ echo "<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3Ewindow.location.href%3D'https%3A%2F%2Fopen.weixin.qq.com%2Fconnect%2Foauth2%2Fauthorize%3Fappid%3DAPPID%26redirect_uri%3DREDIRECT_URI%26response_type%3Dcode%26scope%3Dsnsapi_userinfo%26state%3DTEMP%23wechat_redirect'%3B%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />"; } $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'; $token = json_decode(file_get_contents($token_url)); if (isset($token->errcode)) { echo ' <h1>错误:</h1> '.$token->errcode; echo ' <h2>错误信息:</h2> '.$token->errmsg; exit; } $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token; //转成对象 $access_token = json_decode(file_get_contents($access_token_url)); if (isset($access_token->errcode)) { echo ' <h1>错误:</h1> '.$access_token->errcode; echo ' <h2>错误信息:</h2> '.$access_token->errmsg; exit; } $access_token_url_1 = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret; //转成对象 $access_token_1 = json_decode(file_get_contents($access_token_url_1)); if (isset($access_token_1->errcode)) { echo ' <h1>错误:</h1> '.$access_token_1->errcode; echo ' <h2>错误信息:</h2> '.$access_token_1->errmsg; exit; } $user_info_url_1='https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token_1->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'; //转成对象 $user_info_1 = json_decode(file_get_contents($user_info_url_1)); if (isset($user_info_1->errcode)) { echo ' <h1>错误:</h1> '.$user_info_1->errcode; echo ' <h2>错误信息:</h2> '.$user_info_1->errmsg; exit; } $subscribe=$user_info_1->subscribe; $_SESSION['subscribe']=$subscribe; if($subscribe!=1){?> <!--强制引导关注--> <body align="center" background="http://pbr76n0ea.bkt.clouddn.com/cjcxbg.jpg" style="background-position:center center"> <div style="width:500px;height:450px;margin:auto;margin-top:300px;background-color:rgba(255,255,255,0.8)"> <h1>请先关注公众号</h1> <img src="http://pbr76n0ea.bkt.clouddn.com/cmxf.jpg" alt="传媒先疯公众号二维码"> </div> </body> <?php } else{ echo "<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3Ewindow.location.href%3D'cjcx.php'%3B%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />"; } echo "<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3Ewindow.location.href%3D'cjcx.php'%3B%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />"; ?>
查询登录页面
<?php session_start(); $subscribe=$_SESSION['subscribe']; $info=$_SESSION['info']; $student_name=$_SESSION['student_name']; $student_id=$_SESSION['student_id']; if(!isset($studnet_name)){ $studnet_name="";} if(!isset($studnet_id)){ $studnet_id="";} /*if(!isset($info)){ $info="";} ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>成绩查询系统</title> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%20type%3D%22text%2Fcss%22%3E%0A%20%20%20%20%09*%7Bfont-family%3A%E5%BE%AE%E8%BD%AF%E9%9B%85%E9%BB%91%7D%0A%20%20%20%20%09span%7B%0A%20%20%20%20%09font-size%3A2em%3B%0A%20%20%20%20%09line-height%3A50px%7D%0A%20%20%20%20%09input%5Btype%3Dtext%5D%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A28px%3B%0A%20%20%20%20%20%20%20%20padding%3A%200%3B%0A%20%20%20%20%20%20%20%20width%3A%20200px%3B%0A%20%20%20%20%20%20%20%20height%3A50px%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20button%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A2em%3B%0A%20%20%20%20%20%20%20%20padding%3A%200%3B%0A%20%20%20%20%20%20%20%20width%3A%20150px%3B%0A%20%20%20%20%20%20%20%20height%3A50px%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<style>" title="<style>" /> </head> <body align="center" background="http://pbr76n0ea.bkt.clouddn.com/cjcxbg.jpg" style="background-position:center center"> <div style="width:500px;height:450px;margin:auto;margin-top:300px;background-color:rgba(255,255,255,0.8)"> <form action="checkcj.php" method="post"> <h1 style="font-size:3em"><span style="font-size:1em;color:red">N</span><span style="font-size:1em;color:blue">M</span><span style="font-size:1em;color:green">TV</span>成绩查询系统</h1> <div style="line-height=2em"> <span>请输入姓名:</span><input type="text" name="student_name" size="40" value="<?php echo $student_name?>"> <span>请输入学号:</span><input type="text" name="student_id" size="40" value="<?php echo $student_id?>"> </div> <button type="submit">查询</button>   <button type="reset">清空</button> </form> <span style="font-size:1em;color:blue;line-height:20px">更多大学生便捷工具 敬请关注微信公众号:<span style="font-size:1.2em;color:red">传媒先疯</span></span> <!--以下为是否关注公众号判断,没关注会跳转授权登录php,可以避免直接访问本页面,而跳过了关注验证--> <?php if($subscribe!=1){ echo "<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3Ewindow.location.href%3D'https%3A%2F%2Fopen.weixin.qq.com%2Fconnect%2Foauth2%2Fauthorize%3Fappid%3DAPPID%26redirect_uri%3DREDIRECT_URI%26response_type%3Dcode%26scope%3Dsnsapi_userinfo%26state%3DTEMP%23wechat_redirect'%3B%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />"; } ?> </div> </body> </html>
PHP查询数据库
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>成绩</title> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%20type%3D%22text%2Fcss%22%3E%0A%20%20%20%20th%7B%0A%20%20%20%20font-size%3A2.8em%3B%0A%20%20%20%20%7D%0A%20%20%20%20td%7B%0A%20%20%20%20font-size%3A2.5em%3B%0A%20%20%20%20%7D%0A%20%20%20%20button%20%7B%0A%20%20%20%20%20%20font-size%3A1em%3B%0A%20%20%20%20%20%20padding%3A%200%3B%0A%20%20%20%20%20%20width%3A%20350px%3B%0A%20%20%20%20%20%20height%3A100px%3B%0A%20%20%20%20%7D%0A%20%20%20%20%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<style>" title="<style>" /> </head> <body background="http://pbr76n0ea.bkt.clouddn.com/cjcxbg.jpg" style="background-position:center center"> <?php session_start(); $dbhostip="localhost:3306"; $username="USERNAME"; $userpassword="PASSWORD"; $dbdatabasename="DATABASENAME"; $_SESSION['info']=null; $student_name=$_POST['student_name']; $_SESSION['student_name']=$student_name; $student_id=$_POST['student_id']; $_SESSION['student_id']=$student_id; $con=mysql_connect($dbhostip,$username,$userpassword);// or die("Unable to connect to the MySQL!"); $db = mysql_select_db($dbdatabasename,$con); $qres=mysql_query("SELECT * FROM TABLE WHERE XM='{$student_name}' AND XH='{$student_id}'"); ?> <div style="width:90%;height:200px;background-color:rgba(255,255,255,0.8);margin-top:100px;margin-left:5%"> <img src="http://pbr76n0ea.bkt.clouddn.com/XXLOGO.gif" alt="学校logo" height="150px" width="100%"> </div> <table border="1" bordercolro="red" cellspacing="0" width="90%" style="background-color:rgba(255,255,255,0.8);padding:20px;margin-left:5%"> <tr> <th>科目</th> <th>课程类型</th> <th>总评成绩</th> </tr> <?php $datarow = mysql_num_rows($qres); for($i=0;$i<$datarow;$i++){ $sql_arr = mysql_fetch_assoc($qres); $KCMC = $sql_arr['KCMC']; $KCLX = $sql_arr['KCLX']; $ZPCJ1 = $sql_arr['ZPCJ1']; echo " <tr> <td>$KCMC</td> <td style='text-align:center'>$KCLX</td> <td style='text-align:center'>$ZPCJ1</td> </tr> "; } mysql_close($con); if($i==0){ $_SESSION['info']="姓名或密码错误"; echo "<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3Ewindow.location.href%3D'cjcx.php'%3B%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />"; exit; } ?> <tr> <td>姓名:<?php echo "$student_name" ?></td> <td colspan="2">学号:<?php echo "$student_id" ?></td> </tr> <tr> <td colspan="3">成绩最后更新时间为7月15日,成绩请以教务系统为准</td> </tr> <tr> <td colspan="3" align="center"><button onclick="window.location.href='setcj.php'">设置我的成绩</button></td> </tr> </table> </body> </html>
效果图
完了,学霸体质暴露无遗