会话开始失败后PHP重定向

时间:2014-01-26 20:22:06

标签: php mysql

我正在设法在'checklogin.php'执行后启动一个会话,但是当我尝试重定向到'login_success.php'时,我仍然在同一个'checklogin.php'页面上。你对我能做什么有什么建议吗?

<?php

    ob_start();
    $host="";
    $username="";
    $password="";
    $db_name="";
    $tbl_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 "login_success.php"
    session_start();
    $_SESSION['myusername']=$myusername;
    $_SESSION['mypassword']=$mypassword;
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password. Please try again.";
    }
    ob_end_flush();

    ?>

我收到以下错误:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /homepages/46/d473160420/htdocs/checklogin.php:58) in /homepages/46/d473160420/htdocs/checklogin.php on line 93

Warning: Cannot modify header information - headers already sent by (output started at /homepages/46/d473160420/htdocs/checklogin.php:58) in /homepages/46/d473160420/htdocs/checklogin.php on line 96

2 个答案:

答案 0 :(得分:1)

我刚刚发现我已将PHP代码放在HTML中。我将它放在文档的顶部,现在可以正常工作。

答案 1 :(得分:-1)

这对你有用:

<?php
session_start();
if(isset($_POST['myusername']) AND isset($_POST['mypassword']))
{
$host="";
$username="";
$password="";
$db_name="";
$tbl_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 "login_success.php"
$_SESSION['myusername']= $myusername;
$_SESSION['mypassword']= $mypassword;
header("Location: login_success.php");
}
else {
echo "Wrong Username or Password. Please try again.";
}
}else{
echo "Type your Username and Password";
}
?>