errors in starting session

A

Anonymous

Guest
this code is to authenticate users who visit a site and to start a session. however the code generates errors. errors relating to php not being able to send the session variables because the header has already been sent. what would you advise to solve this problem?

Code:
<?php
           // import variables to be read from form
              import_request_variables("p", "s_");
              // set variables to connect to database
              $db_host = "localhost";
              $db_user = "******";
              $db_pass = "******";
              $db_name = "jamison_id";
              
              // connect to database
              $connection = mysql_connect($db_host, $db_user, $db_pass) or die ("Unable to connect!");
              mysql_select_db($db_name);
              $query = "SELECT id_num from login WHERE user = '$s_user_id' AND passwd = '$s_passwd'";
              $result = mysql_query($query, $connection) or die ("Error in query: '$query' ."  . mysql_error());
              
              // if row exists -> user/pass combination is correct
              	if (mysql_num_rows($result) == 1)
                	{
                		$valid = 1;
                	}
                // else
                //	{ 
                //  	print($s_user_id);
                //	  print($s_passwd);
                //	}
           //  }
            // $valid = authenticate($s_user_id, $s_passwd);
           // print($valid);
             if ($valid == 1)
                {   
             
                  // initiate a session
            	    session_start();
            	
            	    // register some session variables
            //	    session_register("SESSION");
            
                 	// including the username
            	//    session_register("SESSION_UNAME");
            	//    $SESSION_UNAME = $f_user;
	              }
	        ?>

thanks in advance for your help
 
It's a good thing I'll never get tired of answering this question. Oh, wait. :roll: (Sarcasm. Laugh.)

however the code generates errors. errors relating to php not being able to send the session variables because the header has already been sent. what would you advise to solve this problem?

Next time, do us a favor and post the actual error message and not your creative interpretation. But you did a decent job of describing it, so you might yet have a chance.

