php not redirecting to my login_success.php page

我login_success php不重定向。php页面

Tags: php
标签: php

问题 (Question)

Trying to make a login page using php following a tutorial, it's successfully logging in getting both the password and username from the database then showing echo "wrong password" and welcome for each scenario. However it doesnt redirect to my login_success.php page stays on check_login.php, heres my code for the check page:

<?php

$host="localhost"; 
$username="root"; 
$password="root";
$db_name="test";
$tbl_name="members"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 


$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);

$count=mysql_num_rows($result);

if($count > 0){
echo "Welcome";

session_register("myusername");

session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

any help would be great.

试图让一个登录页面使用php教程后,它的成功登录密码和用户名从数据库然后显示回波对每个场景“密码错误”和欢迎。然而我并不重定向到login_success。php页面停留在check_login。php,这我的代码检查页面:

<?php

$host="localhost"; 
$username="root"; 
$password="root";
$db_name="test";
$tbl_name="members"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 


$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);

$count=mysql_num_rows($result);

if($count > 0){
echo "Welcome";

session_register("myusername");

session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

任何帮助将是巨大的。

最佳答案 (Best Answer)

You cannot send anything to the client (i.e. echo "Welcome";) before setting your headers.

Reference http://www.php.net/manual/en/function.header.php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Nor can you send anything to the client before starting sessions.

你不能发送任何客户端(即。echo "Welcome";),然后设置你的头。

参考http://www.php.net/manual/en/function.header.php

记住,标题()必须在任何实际的输出被发送之前,通过正常的HTML标记,空白行一个文件,或者从PHP。这是一个非常常见的错误与包括阅读代码,或者要求,功能,或另一个文件访问功能,前有空格或空行输出标题()。同样的问题存在在使用一个PHP / HTML文件。

你也不能发送任何客户端开始前会议。

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23451189