News:

The "Support Requests" forum is now viewable by guests.

Main Menu

URGENT! --- PHP User Management system errors

Started by Jrrwizard, January 29, 2006, 07:02:13 PM

Previous topic - Next topic

Jrrwizard

Hey everyone,

I figure since there is a contest regarding this it can be answered fast. The problem lies in my user system. When I ( or any user) logs in they experience errors, I'm guessing regarding cookies but I'm no good with php :P.

Heres what I get:

Warning: session_register(): Cannot send session cache limiter - headers already sent (output started at /home/jim/public_html/index.php:5) in /home/jim/public_html/mem_index.php on line 6

-In one section and then two more in another (all on same page, just different areas on the page)

Warning: Cannot modify header information - headers already sent by (output started at /home/jim/public_html/index.php:5) in /home/jim/public_html/check_member.php on line 40

Warning: Cannot modify header information - headers already sent by (output started at /home/jim/public_html/index.php:5) in /home/jim/public_html/check_member.php on line 43


Here are the files in question by the errors

Checkmember.php
<?

   include ("admin/authconfig.php");

   if (isset($_COOKIE['LMUSERNAME']) && isset($_COOKIE['LMPASSWORD']))
    {
        // Get values from superglobal variables
        $USERNAME = $_COOKIE['LMUSERNAME'];
        $PASSWORD = $_COOKIE['LMPASSWORD'];

        $CheckSecurity = new auth();
        $check = $CheckSecurity->page_check($USERNAME, $PASSWORD,$dbhost,$dbusername,$dbpass,$dbname);

$connection = mysql_connect($dbhost, $dbusername, $dbpass);
$SelectedDB = mysql_select_db($dbname);
$result0=mysql_query("SELECT signup from authuser where uname='$USERNAME' and reg_validate=1 and status=1");
while($row = mysql_fetch_array($result0, MYSQL_NUM)) {
$v=$row[0];
}
if($v=='1') {
$query = "SELECT access_name FROM authaccess WHERE signup=1";
}
else {
$query = "SELECT access_name FROM memberaccess WHERE uname='$USERNAME'";
}
$result = mysql_query($query) or die("Query failed : " . mysql_error());

//get group_name value
$i=0;
while ($thisrow=mysql_fetch_row($result))  //get one row at a time
{
$gname="access_".$i;
setcookie ($gname, $thisrow[0]);
$i++;
  }
setcookie ("access_num", $i);

mysql_free_result($result);
  mysql_close($connection);

    }
    else
    {
        $check = false;
    }

if ($check == false)
{

echo "<META http-EQUIV='Refresh' content='0; URL=$url_root/login.php'>";
exit;
}


function getip() {
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ip=getenv(HTTP_X_FORWARDED_FOR);
}
else {
$ip=getenv(REMOTE_ADDR);
}
return $ip;
}
?>


Index.php
<HTML>
<HEAD><link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<TITLE>[+] - JimsResource Version 4.0 - [+]</TITLE>
<style type="text/css">
<!--
@import url("style.css");
.style13 {color: #CCCCCC}
.style24 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 9px;
}
-->
</style>
</HEAD>


Please let me know if it could be a problem elsewhere? But it looks like the problem is in the css style and that doesn't make any sense to me. I tried almost everything regarding session starts and all that. Please help me as soon as possible  ;D

Thanks a lot!
-JWiz

brainiac744

You're getting an error because you're trying to start a session after you've already sent data to the browser. There are 2 ways to fix this, either send the session data BEFORE you send output to the browser, or turn on output buffering (http://www.php.net/ob_start).

Jrrwizard

Ok first things first... I have already mentioned that Im hopelessly lost in php haha

Please let me know how I can

a) send the session data BEFORE you send output to the browser

-I think i tried to do this but it didnt work? haha Please tell me exactly where I need to insert coding and what to insert.

b) please explain this output buffering.

- it makes it look like it will alter the effects of my coding? What is the deal with output bufering and how can I use it if it will fix my problem.

brainiac744

Output buffering does precisely what the name implies--it buffers the output before it is sent to the browser. In other words, all of the content is held by the server until all the php has been parsed, thus allowing the session to start even if content has already been echoed.

I can't tell you specifically what to change because I haven't seen all the code. You need to do ob_start() at the beginning of the files, whichever one is called/parsed first, and ob_end_flush() (I think) at the end of the last file that is parsed. Follow that link about output buffering and you'll learn more than you ever wanted to know about it :P.

