我是一名java程序员,也是Php的新手。我正在尝试在Php中设计一个登录表单,我从数据库中检查用户ID和密码。如果读取的数据是正确的,我想将用户重定向到welcome.php页面。我的代码是:
<?php
ob_start();
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);
$sql="SELECT rid FROM register WHERE rname='$myusername' and rpass='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
@$active=$row['active'];
$count=mysql_num_rows($result);
if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
flush();
header('Location: http://localhost/login/welcome.php');
die('should have redirected by now');
}
else
{
$error="Your Login Name or Password is invalid";
}
}
?>
但在提交具有正确ID和密码的表单后,我收到错误,如下所示:
我尝试了许多解决方案,例如删除空格,使用ob_start()等但是徒劳无功。
答案 0 :(得分:3)
您应该使用以下方法在会话中注册变量:
$_SESSION['myusername'] = $myusername;
请勿使用session_register("myusername");
,因为它不再受支持
答案 1 :(得分:0)
你真的需要你的 flush()吗?也许尝试用
来拥抱你的标题die(header('Location: ...'));
答案 2 :(得分:0)
我使用简单的解决方案。
在您的情况下,您有一些标题发送,而不是您无法修改。
简单的解决方案是发送一些脚本代码;
echo "<script language=\"javascript\">
window.location.assign('http://localhost/login/welcome.php')</script>";
度过愉快的一天。
弗朗索瓦
答案 3 :(得分:0)
您可能想要检查login.php和config.php之间的字符集是否有任何区别。在执行包含时,我遇到了charset的差异导致奇怪输出的情况。