1、登录页面html

<!DOCTYPE HTML>
<html class="no-js">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="renderer" content="webkit">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>登录</title>
        <meta name="robots" content="noindex, nofollow">
<link rel="stylesheet" href="/static/css/style.css?v=17.10.30">
  </head>
    <body class="body-100">

<div class="typecho-login-wrap">
    <div class="typecho-login">
        <form action="" method="post" name="login" role="form">
            <p>
                <label for="name" class="sr-only">用户名</label>
                <input type="text" id="name" name="username" value="" placeholder="用户名" class="text-l w-100" autofocus />
            </p>
            <p>
                <label for="password" class="sr-only">密码</label>
                <input type="password" id="password" name="password" class="text-l w-100" placeholder="密码" />
            </p>
            <p class="submit">
                <button type="submit" class="btn btn-l w-100 primary">登录</button>
            </p>
        </form>        
    </div>
</div>
    </body>
</html>

2、处理网站登录以及获取http post数据,验证密码功能

package main

import (
    "fmt"
    "html/template"
    "log"
    "net/http"
    "strings"
    "time"
)

type Item struct {
    Name  string
    Price int
}

func logouthanler(w http.ResponseWriter, r *http.Request) {
    expiredTime := time.Now()
    c1 := &http.Cookie{
        Name:     "username",
        Expires: expiredTime,
        MaxAge:   -1,
        HttpOnly: true,
    }

    http.SetCookie(w, c1)
}

// 登陆页面
func loginhanler(w http.ResponseWriter, r *http.Request) {

    // 如果是post请求,注意先后顺序
    if r.Method == http.MethodPost {
        // 解析表单 并且校验
        r.ParseForm()
        for k, v := range r.Form {
            fmt.Println("key:", k)
            fmt.Println("val:", strings.Join(v, "-"))
        }
        userName := r.FormValue("username")
        pwd := r.FormValue("password")
        fmt.Println("userName:", userName)
        fmt.Println("pwd:", pwd)

        // 如果输入正确,则发送cookie 并给出反馈
        if userName == "const" && pwd == "const" {

            c1 := &http.Cookie{
                Name:     "username",
                Value:    userName,
                MaxAge:   0,
                HttpOnly: true,
            }

            http.SetCookie(w, c1)

            //fmt.Fprint(w, "登陆成功")
            http.Redirect(w, r, "/manage", http.StatusFound)
        } else {
            fmt.Fprint(w, "登陆失败")
        }

        // 显示登录页面
    } else {
        // 解析模板
        t, err := template.ParseFiles("index.html")
        if err != nil {
            log.Fatal(err)
        }
           t.Execute(w, "")
    }    
}   
func manageHandler(w http.ResponseWriter, r *http.Request) {
    username, err := r.Cookie("username")
    if err != nil || username.Value == "" {
        http.Redirect(w, r, "/", http.StatusFound)
    }

    fmt.Println("enter manage1")
    t, err := template.ParseFiles("manage.html")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("enter manage2")
    t.Execute(w, username.Value)
}

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", loginhanler)
    mux.HandleFunc("/login", loginhanler)
    mux.HandleFunc("/manage", manageHandler)
    mux.HandleFunc("/logout", logouthanler)
    fs := http.FileServer(http.Dir("."))
    mux.Handle("/static/", http.StripPrefix("/static/", fs))

    server := &http.Server{
        Addr:    ":8080",
        Handler: mux,
    }

    err := server.ListenAndServe()
    if err != nil {
        log.Fatal(err)
    }

}

3、登录效果
2021-06-14_201101.png

2021-06-14_201143.png

本文链接地址:https://const.net.cn/35.html

标签: none

添加新评论