Schlo_50 Posted December 18, 2007 Share Posted December 18, 2007 Right guys, Tearing my hair out over this one. I am getting this error: "Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/fhlinux193/a/ahead4shop.co.uk/user/htdocs/demo88/area/index.php:6) in /home/fhlinux193/a/ahead4shop.co.uk/user/htdocs/demo88/area/addtab.php on line 1 Warning: Cannot modify header information - headers already sent by (output started at /home/fhlinux193/a/ahead4shop.co.uk/user/htdocs/demo88/area/index.php:6) in /home/fhlinux193/a/ahead4shop.co.uk/user/htdocs/demo88/area/addtab.php on line 72" I've made sure there is no whitespace, and that the call to start a session is at the top of the script but i keep getting the above error. I'll show you line 6 of index.php, line 1 of addtab.php and line 72 of addtab.php. Line 6, index.php <title>Site Log Books</title><style type="text/css"> Line 1,2 and 3 of addtab.php <?php session_start(); function registerUser($user,$pass1,$pass2){ $errorText = ''; Line 72 of addtab.php header('Location:?id=login'); Please, could someone point out what im doing wrong? I have checked whitespace and checked the position of sesssion_start. Also, if you need any more information please ask! Thanks Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2007 Share Posted December 18, 2007 are you including addtab.php in index.php ? if so your session_start() should be index.php and removed out of addtab.php Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted December 18, 2007 Author Share Posted December 18, 2007 Index.php includes the pages of my site. For example, addtab.php is included in manage.php which is then included in index.php. If you catch my drift? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted December 18, 2007 Share Posted December 18, 2007 If index.php is your main page, try starting the session there. (Kinda what rajivgonsalves said..) Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2007 Share Posted December 18, 2007 Yes session_start should be only in index.php remove it from everywhere else. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted December 18, 2007 Share Posted December 18, 2007 If nothing else works: Are any of your files encoded as UTF-8? If so, read my answers to this post. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted December 18, 2007 Author Share Posted December 18, 2007 Ok, Tried that an great! One of the two errors are gone. Now the remaining one: Warning: Cannot modify header information - headers already sent by (output started at /home/fhlinux193/a/ahead4shop.co.uk/user/htdocs/demo88/area/index.php:7) in /home/fhlinux193/a/ahead4shop.co.uk/user/htdocs/demo88/area/addtab.php on line 72 Line 72 of add tab is: header('Location:?id=login'); and Line 7 of index.php is: <style type="text/css"> Thanks Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted December 18, 2007 Author Share Posted December 18, 2007 I checked out that BOM thing and i can't see it. I tried to see it in Notepad++ and Dreamweaver CS3 but nothing, i had the option in dreamweaver to 'include unicode signiture (BOM)' and that was unchecked so i guess that might not be my problem? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2007 Share Posted December 18, 2007 if your having a header function called after the output is sent to the browser it will not work.. it will give an error.. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted December 18, 2007 Author Share Posted December 18, 2007 How can i get around this then? I need to include the redirect as part of the following code, which happens to be midway down addtab.php // Check to see if user is logged in function checkUser(){ if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){ header('Location:?id=login'); } } Is there a way to re-direct the user to a page without using header()? Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted December 18, 2007 Author Share Posted December 18, 2007 bump Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2007 Share Posted December 18, 2007 well if you do not want to re-write or restructure any of your code then you can use ob_start() before session_start that will turn on the output buffer not recommended tho Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 You need to either re-arange your logic so that the header call happens prior to any output (recomended). The output will not be seen if you redirect anyway. Or, (Id'e consider this a hack) place all your output in an output buffer. eg; <?php ob_start(); // rest of code goes here. ob_end_flush(); ?> Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted December 18, 2007 Author Share Posted December 18, 2007 Sorry for being dim, but could you elaborate on this? You need to either re-arange your logic so that the header call happens prior to any output (recomended) My functions are all contained in the file addtab.php and the header() part is aboput a quarter of the way in, how do i rearrange that so that the call happens before an output? <?php function registerUser($user,$pass1,$pass2){ $errorText = ''; // Check passwords if ($pass1 != $pass2) $errorText = "<p class='text'>The passwords you have entered do not match!</p>"; elseif (strlen($pass1) < 6) $errorText = "<p class='text'>Your chosen password is too short. Passwords must be at least 6 characters in length.</p>"; // Check user existance $pfile = fopen("users.txt","a+"); rewind($pfile); while (!feof($pfile)) { $line = fgets($pfile); $tmp = explode('|', $line); if ($tmp[0] == $user) { $errorText = "<p class='text'>The user name you chose has been taken!</p>"; break; } } // Store user data if ($errorText == ''){ // Convert the password to MD5 $userpass = md5($pass1); fwrite($pfile, "\r\n$user|$userpass"); } fclose($pfile); return $errorText; } function loginUser($user,$pass){ $errorText = ''; $validUser = false; // Check user existance $pfile = fopen("users.txt","r"); rewind($pfile); while (!feof($pfile)) { $line = fgets($pfile); $tmp = explode('|', $line); if ($tmp[0] == $user) { // User exists, check password if (trim($tmp[1]) == trim(md5($pass))){ $validUser= true; $_SESSION['userName'] = $user; } break; } } fclose($pfile); if ($validUser != true) $errorText = "<p class='text'>Invalid username or password!</p>"; if ($validUser == true) $_SESSION['validUser'] = true; else $_SESSION['validUser'] = false; return $errorText; } // Logout the user function logoutUser(){ unset($_SESSION['validUser']); unset($_SESSION['userName']); } // Check to see if user is logged in function checkUser(){ if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){ header('Location: ?id=login'); } } //get customer id and add as the key in usertabs.txt function userid($userid){ $file = file("users.txt"); foreach($file as $key => $val){ $data[$key] = explode("|", $val); $userid = $data[$key][0]; } } //add new tabs function addtab($name,$client){ if ($_POST['submit'] == "submit") { $rand = date("dmyhis"); $_SESSION['rand'] = $rand; $file_name = "usercats.txt"; $open_file = fopen($file_name, "a+"); $file_contents= $_POST['client'] . '|' . $_POST['name'] . '|' . $_SESSION['rand'] ."\n"; fwrite($open_file, $file_contents); fclose($open_file); echo "<p class='text'>New Tab successfully uploaded.</p>"; echo"<br \><p class='text'><a href=\"?id=home\">Control Panel</a></p>"; } } //display users tabs function displaytab(){ $lines = file("usercats.txt"); foreach ($lines as $line) { $data = explode("|", $line); if (current($data) == $_SESSION['userName']) { $name = next($data); $url = next($data); $id = next($data); $sitead = "http://localhost/books1/area/index.php"; $_SESSION['id'] = $id; ?> <link href="style.css" rel="stylesheet" type="text/css" /> <table width="237" background="images/tab.png" border="0" cellpadding="1" cellspacing="0" class="main"> <tr> <td width="58"><div align="left"><a href="?id=view&&<?php echo "$id";?>"><?php echo "$name"; ?></a></div></td> <td width="175" class="main"><div align="right"><a href="?id=display"><img src="../images/edit.png" width="25" height="25" border="0" /></a> <a href="?id=del"><img src="../images/del.png" width="25" height="25" border="0" /></a></div></td> </tr> </table> <?php } } } //delete tabs function deletetab(){ $file = file("usertabs.txt"); foreach($file as $key => $val){ $data[$key] = explode("|", $val); if($_GET['rand'] == $data[$key][4]) { unset($file[$key]); $fp = fopen("usertabs.txt", "w"); fwrite($fp, implode('', $file)); fclose($fp); print "Deleted"; } } } //view pages with content function view($id){ $lines = file("aa.txt"); foreach ($lines as $line) { $data[$key] = explode("|", $line); $user = trim($data[$key][0]); $catname = trim($data[$key][1]); $id = trim($data[$key][2]); $_SESSION['id'] = $id; $_POST['id'] = $id; print '<a href="display.php?id='.$_SESSION['id'].'">'.$catname.'</a><br />'; } } //display client list function allclients(){ $filea = file("aa.txt"); foreach($filea as $Keya => $Vala){ $Dataa[$Keya] = explode("|", $Vala); $client = $Dataa[$Keya][0]; print "$client<br />"; } } ?> Quote Link to comment Share on other sites More sharing options...
haku Posted December 18, 2007 Share Posted December 18, 2007 The problem isn't where the header is, as the header is just part of a function. The problem is that wherever you are calling that function, it is after output has already been sent to the browser. Look closely at the location where that function is called, and then look and see what has been outputted to the browser before it is called. edit: on reading back to your earlier post, the offending line is calling for a link to the style sheet. You have to call that function before that line (and any other line that is outputting to the browser before that). It is line 7 - what have you got on lines 1-6? Quote Link to comment Share on other sites More sharing options...
haku Posted December 18, 2007 Share Posted December 18, 2007 I'll add to that: Basically, this should be the very start of index.php: <?php checkUser(); ?> Before even your doctype or anything. Quote Link to comment Share on other sites More sharing options...
chantown Posted December 18, 2007 Share Posted December 18, 2007 according to my experience, the "headers already sent" error comes from you having a white space or a break from your <?php statement Try deleting any whitespaces before your php starting <?php Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 18, 2007 Share Posted December 18, 2007 Not with sessions, with sessions that error stems from him having session information called after the </head> tag. As someone else already mentioned, he has functions after the close of the head tag which need to be called before he closes it. Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 Not with sessions, with sessions that error stems from him having session information called after the </head> tag. As someone else already mentioned, he has functions after the close of the head tag which need to be called before he closes it. It has nothing to do with the html <head> tag, neither do headers. Headers are sent as soon as (actually just prior to) any output is sent to the browser. Headers are also sent when cookies are created on the client and sessions use cookies. Now, once the headers have been sent, you can no longer use the header function to send more. Quote Link to comment 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.