PHP提交表单工作不正常
我在我的博客页面上有一个基本的登录表单,当我输入正确的用户名和密码时,它表明用户名+密码是正确的,但我没有被重定向到index1.php页面 这是我的密码:PHP提交表单工作不正常,php,Php,我在我的博客页面上有一个基本的登录表单,当我输入正确的用户名和密码时,它表明用户名+密码是正确的,但我没有被重定向到index1.php页面 这是我的密码: <?php require_once('header.php'); require_once('config.php'); $db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //check to see if form has bee
<?php
require_once('header.php');
require_once('config.php');
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
//check to see if form has been submitted
if (isset($_POST['submit'])) {
if ($_POST['username'] == 'bruce' && ($_POST['password'] == 'wayne')) {
echo 'Correct username and password.';
}
else {
echo 'Incorrect username or password. Please try again.';
}
$result = mysqli_query($db, $sql);
$numrows = mysqli_num_rows($result);
if($numrows == 1) {
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$_SESSION['USERNAME'] = $row['username'];
$_SESSION['USERID'] = $row['id'];
header("Location:"."index1.php");
}
else {
header("Location"."login.php?error=1");
}
}
else {
if(isset($_GET['error'])) {
echo "Incorrect login, please try again!";
}
}
?>
<form action="<?php echo $_SERVER['index1.php'] ?>" method="post">
<table id='login'>
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Login!"></td>
</tr>
</table>
</form>
在header()函数之后使用exit()
或die()
函数。否则,脚本将不会终止。下面是一个相关的例子:
首先,您需要使用session_start()
在php中使用会话之前,如果未使用,则$\u会话将成为普通数组,并且不再被记住。除非使用输出缓冲,否则无法在重定向之前进行回显。我把你的回音注释掉了
我一直用这个。您必须在重定向后调用exit
/**
* This is designed to redirect using a quick, easy function.
*/
function gfRedir($_NewPath){
header('Location: '.$_NewPath);
exit();
}
header("Location:index1.php");
exit();
你会像这样使用它:
<?php
require_once('header.php');
require_once('config.php');
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
//check to see if form has been submitted
if (isset($_POST['submit'])) {
if ($_POST['username'] == 'bruce' && ($_POST['password'] == 'wayne')) {
//you cannot echo before a redirect unless you are using output buffering.
//echo 'Correct username and password.';
}
else {
//you cannot echo before a redirect unless you are using output buffering.
//echo 'Incorrect username or password. Please try again.';
}
//where is this $sql at that you are passing into the below function?
$result = mysqli_query($db, $sql);
$numrows = mysqli_num_rows($result);
if($numrows == 1) {
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$_SESSION['USERNAME'] = $row['username'];
$_SESSION['USERID'] = $row['id'];
gfRedir('index2.php');
}
else {
gfRedir('login.php?error=1');
}
}
else {
if(isset($_GET['error'])) {
echo "Incorrect login, please try again!";
}
}
function gfRedir($_NewPath){
header('Location: '.$_NewPath);
exit();
}
?>
<form action="<?php echo $_SERVER['index1.php'] ?>" method="post">
<table id='login'>
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Login!"></td>
</tr>
</table>
</form>
请尝试将此语法用于重定向
/**
* This is designed to redirect using a quick, easy function.
*/
function gfRedir($_NewPath){
header('Location: '.$_NewPath);
exit();
}
header("Location:index1.php");
exit();
谢谢你哪里定义了$db
?你的$sql
语句在哪里?我看不到。你有没有收到错误,比如“headers ready sent”?@michaelhanon没有,我没有收到任何错误。我只是停留在我的登录。php@chris试试下面我的答案,它现在说密码或用户名不正确。不,这只是一个片段,你需要把它粘贴到你当前的代码中。