Sessions warnings

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

Post Reply
carbine
Forum Newbie
Posts: 6
Joined: Mon Mar 14, 2005 6:37 am

Sessions warnings

Post by carbine »

I am devloped a website using php and I'm currently using sessions to pass php variables between scripts.
The code I have is doing exactly what I want i.e. I can access the value of the variables from one script in another.

Though the problem I'm having is that I'm getting the below warning messages. Any suggestions why I am getting such warnings would be greatly appreciated.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at F:\16182014\Scripts\userHomepage.php:10) in F:\16182014\Scripts\userHomepage.php on line 21

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at F:\16182014\Scripts\userHomepage.php:10) in F:\16182014\Scripts\userHomepage.php on line 21


Script 1

Code: Select all

<?php session_start(); 
echo "$_SESSION[rights]: $_SESSION[firstname] $_SESSION[middlename] $_SESSION[surname]";?>
Script 2

Code: Select all

session_start();
session_register('firstname');
session_register('middlename');
session_register('surname');
session_register('rights');

while($Row = mysql_fetch_row($result))
{  $firstname 	= $Row[0];
    $middlename = $Row[1];
    $surname 	= $Row[2];
    $rights 	= $Row[6];
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

also good to note is session_register is obsolete and requires register globals to be turned on. since you are using Session_Start(), you probably don't need session_register...

so this :

Code: Select all

session_start();
session_register('firstname');
session_register('middlename');
session_register('surname');
session_register('rights');
//.....
needs to actually be

Code: Select all

session_start();
while($Row = mysql_fetch_row($result))
{
	$_SESSION['firstname'] = $Row[0];
	$_SESSION['middlename'] = $Row[1];
	$_SESSION['surname'] = $Row[2];
	$_SESSION['rights'] = $Row[6];}
}

You can read more on this subject at :
http://us4.php.net/session_register

Please note the Cautions
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().
Post Reply