Jump to content

session already started error


piano0011

Recommended Posts

Hey guys!

I have the following error about session already started but I am confused because I should need to add session_start if I need to know whether the user has been logged in or not? Sorry about starting with the html at the top but I need this in order to get my nav bar to work at the top of the page but the error is at line 41, straight after the div class="nav-login", I think I should have the session_start there to determine if the user is logged in or not...

<!DOCTYPE html>
<html>
<head>
   
  <title></title>
  <meta charset="utf-8">
   <link rel="stylesheet" type="text/css" href="style.css">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<header>
   <nav> 
     
     
  
 
     
     
      
         <ul>
            <li><a href="header.php">Home</a></li>
            <li><a href="primer.php">Primer Level</a></li>
            <li><a href="level1.php">Level 1</a></li>
            <li><a href="level2.php">Level 2</a></li>
            <li><a href="level3.php">Level 3</a></li>
            <li><a href="signup.php">Signup</a></li>
            <li><a href="update.php">Update Profile</a></li>
            <li><a href="reset.php">Reset Password</a></li>
            <li><a href="qa.php">Q/A</a></li>
            <li><a href="contact.php">Contact Us</a></li>
            <li><a href="donation.php">Donation</a></li>
            <li><a href="maintenance.php">Status</a></li>
            <li><a href="review.php">Review</a></li>
            <li><a href="activate.php">Activate Email</a></li>
            <li><a href="bulletin.php">Bulletin</a></li>

   <div class="nav-login">
          
             <?php
             session_start();
                if (isset($_SESSION['u_uid'])) {
                   echo '<form action="includes/logout.php" method="POST">  
                   <button type="submit" name="submit">Logout</button>
                </form>';

 include_once 'includes/dbh.php';

                $sql = "SELECT * FROM users;";

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql)) {
   header("Location: index.php?login=error");
   exit();
} else {
    
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $resultCheck = mysqli_num_rows($result);

    if($resultCheck > 0) {
      while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['user_id'];
        $sqlImg = "SELECT * FROM profileimg WHERE userid = ?;";

        $stmt = mysqli_stmt_init($conn);

        if (!mysqli_stmt_prepare($stmt, $sqlImg)) {
           header("Location: index.php?login=error");
           exit();
        } else {
            mysqli_stmt_bind_param($stmt, "i", $id);
            mysqli_stmt_execute($stmt);
            $resultImg = mysqli_stmt_get_result($stmt);

            while ($rowImg = mysqli_fetch_assoc($resultImg)) {
               echo "<div>";
                  if ($rowImg['status'] == 0) {
                    $filename = "includes/uploads/profile".$id."*";
                    $fileinfo = glob($filename);
                    $fileext = explode(".", $fileinfo[0]);
                    $fileactualext = $fileext[1];
                    echo "<img class='profile_picture' src='includes/uploads/profile".$id.".".$fileactualext."?".mt_rand()."  width = '50' height = '50''>";
                  } else {
                    echo "<img class='default_picture' src='includes/uploads/profiledefault.jpg' width = '100' height = '50'>";
                  }
                 

                echo"</div>";
              }
            }
            }
        } 



     


}
   
   


 

Link to comment
Share on other sites

you need ONE session_start() statement on any page that sets or references session variables. the error you are getting is because you have executed a session_start() prior to the one where the error is occurring at. the session_start() statement should come near the top of your main file, in an 'initialization' section, where you define, create, and require (you should use require, rather than include or include_once) things that the rest of the code on the page needs.

best guess is the code you have posted is being included/required by another file and that main file has a session_start() statement in it. if so, just remove the session_start() in the posted code.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.