2017-06-03 27 views
0

美好的一天大家好,我是新的PHP。我檢查了w3c並得到了一些很好的提示。不過,我對我的登錄代碼有一個小小的挑戰。它顯示PHP警告:不能修改標題信息 - 已在第8行上的/home/perspect/public_html/jst/index.php(由/home/perspect/public_html/jst/dbcon.php:2開始輸出)發送的標題 以下是我的索引頁面;php登錄工作離線,但不在線

<?php 
session_start(); 
include_once 'includes/dbcon.php'; 

if(isset($_SESSION['user'])!="") 
{ 
    header("Location: candidate_list.php"); 
} 

if(isset($_POST['btn-login'])) 
{ 

    $username = mysqli_real_escape_string($con, $_POST['username']); 
    $password = mysqli_real_escape_string($con, $_POST['password']); 
    $stat = 'notvoted'; 

    $res=mysqli_query($con,"SELECT * FROM students WHERE username='$username' AND status='$stat'"); 
    $row=mysqli_fetch_array($res); 

    if($row['password']==md5($password)) 
    { 
     $_SESSION['user'] = $row['user_id']; 
     header("Location: candidate_list.php"); 
    } 
    else 
    { 
     ?> 
     <script>alert('wrong details');</script> 
     <?php 
    } 

} 
?> 
<!-- Login Box --> 
          <div class="col-md-6 col-md-offset-3 col-sm-offset-3"> 
           <form class="login-page" action="" method="post"> 
            <div class="login-header margin-bottom-30"> 
             <h2>Login</h2> 
            </div> 
            <div class="input-group margin-bottom-20"> 
             <span class="input-group-addon"> 
              <i class="fa fa-user"></i> 
             </span> 
             <input placeholder="VIN" name="username" class="form-control" type="text"> 
            </div> 
            <div class="input-group margin-bottom-20"> 
             <span class="input-group-addon"> 
              <i class="fa fa-lock"></i> 
             </span> 
             <input placeholder="Password" name="password" class="form-control" type="password"> 
            </div> 
            <div class="row"> 

             <div class="col-md-6"> 
              <button class="btn btn-primary pull-right" type="submit" name="btn-login">Login</button> 
             </div> 
            </div> 
            <p> 
           </form> 
          </div> 
          <!-- End Login Box --> 

我的candidate_list.php代碼是;

<?php 
session_start(); 
include_once 'includes/dbcon.php'; 

if(!isset($_SESSION['user'])) 
{ 
    header("Location: index.php"); 
} 
$res=mysqli_query($con, "SELECT * FROM students WHERE user_id=".$_SESSION['user']); 
$userRow=mysqli_fetch_array($res); 
?> 

我dbcon是

<?php 

$con=mysqli_connect("xxx","xxx","xxxx","xxx"); 
// Check connection 
if (mysqli_connect_errno()) { 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

?> 

謝謝您的幫助。

+0

你有沒有試圖改變這一行:'isset($ _ SESSION [ '用戶']) !=「」'isset($ _ SESSION ['user'])'? –

+0

剛剛嘗試,但它仍然不起作用 –

+0

快速檢查 - 您的dbcon文件中的<?php'之前是否有空格?不應該有。 –

回答

1

這種類型的php警告會在頭重定向之前回顯一些內容。

如果你仍然想重定向你可以使用ob_start或者您可以使用JavaScript

window.location.href =路徑
+0

這個想法真的幫助我做對了..非常感謝 –