Jump to content

Archived

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

dbillings

[SOLVED] Session_start() trouble

Recommended Posts

I read the header error bit that is posted and it didn't help me. I keep receiving the following error message and can't figure out why. The login script works fine on it's own but when I include it in my index.php page it gives me the error.

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/www/Devendea/lux/index.php:5) in /home/www/Devendea/lux/login.php on line 2

I made sure that there wasn't any white space before or after my <?php, ?> tags and I still get the error. Here's the script.

[code]<?php
session_start();
include ("mysql_connect1.php");

if (isset($_REQUEST['submit'])){
    $u = $_REQUEST['loginname'];
    $p = $_REQUEST['pass2'];
$hp = md5($p);
$query = "SELECT loginname, admin, member, wararranger FROM users WHERE loginname ='$u' AND pass2 = '$hp'";
    $result = @mysql_query($query)or die ('Query could not be processed: '.mysql_error());
    $row = mysql_fetch_array ($result, MYSQL_NUM);


    if($row){
     
      $_SESSION['loginname'] = $row[0];
      $_SESSION['admin'] = $row[1];
      $_SESSION['member'] = $row[2];
      $_SESSION['wararranger'] = $row[3];
     
    echo "<p>Welcome,<b> {$_SESSION['loginname']}</b>!</p>";
 
  }elseif(!isset($_SESSION['loginname'])){
   
    echo "<p>Invalid login attempt!</p>";
   
   

  }

}

if (isset($_SESSION['loginname'])){

  }else{

include ("loginform.php");

}
?>[/code]

And here is the index.php I'm using and receiving the error on. I tried to keep it simple to see what the problem is but the problem remains.

[code]<html>

<head>

</head>

<body>

<?php include ("login.php");?>

</body>

</html>[/code]

Share this post


Link to post
Share on other sites
If your index.php is this it would be better

[code]
<?php include ("login.php");?>

<html>

<head>

</head>

<body>

</body>

</html>
[/code]

Since sessions need nothing to be before them even HTML

Share this post


Link to post
Share on other sites
Well yes, that would be the problem. You have already started rendering the page by the time the PHP script is included, hence the "headers already sent" error.

Share this post


Link to post
Share on other sites
make sure that theres no white spaces and always put the session_start() at the top good luck.

Share this post


Link to post
Share on other sites
You could move the session_start() function to the top of the other file.

When you include your file like that it will become: [code]<html>

<head>

</head>

<body>

<?php
session_start();
include ("mysql_connect1.php");

if (isset($_REQUEST['submit'])){
    $u = $_REQUEST['loginname'];
    $p = $_REQUEST['pass2'];
$hp = md5($p);
$query = "SELECT loginname, admin, member, wararranger FROM users WHERE loginname ='$u' AND pass2 = '$hp'";
    $result = @mysql_query($query)or die ('Query could not be processed: '.mysql_error());
    $row = mysql_fetch_array ($result, MYSQL_NUM);


    if($row){
     
      $_SESSION['loginname'] = $row[0];
      $_SESSION['admin'] = $row[1];
      $_SESSION['member'] = $row[2];
      $_SESSION['wararranger'] = $row[3];
     
    echo "<p>Welcome,<b> {$_SESSION['loginname']}</b>!</p>";
 
  }elseif(!isset($_SESSION['loginname'])){
   
    echo "<p>Invalid login attempt!</p>";
   
   

  }

}

if (isset($_SESSION['loginname'])){

  }else{

include ("loginform.php");

}
?>

</body>

</html>[/code]

And as you see, then session_start() is not run before something is sent to output.

Share this post


Link to post
Share on other sites

×

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.