header中()在PHP中不重定向 [英] header() in php is not redirecting
问题描述
我有一个有表格的登录页面。登录数据被发送到php脚本中以检查用户是否存在于数据库中。
><?php
$ host =localhost:8889; //主机名称
$ username =root; // Mysql用户名
$ password =root; // Mysql密码
$ db_name =TableName; //数据库名称
$ tbl_name =用户; //表名
//连接服务器并选择databse。
mysql_connect($ host,$ username,$ password)或die(无法连接);
mysql_select_db($ db_name)或死(无法选择DB);
//从表单发送的用户名和密码
$ myusername = $ _ POST ['myusername'];
$ mypassword = $ _ POST ['mypassword'];
//保护MySQL注入(关于MySQL注入的更多细节)
$ myusername = stripslashes($ myusername);
$ mypassword = stripslashes($ mypassword);
$ myusername = mysql_real_escape_string($ myusername);
$ mypassword = mysql_real_escape_string($ mypassword);
$ sql =SELECT * FROM $ tbl_name WHERE username ='$ myusername'and password ='$ mypassword';
$ result = mysql_query($ sql);
// Mysql_num_row正在计数表行
$ count = mysql_num_rows($ result);
//如果结果匹配$ myusername和$ mypassword,则表格行必须为1行
if($ count == 1){
// Register $ myusername,$ mypassword并重定向到文件
session_register(myusername);
session_register(mypassword);
header(location:../ main.php);
die();
}
else {
echo错误的用户名或密码;
}
?>
计数为1,但重定向不会发生。
我已阅读这个很好的答案并检查了以下内容:
- 在开始标记之前没有空白空间,也没有其他回显或打印。
- i尝试打开php标记后放置
ob_start
,但它也不起作用。
- i尝试打开php标记后放置
我不明白BOM的含义。你能帮助我吗?
我过去一直使用完全相同的代码,它的工作原理现在我在mamp和mac上不起作用。 / p>
谢谢。
编辑:
在修复这些事情并启用报告后,人们说了些什么,问题是:
调用未定义的sesion_register。
所以,我改变了它,因为它没有被定义,并且它被弃用,它现在起作用了!谢谢大家!
试试这个:
header(Location:http:// localhost:8889 / main.php);
位置
应该有一个大写L,并且尝试使用绝对URI总是很好的。
编辑
此链接在你的php.ini中检查是否启用了 output_buffering
是一个好主意。然后分别在文件的顶部和底部放置 ob_start()
和 ob_end_flush()
。
I am having a login page which has a form. The login data are send into a php script to check whether the user exists in the DB. If successful, it should redirect.
Here is my code:
<?php
$host="localhost:8889"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="TableName"; // Database name
$tbl_name="Users"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file
session_register("myusername");
session_register("mypassword");
header("location:../main.php");
die();
}
else {
echo "Wrong Username or Password";
}
?>
Count is 1, but redirection does not happen.
I have read this great answer and checked the following:
- there is not empty space before the opening tag, nor I am having another echo or print.
- i tried after the opening php tag to put
ob_start
but it didn't work either.
I didn't understand the thing with BOM. Can you help me on that?
I have been using the exact same code in the past and it worked and now that I am on mamp and mac does not work.
Thanks.
EDIT:
I did what people have said and after fixing these things and enabling reports, and the problem is that:
Call to undefined sesion_register.
So, I changed that as it is not defined nor and it is deprecated and it works now! Thanks all!
Try this:
header("Location: http://localhost:8889/main.php");
Location
should have a capital L, and it's always good to try using an absolute URI.
EDIT
After reading this link it might be a good idea to check that output_buffering
is enabled in your php.ini. Then place ob_start()
and ob_end_flush()
at the top and bottom of your file respectively.
这篇关于header中()在PHP中不重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!