Stupid Errors with session_start()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Stupid Errors with session_start()

Post by dimitris »

I used sessions in my php code and i get the following error:

Warning: Cannot send session cookie - headers already sent by (output started at /home/hxotpon.net/w/connect.inc:9) in /home/xxxxx.net/www/music/index.php on line 15

Warning: Cannot send session cache limiter - headers already sent (output started at /home/hxotpon.net/w/connect.inc:9) in /home/xxxxx.net/www/music/index.php on line 15

Warning: Cannot add header information - headers already sent by (output started at /home/hxotpon.net/w/connect.inc:9) in /home/xxxxx.net/www/music/index.php on line 16

Let me explain myself:
There is not any output before and my code complies with any suggestion in other forums/post .
I tried to run any tutorial with sessions and i get the same errors so i believe that the problem is in the server. Php.ini or php compilation is the key.
Can anyone suggest any misconfiguration that disallows proper use of sessions?
Also can anyone send me his php.ini inorder to compare with mine!
Thanx guys!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There is output before and it is in connect.inc on line 9 - what are the first 10 lines of connect.inc?

Mac
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Reply:

Post by dimitris »

twigletmac wrote:There is output before and it is in connect.inc on line 9 - what are the first 10 lines of connect.inc?

Mac
You are right i write:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
</head>

<body bgcolor="#FFFFFF" text="#000000">

<?php
function connect_db() {...

I have 2 blank lines, i will fix it. Do you see anything else which seems wrong?

The rest of the code is elements for connection to the db

On the other hand, why all the tutorials about sessions i tested made the same errors despite the fact they didn't connect to connect.inc?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

This is output:

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
</head>

<body bgcolor="#FFFFFF" text="#000000">
you have to have session_start() before any output is sent to the browser, output is HTML, javascript, blank lines, spaces before the opening <?php tag, etc.

Mac
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

twigletmac wrote:This is output:

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
</head>

<body bgcolor="#FFFFFF" text="#000000">
you have to have session_start() before any output is sent to the browser, output is HTML, javascript, blank lines, spaces before the opening <?php tag, etc.

Mac
You are right, i tested with <?php ?> before <html><title>...
and it works.

Thanx for your time Mac

The Conclusion for me is:
The rule is to write <?php ?>
with sessions related code before and outside the html area.
Then sessions are passed automatically to another page when i hit a link on that page.
If i'm wrong to anything just correct me!
Last edited by dimitris on Wed Jan 14, 2004 5:27 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

dimitris wrote:You are right, i tested with <?php ?> before <html><title>...
and it works
That's good.
dimitris wrote:So the rule is to write <?php ?> with sessions related code before and outside the html area
You got it.
dimitris wrote:Then sessions are passed automatically to another page when i hit a link?
Exactly, although if you want to access anything from the session, you'll need session_start() in the next page too.

Mac
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

twigletmac wrote:
dimitris wrote:You are right, i tested with <?php ?> before <html><title>...
and it works
That's good.
dimitris wrote:So the rule is to write <?php ?> with sessions related code before and outside the html area
You got it.
dimitris wrote:Then sessions are passed automatically to another page when i hit a link?
Exactly, although if you want to access anything from the session, you'll need session_start() in the next page too.

Mac
Yes, i always use session_start()

I have the very idea that i must write something before a link such as:
<? echo $_SESSION['session_id']; ?> or <? echo $_SESSION['log_status']; ?> in order to pass session values such as session id,username,password OR it is absolutely automatically and you have only to handle them?

Thanks a lot

ADDED NOW:Problem->Solution->Problem
Sorry Merfy's law just occured!
After running the enhanced script other php scripts in the same page (requesting 5 last records from a database) return now die message!
Is there any conflict with the change?
I mention that after the login form is submitted, I use
action="<?php echo $PHP_SELF ?>"
Last edited by dimitris on Wed Jan 14, 2004 5:54 am, edited 2 times in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You shouldn't have to add session ids automatically. Best thing you can do is remove that from your URLs and test the links to see what happens - learning by doing :).

Mac
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

twigletmac wrote:You shouldn't have to add session ids automatically. Best thing you can do is remove that from your URLs and test the links to see what happens - learning by doing :).

Mac
That's right!I will make some tests until i hit my target!
Learning by doing! :wink:
I will check some tutorials inorder to be more confident with what is going on with sessions...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

dimitris wrote:ADDED NOW:Problem->Solution->Problem
Sorry Merfy's law just occured!
After running the enhanced script other php scripts in the same page (requesting 5 last records from a database) return now die message!
Is there any conflict with the change?
I mention that after the login form is submitted, I use
action="<?php echo $PHP_SELF ?>"
What's your error message?

Mac
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post by basdog22 »

<?php echo $PHP_SELF ?>
maybe this: ;?????

Code: Select all

<?php echo $PHP_SELF; ?>

Are elladara :wink:
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

twigletmac wrote:
dimitris wrote:ADDED NOW:Problem->Solution->Problem
Sorry Merfy's law just occured!
After running the enhanced script other php scripts in the same page (requesting 5 last records from a database) return now die message!
Is there any conflict with the change?
I mention that after the login form is submitted, I use
action="<?php echo $PHP_SELF ?>"
What's your error message?

Mac
Well that's my code:
<?php require_once ("../../w/connect.inc");
error_reporting(E_ALL); //<? echo $_SESSION['session_id'];
$query4ma = "SELECT id_diskou,title,hxotpon_prod FROM diskoi WHERE hxotpon_prod='Yes' ORDER BY id_diskou DESC ";
$result4ma = mysql_query ($query4ma) or die("Query failed!");
function displayMagsa($result4ma)
{
$a=1;
while(($row = @ mysql_fetch_array($result4ma)) AND ($a<6))
{
printf("<a href="view_each_disko.php?id=%s"<font color="#000000" size="2" face="Verdana">%s. %s </font></a><br>",$row['id_diskou'],$a,$row['title']);
$a++;
}
}
displayMagsa($result4ma);
?>

So I get the error: Query failed , whereas I could see the last 5 records.
That was working normally in the same page before the login form is submitted!
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

basdog22 wrote:
<?php echo $PHP_SELF ?>
maybe this: ;?????

Code: Select all

<?php echo $PHP_SELF; ?>

Are elladara :wink:
Well! I tried it but the same thing happens again!
I thing that there is no use for ; in that statement!

Ela re patrioti! :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The semi-colon is not necessary but is good to add every time (cuts down on parse errors).

$PHP_SELF should be $_SERVER['PHP_SELF'] as $PHP_SELF is deprecated.

So that we can narrow down the problem a bit better, could you replace:

Code: Select all

$result4ma = mysql_query($query4ma) or die('Query failed!');
with

Code: Select all

$result4ma = mysql_query($query4ma) or die('Query failed!'.mysql_error().'<p>'.$query4ma.'</p>');
and then post the error message you get.

Mac
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

Thats the error I get:
Query failed!No Database Selected

SELECT id_diskou,title,hxotpon_prod FROM diskoi WHERE hxotpon_prod='Yes' ORDER BY id_diskou DESC
No Database?That means that require_once('connect.inc'); fails!
Post Reply