模擬登陸學校教務管理系統,獲取成績單!

界面以下:php

這個模擬是高仿真學校教務系統的,好處就是,能夠快速登錄系統直接獲取成績。html

避開了繁瑣的查詢步驟,和節約了查詢的時間,極大的提升了用戶的體驗。正則表達式

核心源代碼以下:cookie

 <?php

    //屏蔽notice信息
    error_reporting(E_ALL ^ E_NOTICE);  

    //設置文本頭信息
    header("Content-Type:text/html;charset=utf-8");

    if ( $_REQUEST['xh'] && $_REQUEST['psw']){    
        $get= new Index();
        $flag = $get->login();
        if ( false == $flag) exit('學號或密碼錯誤!');
        echo $get->getResults();
    }


class Index{

    private $_cookie;

    //驗證登陸操做
    public function login(){

        $url = 'http://202.197.224.134:8083/jwgl/logincheck.jsp';

        $post = 'identity=student&username='.$_REQUEST['xh'].'&password='.$_REQUEST['psw'];

        $stream = $this->_html($url, $post);

        //將$strean由gbk編碼轉換爲utf-8編碼
        $stream = mb_convert_encoding($stream, 'utf-8', 'gbk');

        $this->_cookie($stream); // 獲取cookie
        
        $url = 'http://202.197.224.134:8083/jwgl/index1.jsp';

        $html = $this->_html($url, $post.'&role=1');
        
        return strpos($stream, '頁面跳轉') !== false;
    }

    //查詢成績的方法
    public function getResults(){

        $url= 'http://202.197.224.134:8083/jwgl/cj/cj1_cjLiebiao.jsp?xq=null&xkjc='.$_REQUEST['xkjc'].'&type=null&xkdl2=1&xh='.$_REQUEST['xh'].'&bh=null';


        $stream = $this->_html($url);

        $stream = mb_convert_encoding($stream, 'utf-8', 'gbk');


        //搜索 $stream中全部匹配 '/<td>(.*?)<\/td>/is' 給定正則表達式 的匹配結果而且將它們以 flag 指定順序輸出到 $match 中. 
        preg_match_all('/<td>(.*?)<\/td>/is', $stream, $match);

         $len=count($match[1],1);


        echo "<table  align='center' width='80%' border='1' cellspacing='0'  bordercolor='#3399cc'  cellpadding='2'><tr style='color:#000000;font-size:15px' bgcolor='#3399cc'><td  colspan='8' align='center'>湘潭大學學號爲<<<font color='red' style='font-weight:bold;'>".$_REQUEST['xh']."</font>>>的成績單</td>";
        $j=0;
        for($i=1;$i<=($len+1)/7;$i++){
            
            echo "<tr>";
            for(;$j<=$len;$j++){    
                echo "<td>".$match[1][$j]."</td>";
                if(($j+1)%7==0){
                    $j=$j+1;
                    break;
                }
            }
            echo "</tr>";
        }
        echo "</table>";
    }
    
    private function _cookie($stream){
    
        preg_match_all("/Set-Cookie: (.*?);/is", $stream, $matches);        
        $this->_cookie = @implode(";", $matches[1]);
    }
    
    private function _html($url, $post = FALSE){
    
        ob_start();

        //初始化curl模塊
        $ch = curl_init($url); 

        //是否顯示頭信息
        curl_setopt($ch, CURLOPT_HEADER, true);

        //設置cURL容許執行的最長秒數
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

        if ( $post){

            //post方式提交
            curl_setopt($ch, CURLOPT_POST, true);

            //要提交的信息
              curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }

        //設置cookie信息,保存在指定的文件中
        curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);

        //執行curl
        curl_exec($ch);  //執行cURL抓取頁面內容 

        //關閉cURL資源,而且釋放系統資源 
        curl_close($ch);

        //此函數返回輸出緩衝區的內容
        $_str = ob_get_contents();

        //去除全部輸出緩衝區
        ob_end_clean();

        return $_str;
    }
}

    echo "<a href='a.html' style='margin-left:45%;'><input type='button' value='返回查成績首頁'></a>";
?>
相關文章
相關標籤/搜索