2014-01-26 21 views
0

我'管理checklogin.php'執行後啓動會話,但當我嘗試重定向到'login_success.php'我仍然在同一'checklogin.php'頁面。你對我能做什麼有什麼建議嗎?會話啓動失敗後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 
+1

你爲什麼要在這裏控制緩衝區輸出? –

+0

你會建議我在哪裏控制它? – Dan

+0

我只是想知道**爲什麼**你正在控制它 –

回答

-2

這會爲你工作:

<?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"; 
} 
?> 
+0

我已經更新了我的答案。我發佈的第一個代碼是錯誤的。 –

+1

謝謝,我用過這個版本。 – Dan

1

我才發現,原來我已經把HTML中的PHP代碼。我把它放在文檔的頂部,現在它可以工作。