Connecting Tech Pros Worldwide Forums | Help | Site Map

Problems with a login page! Need Help!

mideastgirl's Avatar
Member
 
Join Date: Jun 2009
Location: Indiana
Posts: 65
#1: Jun 24 '09
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/m/i/d/mideasthonors/html/adminloginprocess.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/m/i/d/mideasthonors/html/adminloginprocess.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /home/content/m/i/d/mideasthonors/html/adminloginprocess.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 7

Warning: Cannot modify header information - headers already sent by (output started at /home/content/m/i/d/mideasthonors/html/adminloginprocess.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 24

This is the code I am using:
<?php
ob_start();
// first, let's destroy the previous session just in case it wasn't beforehand
// the new session will start at the news-edit page
session_start();

header("Cache-control: private"); //IE 6 Fix

set_time_limit(600);

include("admin-dbcon.php");

//Check to see if the username and password is valid (if it is in the database)
$validate = mysql_query("select * from admin_login where username = '$_POST[username]' and password = '$_POST[password]'");

$isvalid=mysql_num_rows($validate);

//if valid login send on, if not send to error page
if ($isvalid != 0) {
$_SESSION['username'] = $_POST[username];
while ($row=mysql_fetch_array($validate)){
$_SESSION['userid']=$row["ID"];
}
header("Location: admintasks.php");
}
//else {
// header("Location: login-error.php");
//}
?>

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 25 '09

re: Problems with a login page! Need Help!


The problem you have is a PHP problem not a MySQL problem.
Basically session_start() must be called before you output anything and you must make sure that it is not called twice (e.g. if you are including a file in another file).
The error message also tells you exactly which lines of code are outputting something before calling session_start();
Also read the PHP manual for those functions before using them.
Member
 
Join Date: Dec 2007
Posts: 47
#3: Jun 30 '09

re: Problems with a login page! Need Help!


It looks as if this code is being called from another php file....

If so the ob_start(); should be at the top of the file calling this along with the session_start(); and removed from the code sent.

Session error looks like its saying that session has already been started and the header error is saying that output has already been sent to the browser.

Guessing yout main script has outputed something then called this??


Quote:
if ($isvalid != 0) {
$_SESSION['username'] = $_POST[username];
while ($row=mysql_fetch_array($validate)){
$_SESSION['userid']=$row["ID"];
}
header("Location: admintasks.php");
}
This should be something like this
Expand|Select|Wrap|Line Numbers
  1. if ($isvalid == 1) {
  2.     $_SESSION['username'] = $_POST[username];
  3.     $_SESSION['userid']=mysql_result($isvalid, "ID");
  4.  
  5.     header("Location: admintasks.php");
  6. else if($isvalid > 1)
  7. {/*ERROR code here*/}
As what if there were multiple lines with the same username?

If you are going to use the fetch array loop method you need mysql_fetch_assoc not mysql_fetch_array.
Reply

Tags
headers already sent, session_start()