[英]PHP login form to direct user onto new page
我在HTML上創建了一個登錄表單,要求輸入特定的用戶名+密碼。 (用戶=你好,通過=再見)
我希望我的PHP表單檢查用戶輸入是否正確(Hello&Bye),如果正確,請將用戶定向到名為“ next.html ”的頁面
這是我的login.html表單
<html> <h1> <font color="red">Log In To Add Entry!</font></h1> <form name="myForm" action="login.php" method="post"> <fieldset > <legend>Login</legend> <input type='hidden' name='submitted' id='submitted' value='1'/> <label for='username' >UserName*:</label> <input type='text' name='username' id='username' maxlength="50" /> <br> <label for='password' >Password*:</label> <input type='password' name='password' id='password' maxlength="50" /> <input type='submit' name='Submit' value='Submit' /> </fieldset> </form> </html>
我的login.php :
<?php function Login() { if(empty($_POST['username'])) { $this->HandleError("UserName is empty!"); return false; } if(empty($_POST['password'])) { $this->HandleError("Password is empty!"); return false; } $username = trim($_POST['username']); $password = trim($_POST['password']); if(!$this->CheckLoginInDB($username,$password)) { return false; } session_start(); $_SESSION[$this->GetLoginSessionVar()] = $username; return true; } ?>
和我的next.html頁面,如果用戶名和密碼正確,我希望用戶進入該頁面:
<?xml version = "1.0" encoding = "utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <title>Entry page</title> </head> <body> <h1> <font color="red">Add an entry to Joe's Blog!</font></h1> </html>
使用標頭(“位置: http : //www.example.com/ ”); 重定向用戶。 將代碼更改為此:
<?php
function Login()
{
if(empty($_POST['username']))
{
$this->HandleError("UserName is empty!");
return false;
}
if(empty($_POST['password']))
{
$this->HandleError("Password is empty!");
return false;
}
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if(!$this->CheckLoginInDB($username,$password))
{
return false;
}
session_start();
$_SESSION[$this->GetLoginSessionVar()] = $username;
header("Location: next.html");
return true;
}
?>
在login.php中,只需在上面添加;
if( Login() ) {
header("Location: next.html"); exit();
}
如果您只想使用該登錄信息,請替換:
if(!$this->CheckLoginInDB($username,$password))
附:
if( $username == "hello" && $password == "bye" )
您可以嘗試以下方法:
<?php
function Login() {
if(empty($_POST['username']))
{
$this->HandleError("UserName is empty!");
return false;
}
if(empty($_POST['password']))
{
$this->HandleError("Password is empty!");
return false;
}
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if ('hello' == $username && 'bye' == $password) {
session_start();
$_SESSION[$this->GetLoginSessionVar()] = $username;
header("Location: next.html");
return true;
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.