piano0011 Posted July 7, 2018 Share Posted July 7, 2018 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>"; } } } } } Quote Link to comment https://forums.phpfreaks.com/topic/307460-session-already-started-error/ Share on other sites More sharing options...
mac_gyver Posted July 7, 2018 Share Posted July 7, 2018 (edited) 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 July 7, 2018 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/307460-session-already-started-error/#findComment-1559370 Share on other sites More sharing options...
piano0011 Posted July 7, 2018 Author Share Posted July 7, 2018 This is interesting because I tried to get rid of that session_start but it won't show the logout button as intended: So, I guess that I need the session_start here and should delete the other main one somewhere.. Quote Link to comment https://forums.phpfreaks.com/topic/307460-session-already-started-error/#findComment-1559371 Share on other sites More sharing options...
piano0011 Posted July 7, 2018 Author Share Posted July 7, 2018 I got it! I deleted the other session_start in my other file and now it is working.... Quote Link to comment https://forums.phpfreaks.com/topic/307460-session-already-started-error/#findComment-1559372 Share on other sites More sharing options...
piano0011 Posted July 7, 2018 Author Share Posted July 7, 2018 What is the difference between require and include_once or include? Quote Link to comment https://forums.phpfreaks.com/topic/307460-session-already-started-error/#findComment-1559374 Share on other sites More sharing options...
mac_gyver Posted July 7, 2018 Share Posted July 7, 2018 ^^^ that's answered in the php.net documentation. Quote Link to comment https://forums.phpfreaks.com/topic/307460-session-already-started-error/#findComment-1559375 Share on other sites More sharing options...
piano0011 Posted July 7, 2018 Author Share Posted July 7, 2018 Ok... will look into it.. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/307460-session-already-started-error/#findComment-1559376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.