webzone (archived)

output buffering reduces performance.

the best way is to simply use session_start() at the top of your script (before any content and before using the "echo" statement or the "print" function). make sure that there is no empty line before the beginning of your script and everything should work.

Jrrwizard

Um.. I have tried reading it but it was basically all a blur to me.
It seems like I need a lot more than just a simple start -  end insert in the pages.

If anyone else knows where / what I should put the commands in my coding please reply and help me out.

Jrrwizard

Webzone... I tihnk I tried that before :-/

Where do I need to put session start, what php file?

I tried putting <? session_start() ?> at the top of index.php but it resulted in more problems for me :(

webzone (archived)

I assume that "admin/authconfig.php" is included on every page. Then, add at the very beginning of "admin/authconfig.php" this code : <?php session_start(); ?>

That's all.

Jrrwizard

#8
Um doesn't seem to work either... At the top of authconfig.php I have

<?
session_start();
$dbhost="localhost";
...
...
...
...
and so on


Well... Now Im down to just 2 errors
Warning: Cannot modify header information - headers already sent by (output started at /home/jim/public_html/index.php:12) in /home/jim/public_html/check_member.php on line 40

Warning: Cannot modify header information - headers already sent by (output started at /home/jim/public_html/index.php:12) in /home/jim/public_html/check_member.php on line 43

webzone (archived)

You are probably sending some content to the browser before using the setcookie() function.

Basically, you can never use the header, session_start or setcookie functions after the headers are sent. PHP sends the headers when you use echo or print for the first time, or when you put some HTML code before your script.

Invalid scripts :
<html><?php session_start(); ?> is invalid because the html code forces the headers to be sent before the beginning of the script.

____ (this is an empty line)
<?php session_start(); ?>
is invalid because the empty line forces PHP to send headers.

<?php echo "something";
session_start(); ?>
is invalid because the echo function sends headers.

Jrrwizard

#10
Here is a screenshot of the problem for further help:



As you can see the "user login" section is in question. While it is all located in "index.php"
I have used php dynamic inclusion for the "user login" section, so actually all that is in that box is called up in "form.php" and, once logged in, goes to "mem_welcome.php"

But it seems the problem is either in index or checkmembers? Maybe even authconfig... what I dont understand is why it tells me the problem is in line 12 of index.php which bears no significance (as far as I can see) to the session start command.

Argh, this is driving me crazy

The problem now seems to lie in checkmember.php?
$i=0;
while ($thisrow=mysql_fetch_row($result))  //get one row at a time
{
$gname="access_".$i;
setcookie ($gname, $thisrow[0]);
$i++;
  }
setcookie ("access_num", $i);

mysql_free_result($result);
  mysql_close($connection);

Anything wrong with those lines in conjunction with the rest of the code?

webzone (archived)

In these lines, you are using the setcookie() function. The code as it is contains no errors. However, you have already sent content to the browser. You cannot use setcookie() once you sent content. When PHP reaches this part of the code, he sees that you have not respected that rule and immediately triggers an error.

What you need to do is to make sure, as I already said, that you use setcookie before sending HTML code.

Let's pretend that cookie.php only contains <?php setcookie(); ?> and that index.php only contains <html><body>Welcome!<?php include('cookie.php'); ?>.

See the problem here? When PHP sees the HTML tags, he sends the content to the browser. After this point, it is no longer possible to use setcookie(). Then, after, PHP continues and sees the include(), so he starts to do what is in cookie.php. That triggers an error because cookie.php uses the setcookie function when it is not allowed.

I suspect that the problem is very similar on your site. Please make sure you understand this post, these are the basics of PHP. Once you understood this, you will be able to fix your error by yourself and avoid doing the same mistake later.

Jrrwizard

I completely understand what you are saying, but I cannot see that I have already entered code prior to the cookie.

Oh my god I am on the brink of insanity here... I am so frustrated with this it is not even funny. I have no idea why index.php would even cause a problem, im not setting cookies or session ANYWHERE in it because it is just acting as a template where other pages are being dynamically included in them.

I have no idea what is up with all this cokkie stuff and session, I really need someone to look over my files and solve it for me cause I am completely lost in this php. My head hurts :(

webzone (archived)

Even if index.php does not set cookies himself, he probably includes a file that does. If I well understood, index.php includes checkmember.php. In the code you posted from checkmember, there are two lines that set a cookie. There is your answer. YOu have to make sure that checkmember is included before index.php starts to send content.

brainiac744