PHP登录表单将用户引导到新页面

时间:2018-03-25 04:57:51

标签: php html if-statement input xhtml

我在HTML上创建了一个登录表单,要求输入特定的用户名+密码。 (User = hello,pass = bye)

我希望我的PHP表单检查用户输入是否正确(Hello& Bye),如果是,请将用户定向到名为" next.html &#的页面34;

这是我的 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>
&#13;
&#13;
&#13;

我的 login.php

&#13;
&#13;
<?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;


    
}

?>
&#13;
&#13;
&#13;

和我的 next.html 页面,如果用户+密码正确,我希望用户访问该页面:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

使用标题(&#34;位置:http://www.example.com/&#34;);重定向用户。将您的代码更改为:

<?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;

}

?>

答案 1 :(得分:0)

login.php 中添加以上内容;

if( Login() ) {
     header("Location: next.html"); exit();
}

如果您只想使用该登录信息,请替换:

if(!$this->CheckLoginInDB($username,$password))

。通过

if( $username == "hello"  && $password == "bye" )

答案 2 :(得分:0)

你可以试试这个:

<?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;

    }
}

?>