php學習筆記-1.登入頁面的學習
阿新 • • 發佈:2019-01-10
使用者通過form表單,使用post方法,將賬號和密碼提交到伺服器,伺服器驗證賬號和密碼,若不對,彈出提示框要求重新輸入,若正確,頁面跳轉到主框架頁面
1、程式碼片段——form表單
2、程式碼片段——php獲取form提交資料,呼叫userAgreed方法驗證使用者輸入資訊<form class="form-signin" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <h2 class="form-signin-heading">Please sign in</h2> <label for="inputEmail" class="sr-only">Email address</label> <input name="personal[email]" type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> <label for="inputPassword" class="sr-only">Password</label> <input name="personal[password]" type="password" id="inputPassword" class="form-control" placeholder="Password" required> <div class="checkbox"> <label> <input type="checkbox" value="remember-me"> Remember me </label> </div> <input type="hidden" name="action" value="submitted" /> <button class="btn btn-lg btn-primary btn-block" type="submit" name="submit" >Sign in</button> </form>
<?php if (isset($_POST['action']) && $_POST['action'] == 'submitted') { echo '<pre>'; print_r($_POST); echo $_POST['personal']['email']; echo '<br />'; echo $_POST['personal']['password']; if(userAgreed($_POST['personal']['email'], $_POST['personal']['password'])){ echo "Login success"; }else{ echo "Login failed"; } echo '<a href="'. $_SERVER['PHP_SELF'] .'">返回重填</a>'; echo '</pre>'; } else { ?>
3、程式碼片段——thinkPHP框架中common.php檔案中的userAgreed方法
function userAgreed($username, $password){ $db = M('user'); $condition['email'] = $username; $condition['password'] = $password; $result = $db->where($condition)->find(); dump($result); if(isset($result)){ return true; }else{ return false; } }