Jump to content

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
https://forums.phpfreaks.com/topic/307460-session-already-started-error/
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.

Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.