PHP Login Page Redirect

跟風遠走 提交于 2019-12-24 12:03:05

问题


this code is incomplete and I need it to redirect to a page called mainpage.php with hardcoded information for the username and password. Here's the code.

<?php
session_start();
$username="user";
$password="123";

if($_POST['username'] == $username && $_POST['password'] == $password)
?>

<html>
    <head>
    </head>
    <body>
        <div style='text-align:center'>
        <h3>Welcome To Kontak</h3>
        </div>
        <hr /><br />
        <form id='login' action="" method='post' accept-charset='UTF-8'>
            <fieldset style="width:550px">
            <legend>Admin 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" />


            <label for='password' >Password:</label>
            <input type='password' name='password' id='password' maxlength="50" />

            <input type='submit' name='submit' value='Submit' />
            </fieldset>
        </form>
    </body>
</html>

回答1:


I think you are looking for this:

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

header( 'Location: mainpage.php' );
?>

so the header will take to mainpage.php when if condition is satisfied. --Vijay




回答2:


Have to start and end the if block

<?php
session_start();
$username="user";
$password="123";

if(isset($_POST['username']) && $_POST['username'] == $username && $_POST['password'] == $password)
{
    header("Location: your_page.php");
}
else
{
?>

<html>
    <head>
    </head>
    <body>
        <div style='text-align:center'>
        <h3>Welcome To Kontak</h3>
        </div>
        <hr /><br />
        <form id='login' action="" method='post' accept-charset='UTF-8'>
            <fieldset style="width:550px">
            <legend>Admin 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" />


            <label for='password' >Password:</label>
            <input type='password' name='password' id='password' maxlength="50" />

            <input type='submit' name='submit' value='Submit' />
            </fieldset>
        </form>
    </body>
</html>
<?php
}
?>



回答3:


Change 'header("Location: your_page.php");' to 'header("Location: mainpage.php");'



来源:https://stackoverflow.com/questions/19782194/php-login-page-redirect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!