我在提交表单后看了很多回复重定向到另一个页面,但到目前为止还没能让它工作,可能是因为我不知道在哪里实际放置代码。有人可以帮忙吗?其余的代码工作正常,我只需要知道在哪里放置header():
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
//connects to database, checks username & password against database to see is user exists
if($username && $password)
{
include ("connect.php");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if($numrows !==0)
{
while($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
//if username and password are correct
if($username==$dbusername&&md5($password)==$dbpassword)
{
echo "You are logged in. <a href='main.php'>Continue to site.</a>";
$_SESSION['username'] = $username;
}
//if password is incorrect
else
echo "Your password is incorrect.";
}
//if username is incorrect
else
die("Username does not exist.");
}
//if no information is submitted
else
die("Please enter your login details.");
//prevents errors from displaying on page
error_reporting(0);
?>
我还需要知道这个页面的位置:
<?php
//Check if register button was pressed
$button = $_POST['button'];
//if button was pressed,
if ($button)
{
//get data from form,
$username = $_POST['username'];
$password = $_POST['password'];
$retype_password = $_POST['retype_password'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
}
//check if all information has been entered,
if ($username && $password && $retype_password && $first_name && $last_name)
{
//check if password and retype_password are the same
if($password==$retype_password)
{
//check if username already exists
include("connect.php");
$query = mysql_query("SELECT * FROM users WHERE username = '$username'");
$numrows = mysql_num_rows($query);
if($numrows == 0)
{
//encrypt password
$password = md5($password);
//sends data from form to database - creates new user
$register = mysql_query("INSERT INTO users VALUES ('', '$username', '$password', '$first_name', '$last_name')");
echo "You are now registered. <a href='main.php'>Continue to site.</a>";
}
else
echo "Username is unavailable.";
}
else
echo "Password did not match.";
}
//prevents errors from displaying on page
error_reporting(0);
?>
提前致谢!
答案 0 :(得分:0)
if($username==$dbusername&&md5($password)==$dbpassword)
{
$_SESSION['username'] = $username;
header( 'Location: http://www.yoursite.com/new_page.html' ) ;
}
答案 1 :(得分:0)
一旦完成工作,你应该把它放在:
之后//echo "You are logged in. <a href='main.php'>Continue to site.</a>";
$_SESSION['username'] = $username;
header('Location: your url');
exit;
不要忘记“退出”或将执行以下操作。
也就是说,在重定向之前你不能回应一些东西,这是合乎逻辑的,因为无法看到回声。
所以,要么你不回应:
$_SESSION['username'] = $username;
header('Location: your url');
exit;
或者您执行HTML(或javascript)重定向,延迟时间为5秒:
echo "You are logged in. <a href='main.php'>Continue to site.</a>";
$_SESSION['username'] = $username;
exit;
在这种情况下你必须把它放在&lt;头&gt;部分,进行HTML重定向:
<meta http-equiv="refresh" content="0; url=http://example.com/main.php" />
另外
error_reporting(0);
应该放在页面的开头,除非你想要显示以前行的错误。
但是:error_reporting(0);不应该在开发站点上使用(并且始终在生产站点上)。
您应该打开display_errors('on')和error_reporting(E_ALL)来查看错误 - 错误对开发人员非常有用。