问题
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