PHP 入門指南 - encryptor_function.php




encryptor_function.php 的程式原始碼如下

<?php
include "encryptor.php";

$e = new Encrypt();
$data = "code.txt";
$link = @mysql_connect("localhost", "encryptdata", "0000") or die("mysql worng - ".mysql_error());

function make_page($main, $display) {
    $page = "
    <html>
    <head>
    <title>Encode Software</title>
    <link rel='stylesheet' href='encryptor.css'> 
    </head>
    <body>
    <div class='menu'>
    <a href='{$_SERVER['PHP_SELF']}'>Home</a> - 
    <a href='{$_SERVER['PHP_SELF']}?op=record'>Record</a> -
    <a href='{$_SERVER['PHP_SELF']}?op=about'>About</a>
    </div>
    <div class='main'>
    $main
    </div>
    <div class='display'>
    $display
    </div>
    </body>
    </html>";
    
    return $page;
}

function input_form() {
    $main = "
    <form action='{$_SERVER['PHP_SELF']}' method='post'>
    <input type='text' name='input' size='67'><br>
    <input type='submit' value='New' name='op'>
    <input type='submit' value='Load' name='op'>
    <input type='submit' value='Encode' name='op'>
    <input type='submit' value='Decode' name='op'>
    <input type='submit' value='Clean' name='op'>
    </form>";
    
    return $main;
}

function new_button() {
    global $e, $data, $link;
    $f = fopen($data, "w+");
    $s = join($e->cArray, "");
    fwrite($f, $s);
    fclose($f);
 
    $sql = "insert into resord (action, code, input, output) values('new', '{$s}', '', '')";
    mysql_db_query("encrypt_software", $sql, $link) or die("mysql wrong").mysql_error();
 
    return "new: ".$s;
}

function load_button() {
    global $e, $data, $link;
    $f = fopen($data, "r");
    if (filesize($data) == 0) {
        fclose($f);
        return "load denied";
    }
    else {
        $s = fread($f, filesize($data));
        $e->setcArray($s);
        fclose($f);
  
        $sql = "insert into resord (action, code, input, output) values('load', '{$s}', '', '')";
        mysql_db_query("encrypt_software", $sql, $link) or die("mysql wrong").mysql_error();
        
  return "load: ".$s;
    }
}

function encode_button($input) {
    global $e, $data, $link;
    $f = fopen($data, "r");
    if (filesize($data) == 0) {
        fclose($f);
        return "encode denied";
    }
    else {
        $s = fread($f, filesize($data));
        $e->setcArray($s);
  $output = $e->toEncode($input); 
        fclose($f);
  
        $sql = "insert into resord (action, code, input, output) values('encode', '{$s}', '{$input}', '{$output}')";
        mysql_db_query("encrypt_software", $sql, $link) or die("mysql wrong").mysql_error();
  
        return "encode: ".$output;
    }
}

function decode_button($input) {
    global $e, $data, $link;
    $f = fopen($data, "r");
    if (filesize($data) == 0) {
        fclose($f);
        return "decode denied";
    }
    else {
        $s = fread($f, filesize($data));
        $e->setcArray($s);
  $output = $e->toDecode($input);
        fclose($f);
  
        $sql = "insert into resord (action, code, input, output) values('decode', '{$s}', '{$input}', '{$output}')";
        mysql_db_query("encrypt_software", $sql, $link) or die("mysql wrong").mysql_error();
  
        return "decode: ".$output;
    }
}

function clean_button() {
    global $e, $data, $link;
    $f = fopen($data, "w+");
    fwrite($f, "");
    fclose($f);
 
    $sql = "insert into resord (action, code, input, output) values('clean', '', '', '')";
    mysql_db_query("encrypt_software", $sql, $link) or die("mysql wrong").mysql_error();
 
    return "Encrypt object is clean out.";
}

function record() {
    global $link;
    $sql = "select * from resord";
    $result = mysql_db_query("encrypt_software", $sql, $link) or die("mysql wrong").mysql_error();
    $temp = "<h3>Record:</h3><br / >";
    $temp = $temp."sn-action-input-output<br / >";
    while ($data = mysql_fetch_array($result)) {
        $temp = $temp.$data['sn']."-".$data['action']."-".$data['input']."-".$data['output']."<br / >";
    }
    return $temp;
}

function about() {
    $temp = "《程式語言教學誌》的範例程式<br / >
    <a href='http://pydoing.blogspot.com/'>http://pydoing.blogspot.com/</a><br / >
    名稱:Encrypt Software<br / >
    功能:示範 PHP 程式<br / > 
    作者:張凱慶<br / >
    時間:西元 2012 年 11 月 */";
    return $temp;
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:encryptor_function.php
    功能:示範 PHP 程式 
    作者:張凱慶
    時間:西元 2012 年 11 月 */
?>


您可以繼續參考
範例程式碼


相關目錄
回 PHP 入門指南
回 PHP 教材
回首頁


參考資料
http://www.php.net/
http://www.php.net/manual/en/getting-started.php
http://www.w3schools.com/php/default.asp
http://www.apache.org/
http://www.mysql.com/
http://dev.mysql.com/doc/index.html
http://www.appservnetwork.com/

3 則留言:

超冏 提到...

非常感謝你的範例!
對我學習幫助很大

不過這裡好像有個小錯誤
應該把第十三行<... href='test.css'>
中的test.css 改成 encryptor.css

Kaiching Chang 提到...

已修改,感謝指正 :)

Ling Pang 提到...

首先感謝你的教學,非常好的一步一步學習

這裏發現有些少錯誤出現
於第 55行開始連結 mysql 的 table "resord" 是小階
但你於建立數據庫的 table 時是用大階
最初發現執行 encryptor04.php 出現連結數據庫出錯
把這個改為大階後就正常了。