PHP代码无法正常工作 - 如果并重定向到页面

时间:2014-01-11 15:18:59

标签: php

我是php的新手,我写了这段代码:

<?php

$usernametest="Testing";
$passwordtest="TestingPass";

if (isset($_POST['submit']))
{
    if ((isset($_POST['username']) == $usernametest ) && (isset($_POST['password']) == $passwordtest ))
    { include ('templates/main.php');
    }
    else
    {
        echo "please enter the correct username and password combination";
    }
    exit();
}
?>

我制作了2个文本框和一个提交按钮,如果用户名等于Testing并且密码等于TestingPass,我希望用户被定向到另一个页面,如果用户没有键入正确的组合,我希望网站说出来用户名和密码不正确。另外,我应该在哪里准确地粘贴这段代码?文本框代码上方?

4 个答案:

答案 0 :(得分:2)

条件检查和重定向时出错:

<?php

    $usernametest="Testing";
    $passwordtest="TestingPass";

    if (isset($_POST['submit']))
    {
        if ((isset($_POST['username']) && $_POST['username'] == $usernametest ) && (isset($_POST['password']) && $_POST['password'] == $passwordtest ))
        { 
           header('location: templates/main.php');
        }
        else
        {
            echo "please enter the correct username and password combination";
        }
        exit();
    }
    ?>

答案 1 :(得分:0)

您想使用header()。因此,您应该没有包含该条件,因为标题已经发送。

<?php

$usernametest="Testing";
$passwordtest="TestingPass";

if (isset($_POST['submit']))
{
if ($_POST['username'] == $usernametest && $_POST['password'] == $passwordtest)
{

header("Location: MY_PAGE.php");
}
else
{
echo "please enter the correct username and password combination";
}
exit();
}
?>

答案 2 :(得分:0)

Isset()函数检查变量是否存在并返回布尔值。你必须检查这样的平等:

if ($_POST['username'] == $usernametest && $_POST['password'] == $passwordtest)

答案 3 :(得分:0)

始终将值存储在变量中以使代码更易于理解

<?php

$ usernametest = “测试”; $ passwordtest = “TestingPass”;

if(isset($ _ POST ['submit'])) {

   $username    =   $_POST['username'];
   $password    =   $_POST['password'];

if ($username == $usernametest && $password == $passwordtest ))
{       header("location:templates/main.php"); 
}
else
{
    echo "please enter the correct username and password combination"; exit();
}

} ?&GT;