The problem is that when sessions are in use, PHP must add information to the HTTP response header (the messages sent back to the user's browser before the actual page). But if you've already started sending part of the page (e.g. you've put some HTML before the session_start(), or even a blank line or a single space or a period (read: anything), PHP has missed it's opportunity to add information to the header.

The solution: Get rid of any HTML, text, and whitespace before your initial "<?php", and then get rid of any PHP code that might generate output (e.g. print/echo) or error messages or anything before session_start().
 
here are the error messages..

Warning: Cannot send session cookie - headers already sent by (output started at f:\icarus\nubia\auth.php:2) in f:\icarus\nubia\auth.php on line 34

Warning: Cannot send session cache limiter - headers already sent (output started at f:\icarus\nubia\auth.php:2) in f:\icarus\nubia\auth.php on line 34

Warning: open(/tmp\sess_f537e04e49206e917a9dba6b305b9efd, O_RDWR) failed: No such file or directory (2) in f:\icarus\nubia\auth.php on line 34

Warning: open(/tmp\sess_f537e04e49206e917a9dba6b305b9efd, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
 
Posted: Fri Dec 12, 2003 8:42 pm Post subject:
here are the error messages..

Warning: Cannot send session cookie - headers already sent by (output started at f:\icarus\nubia\auth.php:2) in f:\icarus\nubia\auth.php on line 34

Warning: Cannot send session cache limiter - headers already sent (output started at f:\icarus\nubia\auth.php:2) in f:\icarus\nubia\auth.php on line 34
put the ob_start() function at the top of script

Warning: open(/tmp\sess_f537e04e49206e917a9dba6b305b9efd, O_RDWR) failed: No such file or directory (2) in f:\icarus\nubia\auth.php on line 34

Warning: open(/tmp\sess_f537e04e49206e917a9dba6b305b9efd, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0

create a tmp directory and set the session.save_path
in php.ini to that tmp directory path and give r/w rights
 
sigix said:
put the ob_start() function at the top of script

This is a lousy workaround that doesn't fix the actual problem and will degrade the performance of your page. Please disregard it. Sorry sigix.

sigix said:
create a tmp directory and set the session.save_path
in php.ini to that tmp directory path and give r/w rights

This is the correct solution and will most likely make the above workaround unnecessary. By the way, posting all those errors the first time would've gotten your question answered that much sooner. Not to mention we have a very useful Search facility that would've provided you with an answer without you needing to ask it at all -- this very same question has been answered at least once this month and at least two dozen times previously.
 
thanks for the help, it definitely saved my from hours of pulling at me hairs.

however i have a new problem. i can create session variables, but i can't access them across pages.

for example.

page2.php

Code:
<?php 
// start the session 
session_start(); 
header("Cache-control: private"); //IE 6 Fix 

echo "<strong>Step 2 - Register Session </strong><br />"; 

// Get the user's input from the form 
   $name = $_POST['name']; 

// Register session key with the value 
   $_SESSION['name'] = $name; 

// Display the sssion information: 
?> 

Welcome to my website <strong><? echo $_SESSION['name']; ?></strong>!<br /> 
Let's see what happens on the <a href="page3.php">next page.</a><br /><br />

page3.php
Code:
<?php 
// start the session 
session_start(); 
header("Cache-control: private"); //IE 6 Fix 
?> 
<strong>Step 3 - Test Session Part II </strong><br /> 
Hey <strong><? echo $_SESSION['name']; ?></strong> Everything is still working!<br /><br /> 
<strong>Pick an option:</strong><br /> 
Let's delete this session value now. <a href="page4.php">Click Here.</a><br /> 
Let's destroy this session. <a href="page5.php">Click Here.</a><br /><br />

on page2.php (after the _SESSION['name'] variable is set with a form on another page) the value of the variable comes up fine. however on page3.php, the value is blank. any thoughts why? i tried tinkering with the php.ini (v.4.0 on a Win98 box) but nothing changed.
 
are you sure it is 4.0? that's 3+ years old. 4.3.4 is the latest and I would reccomend using that.

version 4.0 did not have support for the superglobals $_SESSION that started at version 4.2

I'm assuming name is being set by a form?
 
here's the code for the form that sets the $_SESSION variable.
Code:
<?php 
session_start(); 
header("Cache-control: private"); // IE 6 Fix. 
?> 

<FORM METHOD="POST" ACTION="page2.php"> 
Enter your Name: <input type="text" name="name"> 
<input type="SUBMIT" value="Submit"> 
</FORM>

the server is using php 4.2.4 not 4.0, sorry about that.
 
i've been having a problem with the server starting the sessions, and when i tried to close a session, i got this error

Warning: Trying to destroy uninitialized session in f:\icarus\nubia\page4.php on line 5

from this code:
Code:
<?php 
// start the session 
session_start(); 
$_SESSION = array(); 
session_destroy(); 
echo "<strong>Step 5 - Destroy This Session </strong><br />"; 

if($_SESSION['name']){ 
    echo "The session is still active"; 
} else { 
    echo "Ok, the session is no longer active! <br />"; 
    echo "<a href=\"page1.php\"><< Go Back Step 1</a>"; 
} 
?>

would it matter that i turned the session_auto_start to 1 in the php.ini file?
 
sigix wrote:
put the ob_start() function at the top of script
This is a lousy workaround that doesn't fix the actual problem and will degrade the performance of your page. Please disregard it. Sorry sigix.
ok no problem swirlee :eek:
ok what else should I use/not use to avoid this "session/header already sent" errors, these errors are really anoying
 
obsidianchrysalis said:
i've been having a problem with the server starting the sessions, and when i tried to close a session, i got this error

Warning: Trying to destroy uninitialized session in f:\icarus\nubia\page4.php on line 5

from this code:
Code:
<?php 
// start the session 
session_start(); 
$_SESSION = array(); 
session_destroy(); 
echo "<strong>Step 5 - Destroy This Session </strong><br />"; 

if($_SESSION['name']){ 
    echo "The session is still active"; 
} else { 
    echo "Ok, the session is no longer active! <br />"; 
    echo "<a href="page1.php"><< Go Back Step 1</a>"; 
} 
?>

would it matter that i turned the session_auto_start to 1 in the php.ini file?

I tested your code [on linux]
it gave me following output
Code:
Step 5 - Destroy This Session
Ok, the session is no longer active!
<< Go Back Step 1

if you still face this problem try this
Code:
<?php 
// start the session 
session_start(); 
$_SESSION = array(); 
session_unset();
session_destroy(); 
echo "<strong>Step 5 - Destroy This Session </strong><br />"; 

if($_SESSION['name']){ 
    echo "The session is still active"; 
} else { 
    echo "Ok, the session is no longer active! <br />"; 
    echo "<a href="page1.php"><< Go Back Step 1</a>"; 
} 
?>

and for your last question:
refer to the php manual : ppl have really word hard for that manual :D
1 means it will auto start when your page load
0 means explicitly /you have to start your self
 
Back
Top