homepage Welcome to WebmasterWorld Guest from 184.73.104.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Session problem
Cannot send session cookie - headers already sent
SIRokai

10+ Year Member



 
Msg#: 5715 posted 7:34 pm on Nov 12, 2004 (gmt 0)

Hi all,

I cant get this this to work...
I have a form which submits to a file that check the fields and validates it and creates the Session vars.

As follows
<?php
session_start();
//create the variables
$first_name = 'first_name';
$last_name = 'last_name';
$email_address = 'email_address';

// create the session variable
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email_address'] = $email_address;

include 'db.php';

// Convert to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if((!$username) (!$password)){
$error .= "Please enter ALL of the information! <br />";
include 'login_form.php';
exit();
}
// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
while($row = mysql_fetch_array($sql)){
foreach( $row AS $key => $val ){
$$key = stripslashes( $val );
}
header("Location: login_success.php");
}
} else {
$error = "You could not be logged in! Either the username and password do not match or you have not validated your membership! Please try again!<br />";
include 'login_form.html';
}
?>

if it validates it goes to the next page, login is successful

<?php
session_start();
if ( empty( $first_name) ) {
print "Please login below!";
include 'login_form.php';
}
else {

include 'db.php';

echo "Welcome, ". $_SESSION['first_name'] .". ";

echo "<title>Title</title><br><br><br><h3><b>text</b></h3>";

include 'file1.php';

echo "<br><br><br><h3><b>text:</b></h3>";

include 'file2.php';

echo "<br><br><br><h3><b>text:</b></h3>";

include 'file3.php';

echo "<br><br><br><h3><b>text:</b></h3>";

include 'file4.php';
;}
echo "<br><br><br><b><a href=logout.php>Logout</a><br>";
?>

I am getting the following error

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Inetpub\vhosts\sitename.ca\httpdocs\php\login_success.php:1) in C:\Inetpub\vhosts\sitename.ca\httpdocs\php\login_success.php on line 2

Could you look the code over... Perhaps you see something I dont.

thanks in advance
J

 

baertyp

10+ Year Member



 
Msg#: 5715 posted 9:33 am on Nov 13, 2004 (gmt 0)

Nothing wrong with the code as far as I can see. Have a closer look at all the included files, maybe there's a session_start() in one of them.

Regards
Markus

tosho

10+ Year Member



 
Msg#: 5715 posted 2:37 am on Nov 14, 2004 (gmt 0)

have you checked for any spaces or new lines in the beginning of the file, before the <?php tag?

SIRokai

10+ Year Member



 
Msg#: 5715 posted 1:00 pm on Nov 16, 2004 (gmt 0)

I did... I cant see any problems, there are no spaces and all pages start with the session.

J

virileone

10+ Year Member



 
Msg#: 5715 posted 9:53 am on Nov 17, 2004 (gmt 0)

Check 2 things....
one you cant print anything b4 sending a (header 'location: ) chingas..

2 (the probable case)
there is a space b4 your 1st line if <?php in your db.php include.
.

Good luck.

J

// Convert to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if((!$username) (!$password)){
$error .= "Please enter ALL of the information! <br />";
include 'login_form.php';
exit();
}
// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
while($row = mysql_fetch_array($sql)){
foreach( $row AS $key => $val ){
$$key = stripslashes( $val );
}
header("Location: login_success.php");

mincklerstraat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 5715 posted 10:17 am on Nov 17, 2004 (gmt 0)

The answer is in your error message:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Inetpub\vhosts\sitename.ca\httpdocs\php\login_success.php:1) in C:\Inetpub\vhosts\sitename.ca\httpdocs\php\login_success.php on line 2

This means that on line 1 of login_success.php, 'output started' already - something has been output - either a line or a space or anything else before the opening <?php tag in this file, or else something output by echo, print, or one of the other output-producing functions. The first is much more likely. Check out that file, and that line - line 1.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved