0

I have looked through SO to get help with my code however haven't come across/understood the code which helps me with my error.

I have a database with a table which holds username, password and first name. I would like to display the user's first name when they log into the dashboard.

I receive this error with the code I will provide below:

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

My Code for Log In:

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

Code for dashboard to display username:

<?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>

Any help would be very much appreciated thanks!

Sandeep
  • 59
  • 1
  • 2
  • 7
  • remove this `$dbusername=$log['username'];` u are not selecting it and hence breaking the code !! – Abhik Chakraborty Apr 04 '14 at 11:27
  • can you try this something like this for $query $query = "SELECT * FROM members where username = '".$username."'"; – JamesMarc Apr 04 '14 at 11:32
  • @AbhikChakraborty Error: 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:2) in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 2 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 38 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 39 – Sandeep Apr 04 '14 at 11:43
  • @naveengoyal With that error, it still does not show the First Name. If I replace $dbfirstName in the html line, with $SESSION_('username'), it does display my username? – Sandeep Apr 04 '14 at 11:44

4 Answers4

1

I have gone through your code I think You should do.

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

sesson_start must be defined before any output

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

Than you have typo in you sql query

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

Dont forget to valited all user input inside sql query, to avoid sql injection wiki

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

You are trying to use more cols from DB that you are stating in your 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'];?>

No myqli in second file

$connect = mysqli_connect("", "", "", "");
$user = $_SESSION['username'];
$query = "SELECT firstName FROM members where username = '$username'"; 
$result = mysqli_query($connect, $query);
Adam Fischer
  • 1,075
  • 11
  • 23
  • Hi @AdamFischer, tried this but received a host of errors due to the mysql_real_escape_string: Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /var/sites/s/ssangar.com/public_html/login2.php on line 7 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /var/sites/s/ssangar.com/public_html/login2.php on line 7 (and a bit more along the same lines) – Sandeep Apr 04 '14 at 11:47
  • Edited my answer, mysqli_real_escape_string must be used – Adam Fischer Apr 04 '14 at 11:51
  • Awesome, the mysqli_... works now, however still having the same issue with the calling First Name. Is the code I am using in the html (

    Welcome !

    ) correct?
    – Sandeep Apr 04 '14 at 12:31
  • It looks like it is, can you print_r($log); and echo "SELECT firstName FROM members where username = '$username'" ? – Adam Fischer Apr 04 '14 at 12:33
  • $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; ?> – Adam Fischer Apr 04 '14 at 12:45
  • 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:2) in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 3 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 36 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 37 – Sandeep Apr 04 '14 at 12:50
  • SELECT firstName FROM members where username = '' – Sandeep Apr 04 '14 at 12:51
  • Coul you please edit your question and provide more of the dashboard.php file? – Adam Fischer Apr 04 '14 at 12:53
  • Updated the code so you can see the whole thing. Appreciate your help by the way. – Sandeep Apr 04 '14 at 13:03
  • $query = "SELECT firstName FROM members where username = '$user'"; and remove the ob_start(); from the beggining of your code – Adam Fischer Apr 04 '14 at 13:19
  • Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/sites/s/ssangar.com/public_html/PMWebsite/dashboard.php on line 34 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 35 SELECT firstName FROM members where username = 'sangars2' – Sandeep Apr 04 '14 at 13:29
  • you miss mysqli_connect in you dashboard.php (no mysqli connection), answer edited – Adam Fischer Apr 04 '14 at 13:34
  • Perfect! Worked a charm. Thanks for your help! – Sandeep Apr 04 '14 at 13:38
0

Before you call start_session() no output, either by normal HTML tags, blank lines in a file, or from PHP, may be made. That's why you're receiving this error:

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

Also you're trying to redirect after the header is already sent.

If you want to redirect using header() after some output already is made:

Use Output Buffer

<?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();
?>

Don't forget to call ob_flush at the end of your script.

Also, respect the correct order of session_start() and 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.

Source: session_start()

Read more about ob_start(), ob_gzhandler() and ob_flush()

citizen404
  • 1,485
  • 1
  • 10
  • 19
0

This is happening because, your file is printing some output before you do a redirect.

You need to add output buffering.

With this, whatever output your file prints, it gets stored in output buffer and the redirection will work.

So, add ob_start() right after <?php and your code should work.

Pupil
  • 23,834
  • 6
  • 44
  • 66