farban Posted March 11, 2009 Share Posted March 11, 2009 Hello there I am making a login for my social network site as a uni project. I have followed a tutorial for the login but when i login at the login.php page it shows up with three header errors stating Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\testsite\community\login.php:2) in C:\xampp\htdocs\testsite\community\login.php on line 17 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\testsite\community\login.php:2) in C:\xampp\htdocs\testsite\community\login.php on line 17 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\testsite\community\login.php:2) in C:\xampp\htdocs\testsite\community\login.php on line 23 Here is all the source code for the files that are related to the login of the site login.php <?php if(isset($_POST['submitted'])) { require_once ('includes/loginfunctions.php'); require_once ("includes/_connect.inc.php"); list ($check, $data) = check_login ($dbc, $_POST['user_username'], $_POST['user_password']); if ($check) { session_start(); $_SESSION['user_id'] = $data ['user_id']; $_SESSION['user_first_name'] = $data ['user_first_name']; $url = absolute_url ('loggedin.php'); header("Location: $url"); exit(); } else { $errors = $data; } mysqli_close($dbc); } include ("includes/loginpage.php"); ?> loginfunctions.php <?php function absolute_url ($page = 'index.php') { $url = 'http://'.$_SERVER['HTTP_HOST']. dirname($_SERVER['PHP_SELF']); $url = rtrim ($url, '/\\'); $url .= '/'. $page; return $url; } function check_login ($dbc, $user_username ='', $user_password ='') { $errors = array(); if (empty($user_username)) { $errors[] ='you forgot to enter your username'; } else { $u = mysqli_real_escape_string($dbc, trim($user_username)); } if (empty($user_password)) { $errors[] ='you forgot to enter your password'; } else { $p = mysqli_real_escape_string($dbc, trim($user_password)); } if (empty($errors)) { $q = "SELECT user.user_id, user.user_first_name FROM user WHERE user.user_username ='$u' AND user.user_password =SHA1('$p')"; $r = @mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); return array (true, $row); } else { $errors[] = 'the username and password do not match'; } } return array(false, $errors); } ?> loginpage.php <?php include("includes/start.php"); if (!empty($errors)) { echo '<h1>Error</h1> <p>the following error occured:<br />'; foreach ($errors as $msg) { echo " - $msg<br />\n"; } echo '</p><p>Please try agian</p>'; } ?> <h1>Login</h1> <form action="login.php" method="post"> <p>Username : <input type="text" name="user_username" size="20" maxlengh="80"/> </p> <p>Password : <input type="password" name="user_password" size="20" maxlengh="80"/> </p> <p><input type="submit" name="submit" value="Login"/> </p> <input type="hidden" name="submitted" value="TRUE"/> </form> <?php include("includes/footer.php"); ?> loggedin.php <?php session_start(); if (!isset($_SESSION['user_id'])) { $url = absolute_url(); header("Location: $url"); exit(); } include("includes/start.php"); echo "<h1>You are Logged in</h1> <p>You are now logged in, {$_SESSION ['user_first_name']}!</p> <p><a href=\"logout.php\">Logout</a></p>"; include("includes/footer.php"); ?> logout.php <?php session_start(); if (!isset($_SESSION['user_id'])) { require_once ('includes/loginfunctions.php'); $url = absolute_url(); header("Location: $url"); exit(); } else { $_SESSION = array(); session_destroy(); setcookie ('PHPSESSID', '', time()-3600, '/', '',0,0); } include ("includes/start.php"); echo "<h1>logged out</h1> <p>you are now logged out</p>"; include ("includes/footer.php"); ?> start.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <link href="core/general.css" rel="stylesheet" type="text/css" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="en-us" /> <title>SCO SITE</title> </head> <body> <div class="container"> <div class="header"> <h6>HEADER</h6> </div> <div class="menu"> <li><a href="index.php">Home</a></li> <li><?php if ((isset($_SESSION ['user_id'])) && (!strpos($_SERVER['PHP_SELF'], 'logout.php'))) { echo '<a href="logout.php">Logout</a>'; } else { echo '<a href="login.php">Login</a>'; } ?></li> </div> <div class="content"> Can anyone point out the problem ? im really pulling my hair out over this and want my login to work Much thanks Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/ Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 use require_once() not include() try it... Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782377 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2009 Share Posted March 11, 2009 Based on the error output started at ...login.php:2 your probably have two blank lines before the <?php tag. Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782396 Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 use require_once() not include() try it... Wrong. Will not help at all. Read this sticky Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782400 Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 well i be dammed, on my book it says, if you use require_once() from a page with header's you wont get the error message as it get the page once. probably a white space anyway. Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782403 Share on other sites More sharing options...
farban Posted March 11, 2009 Author Share Posted March 11, 2009 I read the sticky and sorry im just not very good at understanding php and the header problems. Same number of header problems still Here is what the login.php looks like now login.php <?php if(isset($_POST['submitted'])) { require_once ('includes/loginfunctions.php'); require_once ("includes/_connect.inc.php"); list ($check, $data) = check_login ($dbc, $_POST['user_username'], $_POST['user_password']); if ($check) { session_start(); $_SESSION['user_id'] = $data ['user_id']; $_SESSION['user_first_name'] = $data ['user_first_name']; $url = absolute_url ('loggedin.php'); header("Location: $url"); exit(); } else { $errors = $data; } mysqli_close($dbc); } require_once ("includes/loginpage.php");?> Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782406 Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 <?php ob_start //<<< ?>ob_end // end of page. the lazy way. Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782409 Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 well i be dammed, on my book it says, if you use require_once() from a page with header's you wont get the error message as it get the page once. This has some sense in case a file with session_start() is included several times. Note however, that the error here occurs in main file, and before any include() is called. And by giving an advice like you did, without any explanation you're just introducing confusion. Be careful about it. Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782410 Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 agree sorry for being lazy. Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782412 Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 farban: are you 100% sure there are no white space characters before <?php tag in login.php ? Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782421 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2009 Share Posted March 11, 2009 If the current error message is exactly the same as in the first post, you have two blank lines or one blank line and BOM characters before the <?php tag. If the error message is different, post it to get the quickest solution. Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782424 Share on other sites More sharing options...
samshel Posted March 11, 2009 Share Posted March 11, 2009 thats the only possible reason, either a whitespace or a newline char.. Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782425 Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 thats the only possible reason, either a whitespace or a newline char.. A newline char is a whitespace char Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782427 Share on other sites More sharing options...
samshel Posted March 11, 2009 Share Posted March 11, 2009 oops ! sorry ! Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782432 Share on other sites More sharing options...
redarrow Posted March 11, 2009 Share Posted March 11, 2009 are you using dream weaver there , if so use notepad Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782437 Share on other sites More sharing options...
farban Posted March 11, 2009 Author Share Posted March 11, 2009 im using jedit [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782442 Share on other sites More sharing options...
samshel Posted March 11, 2009 Share Posted March 11, 2009 If the current error message is exactly the same as in the first post, you have two blank lines or one blank line and BOM characters before the <?php tag. If the error message is different, post it to get the quickest solution. did u consider this? Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782444 Share on other sites More sharing options...
farban Posted March 11, 2009 Author Share Posted March 11, 2009 whole project with sql file in folder [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782464 Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 There's an extra line on top oh loginfunctions.php Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782481 Share on other sites More sharing options...
farban Posted March 11, 2009 Author Share Posted March 11, 2009 mchl i could kiss you lol much thanks to all who helped ill mark it as solved Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782491 Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 I won't oppose if you're female. Otherwise forget it Quote Link to comment https://forums.phpfreaks.com/topic/149000-solved-having-three-header-errors-which-i-cant-seem-to-solve/#findComment-782493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.