Jump to content

Warning: session_start()..


Schlo_50

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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()?

Link to comment
Share on other sites

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();

?>

Link to comment
Share on other sites

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 />"; 

}
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.