tawan158
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    0215-網頁前端設計與開發運用培訓班 === ###### tags: `德鍵` --- 一、修改指令執行程式:把user.php 視為後台需要權限才能進入 --- 1. reg、logout、login 移至 index.php 2. 函式也需要移動 3. 樣板的指令按鈕也要需修改 4. op=login 在 login_form 送出,故該表單的action 需改至 index.php 5. op=logout 是按鈕送出 ![](https://i.imgur.com/5iikgIr.png) 6. op=reg 在 reg_form 送出,故該表單的action 需改至 index.php 7. 在 user.php 加上權限 ```php #權限檢查 if(!$_SESSION['admin'])redirect_header("index.php", '您沒有權限', 3000); ``` 8. 二、查詢 --- 1. 指令:user.php?op=op_list 2. 在user.php建立一個 switch -> case "op_list" 因為查詢一般都設在 default,故op可省略 3. case ```php default: $op = "op_list"; op_list(); break; ``` 4. 函式 ```php function op_list(){ global $smarty; } ``` 5. Mysql 查詢語法 參考:https://www.mysql.tw/2015/05/select-sql.html ``` SELECT * FROM `users` LIMIT 50 ``` A. 讀取資料庫的內容,一律用 select 語法: ```htmlmixed= SELECT `查詢的欄位` [FROM `資料表名稱` 附加的篩選條件] ``` B. 其中篩選條件語法如下(有順序關係,需注意): ```htmlmixed= [where 篩選條件] [group by `欄位名稱`][having group的篩選條件] [order by {unsigned_integer | `欄位名稱` | formula} [asc | desc] ,...] [limit [起點,] 筆數] ``` 6. PHP從資料庫取得資料的方法 A. 寫SQL送去資料庫執行後,會傳回一個資源變數物件,如\$result B. 可以利用 \$result 的各種取得資料方法,將資料一筆一筆取回。 C. 一筆資料以上的資料,請放至 while(){} 迴圈中取回。 D. 利用 \$result->fetch_assoc() 取出的資料陣列,會以資料表欄位名稱為陣列索引; E. 以 \$result->fetch_row() 取出的資料陣列,是以欄位順序為陣列索引,通常搭配list()使用 F. \$row=\$result->fetch_assoc(); 得到的結果為: ```php Array ( [uid] => 1 [uname] => ugm [pass] => $2y$10$Eq697ikeRvikozYwwAeHJePFG4HlYovmYMYh0n3pWJ3StDY5Zx0KS [name] => 郭俊良 [tel] => 0921xxxxx [email] => tawanxxx@gmail.com [kind] => 0 [token] => $2y$10$cWXryHqslAduXsSTKveKO.eBgmEjziVBAE0tKCg9tNADYhY9zXAsO ) ``` G. \$row=\$result->fetch_row(); 得到的結果為: ```php Array ( [0] => 1 [1] => ugm [2] => $2y$10$Eq697ikeRvikozYwwAeHJePFG4HlYovmYMYh0n3pWJ3StDY5Zx0KS [3] => 郭俊良 [4] => 09215560421 [5] => tawan158@gmail.com [6] => 0 [7] => $2y$10$cWXryHqslAduXsSTKveKO.eBgmEjziVBAE0tKCg9tNADYhY9zXAsO ) ``` 故$result->fetch_row()會用list ```php list($uid, $uname, $pass, $name, $tel, $email, $kind, $token)=$result->fetch_row(); ``` 上課: ```php function op_list(){ global $smarty,$db; $sql = "SELECT * FROM `users` "; $result = $db->query($sql) or die($db->error() . $sql); while($row = $result->fetch_assoc()){ //Array ( [uid] => 2 [uname] => 1111 [pass] => $2y$10$q0HZPPfgltzI4sIujmZXheLwxzrf3DB2jjRbqo6PnjLko1f2ltFg. [name] => 1 [tel] => 1 [email] => adsfasdf@1111 [kind] => 0 [token] => ) print_r($row);die(); } //$result->fetch_assoc() } ``` 三、 迴圈用法 --- 1. 不曉得迴圈數的 ```php while(條件為真){ //執行動作 } ``` 2. 已知道迴圈數 ```php for($i=0;$i<迴圈數;$i++){ //執行動作 } ``` 3. 抽取陣列 ```php foreach($陣列 as $索引=>$值){ //執行動作 } ``` 四、資料過濾 1. PHP的變數簡單分可分為在頁面中自己定義的變數,以及外面傳來的變數。 1. 凡是外面傳來的變數,一律要進行變數過濾。 1. 文字部份, htmlspecialchars(文字) 函數會將HTML做轉義,讓標籤失效,例如<轉成 \&lt;,預設只轉雙引號(單引號無效),用 htmlspecialchars(文字,ENT_QUOTES) 更好。 1. HTML Entity Name 字符實體列表 :https://www.w3schools.com/html/html_entities.asp 1. htmlentities() 函數也可轉義,但並不適用中文,因為會連同中文字一起轉義。 1. 用 htmlentities() 和 htmlspecialchars() 只能防止XSS攻擊(避免被值入javascript),不能防止SQL隱碼攻擊。 1. 用filter_var(\$變數)亦可,會直接把HTML語法直接刪除。 1. 數字的過濾,只要加個(int)即可,如\$sn=(int)\$_POST['sn']; 1. 不同過濾器的運用時機:要在php檔案中運算用的,請用PHP來過濾,要寫入資料庫的,用資料庫的real_escape_string()來過濾。 上課資料 ```php function op_list(){ global $smarty,$db; $sql = "SELECT * FROM `users` "; $result = $db->query($sql) or die($db->error() . $sql); $rows=[];//array(); while($row = $result->fetch_assoc()){ $row['uname'] = htmlspecialchars($row['uname']);//字串 $row['uid'] = (int)$row['uid'];//整數 $row['kind'] = (int)$row['kind'];//整數 $row['name'] = htmlspecialchars($row['name']);//字串 $row['tel'] = htmlspecialchars($row['tel']);//字串 $row['email'] = htmlspecialchars($row['email']);//字串 $rows[] = $row; } $smarty->assign("rows",$rows); } ``` 五、 Smarty迴圈用法 1. smarty3 ```php <{foreach $來源 as $別名}> <{$別名.索引}> <{foreachelse}> 該變數沒有值時要出現的內容 <{/foreach}> ``` 2. smarty2 亦相容於 smarty3 ```php <{foreach from=$來源 key=索引 item=項目 name=別名}> <{$項目.索引}> <{foreachelse}> 該變數沒有值時要出現的內容 <{/foreach}> ``` 六、萊爾富咖啡 ![](https://i.imgur.com/MDt9dwk.png) https://forms.gle/wpzYoNetETkScxxAA

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully