2014-04-04 63 views
0

我已經瀏覽過所以獲得我的代碼的幫助,但沒有遇到/瞭解代碼,這有助於我的錯誤。在PHP中顯示名字問題

我有一個包含用戶名,密碼和名字的表的數據庫。 我想在登錄儀表板時顯示用戶的名字。

我收到此錯誤的代碼,我將在下面提供:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php:31) in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 31

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 35

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 36

我的日誌代碼在:

<?php 

    session_start(); 
    if(isset($_POST['submitted'])) 
    { 

    $username = $_POST['username']; 
    $password = $_POST['password']; 

     $connect = mysqli_connect("", "", "", ""); 


     if (mysqli_connect_errno()){ 
      echo mysqli_connect_error(); 
     } 

     $query = "SELECT * FROM members where username = '$username'"; 


     $result = mysqli_query($connect, $query); 
     $log = mysqli_fetch_array($result); 
    $dbusername=$log['username']; 
    $dbpassword=$log['password']; 
    // } 
    if($username == $dbusername && $password == $dbpassword) 
    { 
     $_SESSION['username'] = $dbusername; 
     $_SESSION['password'] = $dbpassword; 
     header("Location: PMWebsite/dashboard.php"); 

    }else 
    { 
    echo "Username or Password Incorrect"; 

    } 
    }else{ 
    echo "No Connection"; 
    } 
    ?> 

代碼儀表盤顯示的用戶名:

<?php ob_start(); 
    session_start(); ?> 
    <!DOCTYPE html> 
    <html lang="en"> 
    <head> 
    <title>Dashboard</title> 

    <link href="css/bootstrap.min.css" rel="stylesheet"> 
    <link href="dashboard.css" rel="stylesheet"> 
     <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
     <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> 
    </head> 

    <body> 
    <?php 
    $user = $_SESSION['username']; 
    $query = "SELECT firstName FROM members where username = '$username'"; 
    $result = mysqli_query($connect, $query); 
    $log = mysqli_fetch_array($result); 
    $dbfirstName=$log['firstName']; 
    print_r($log); 
    echo $query; ?> 


     <div class="navbar navbar-inverse navbar-fixed-top" role="navigation"> 
     <div class="container-fluid"> 
      <div class="navbar-header"> 
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> 
       <span class="sr-only">Toggle navigation</span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
      </button> 
      <a class="navbar-brand" href="index.html">Aston Racing: Formula Student</a> 
      </div> 
      <div class="navbar-collapse collapse"> 
      <ul class="nav navbar-nav navbar-right"> 
       <li><a href="#">Settings</a></li> 
       <li><a href="#">Profile</a></li> 
       <li><a href="#">Log Out</a></li> 
      </ul> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-sm-3 col-md-2 sidebar"> 
      <ul class="nav nav-sidebar"> 
       <li class="active"><a href="#"><img src="PM/projects.png"> Projects</a></li> 
       <li><a href="gantt/teamGantt.html"><img src="PM/teamtimetable.png"> Team Gantt</a></li> 
       <li><a href="#"><img src="PM/meetings.png"> Meetings</a></li> 
       <li><a href="parts.html"><img src="PM/parts.png"> Part Orders</a></li> 
       <li><a href="#"><img src="PM/fileshare.png"> File Share</a></li> 
      </ul> 
      </div> 
      <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"> 
      <h1 class="page-header">Welcome <?php echo $dbfirstName; ?>!</h1> 

      <div class="row placeholders"> 
       <div class="col-xs-6 col-sm-3 placeholder">    
       <h4>Tasks To Do</h4> 
       <span class="text-muted"></span> 
      </div> 
      <div class="col-xs-6 col-sm-3 placeholder">    
       <h4>Upcoming deadlines</h4> 
       <span class="text-muted"></span> 
      </div> 
      <div class="col-xs-6 col-sm-3 placeholder"> 
       <h4>Goals</h4> 
       <span class="text-muted"></span> 
      </div> 
     </div> 
     </div> 
    </div> 
    </div> 
    </div> 
    </div> 
    </div> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script src="js/bootstrap.min.js"></script> 
    <script src="docs.min.js"></script> 
    </body> 
    </html> 

任何幫助將非常感謝謝謝!

+0

刪除這個'$ dbusername = $ log ['username'];'你沒有選擇它,因此打破了代碼! –

+0

<?php session_start();這應該是最前面的身體標籤... –

+0

你可以嘗試這樣的事情是這樣的$查詢$查詢=「選擇*從成員,其中用戶名='」。$ username。「'」; – JamesMarc

回答

0

sesson_start必須在任何輸出

<?php session_start(); ?> 
<html> 
<head> 
</head> 
<body> 
.... 

之前比你在你錯字SQL查詢

$user = $_SESSION['user']; 
$query = "SELECT firstName FROM members where username = '$user'"; 

不要忘記valited SQL查詢裏面所有的用戶輸入,避免sql injection wiki

定義
$username = mysqli_real_escape_string ($connect , $_POST['username']); 
$password = mysqli_real_escape_string ($connect , $_POST['password']); 

您正在嘗試使用更多的數據庫,您在y中聲明我們的SQL

$query = "SELECT username, firstName FROM members where username = '$user'"; 
$result = mysqli_query($connect, $query); 
$log = mysqli_fetch_array($result); 
$dbusername=$log['username']; 
$dbfirstName=$log['firstName'];?> 

沒有myqli在第二個文件

$connect = mysqli_connect("", "", "", ""); 
$user = $_SESSION['username']; 
$query = "SELECT firstName FROM members where username = '$username'"; 
$result = mysqli_query($connect, $query); 
+0

嗨@AdamFischer,試過這個,但由於mysql_real_escape_string收到了一系列錯誤:警告:mysql_real_escape_string()[function.mysql-real-escape-string]:無法通過套接字連接本地MySQL服務器'/ var/(2)在第7行的/var/sites/s/ssangar.com/public_html/login2.php 警告:mysql_real_escape_string()[function.mysql-real-escape-string]:到服務器的鏈接無法在/var/sites/s/ssangar.com/public_html/login2.php在第7行 (和更多沿着相同的行) – Sandeep

+0

編輯我的答案,必須使用mysqli_real_escape_string –

+0

真棒,mysqli _...現在工作,但仍然有與呼叫名字相同的問題。我在HTML中使用的代碼(

Welcome <?php echo $ dbfirstName?>!

)是否正確? – Sandeep

0

在打電話前start_session()無輸出,無論是普通的HTML標籤 ,文件中的空行,或PHP,可以進行。這就是爲什麼你收到這個錯誤:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php:31) in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 31

你也試圖在標題已發送後重定向。

如果你想要一些產量已作出後使用header()重定向:

使用輸出緩衝

<?php 
// Output can be delayed using buffers 

// that means you can output and the following 
// header commands will still be executed 

// "ob_gzhandler" callback 
// to send with compression 
ob_start("ob_gzhandler"); 

// No output may be made, either by normal HTML 
// tags, blank lines in a file, or from PHP 
// until you defined your header information 
// using header() 

// Set your header (example): 
// set caching expiration to 30 days in the future 
$date = gmdate("D, j M Y H:i:s \G\M\T", time() + 30*24*60*60); 

// same as: <meta http-equiv="expires" content="Sun, 4 May 2014 13:10:54 GMT" /> 
header('Expires: ' . $date); 

echo "something"; 

header("Location: http://www.example.com"); 
// redirect to http://www.example.com 

// send contents of buffer 
ob_flush(); 
?> 

不要忘了在你的腳本的末尾調用ob_flush

此外,尊重session_start()ob_start()的正確順序。

If a user uses ob_gzhandler or similar with ob_start(), the function order is important for proper output. For example, ob_gzhandler must be registered before starting the session.

來源:session_start()

瞭解更多關於ob_start()ob_gzhandler()ob_flush()

0

這是發生,因爲,你做一個重定向之前,你的文件打印一些輸出。

您需要添加輸出緩衝。

有了這個,無論您的文件輸出如何,它都會存儲在輸出緩衝區中,並且重定向也可以工作。

因此,在<?php之後加上ob_start(),你的代碼應該可以工作。

+0

即使我在頂部添加session_start,我是否也需要這個? – Sandeep

+0

是在文件的最開始處。 – Pupil

1

我已經通過你的代碼我想你應該這樣做。

1.Always write your <?php session_start(); ?> above the <htnl> tag and don't keep space while writing <?php ?> syntax