session_start

by 9 replies
11
Hey All

so a few things going on here I can use a hand with 1) I'm getting session error can't send header already sent issue and it's the very first thing being called I attached image for viewing as the 2) issue is looking at image when I add the session_start it messes up all the alignment of the page if I remove it works fine??

PHP Code:
<?php
session_start
();
if (!isset(
$_SESSION['username'])) {
echo 
"I'm truly Sorry However In Order To View This Section You Must Be Logged In.<a hfref='login.php'>Click Here To Login</a>";
exit();
}
include(
"connect.php");
?>
#programming #sessionstart
  • Make sure the <?php is on line 1 column 1. You may need to look at the ob_start and ob_flush functions.
  • and why you think I'm loosing alignment of page?
  • dunno OB's don't work I had already tried flush and start both changed nothing?
  • I'm thinking the alignment issue is do to the PHP error message being displayed. The session_start() function should not cause an alignment issue. Use a browser with some developer tools like Chrome or Firefox so that you can take a look at the elements individually on the page. Maybe some CSS file isn't loaded because of the PHP error?
  • ok, I believe the same as after I wrote that I had another look and yes it's prob cause the text it being output.. anyhow does anyone have another solution I can try to fix this error...
  • Can you zip the code file and post it for a look?
    Is this code included by some other file?
    Something has output to the browser before session_start() started, it doesn't have to be much a single space is all that is required for session_start() to produce the error.
  • Unless I am mistaken you need to close the session at the end of your code... At the moment you are trying to open a session on top of a session thus the error headers already sent.
    • [1] reply
    • No, that doesn't happen. A session_start() in one file begins the session, a session_start() in another continues the same session. See PHP: session_start - Manual for the example given for the function. The issue is that something has written output before session_start() is called. Maybe do something like

      <?php
      if (!isset($_SESSION)) {
      session_start();
      }
      ?>

      If you want to debug why the session already existed you could add the following to get clues about the session.

      <?php
      else {
      echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
      }
      ?>
      • [1] reply

Next Topics on Trending Feed

  • 11

    Hey All so a few things going on here I can use a hand with 1) I'm getting session error can't send header already sent issue and it's the very first thing being called I attached image for viewing as the 2) issue is looking at image when I add the session_start it messes up all the alignment of the page if I remove it works fine??