Log in

View Full Version : session_start() error



hyde360
07-21-2009, 04:40 AM
Hello,

Right, I got two php files one is the main page that shows everything within a layout and one file called (functions.php) but the problem is I'm including a session.php which I use to check users online/offline and many many other things.

Although, my function object "userBar();" won't recongise the "$session" variable that's in my session.php file, however I've tried including the session.php file within my function object but it throws this error at me:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /path/to/main/mypage.php:8) in /path/to/my/session.php on line 48

Which I know what that means, that a session_start() must be performed before any html is outputted which I have done so but for some reason it's still throwing this evil message at me...

Here's what my function file looks like:



<?php

#debugging purposes only

# error_reporting(E_ALL);

//Xbox Daily Function File

// $version : 1.1.0
// $filename : functions.php
// $website : www.xboxdaily.co.uk

//Let"s create the userBar object

//We need to define two vars for our userBar() object.

function userBar() {

require $_SERVER['DOCUMENT_ROOT']."/users/mk/session.php";

if (!$session->logged_in) {

echo '<img alt="not logged in" src="img/user_silhouette.png" /> Welcome <b>Guest</b>, please <a title="login" href="".login_url."">login</a> or <a title="register" href="".register_url."">register</a>.';

} else {

//If they are logged in, then display a nice little welcome note.

echo '
<img alt="logged in" src="img/user.png" /> Welcome back <b>$session->username</b>! <img alt="Community Image" height="16" src="img/page_white_stack.png" width="16" />
<a title="My Community" href="".community_url."/$session->username.html">My Community</a> |
<img alt="Account Image" height="16" src="img/dashboard.png" width="16" />
<a title="My Dashboard" href="".dashboard_url."">My Dashboard</a> |
<img alt="padlock" height="16" src="img/lock_unlock.png" width="16" />
<a title="Sign me out" href="".signout_url."">Sign me out</a> |
<img alt="Pm" height="16" src="img/envelope.png" width="16" />
New messages (undefined)
';

}

}

?>


Before any meantioned about the constants that are not in the script above, I removed them to see if it would solve this issue (they in the script)

And on the page that is including this file looks like this:

...Won't post full script as it's huge :)



<?php include("functions.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-gb" http-equiv="Content-Language" />
<meta content="text/html; charset=windows-1252" http-equiv="Content-Type" />
<meta name="description" content="XboxDaily.co.uk - Your daily resource for Xbox 360 Games, Xbox 360 Videos, Xbox 360 Previews, and much more!!!" />
<meta name="keywords" content="xbox, microsoft, xbox 360, microsoft xbox, microsoft xbox 360, 360, my xbox, game, gaming, games, videos, daily, MS, daily gaming" />
<meta name="author" content="Xbox Daily, Visual Blue" />
<meta name="revised" content="08/02/2009" />
<title>:: Xbox Daily :: Your daily resource for Xbox 360 Games, Xbox 360 Videos,
Xbox 360 Previews...</title>


Also the main code above it's nothing major and doesn't include any other php scripts apart Database connections etc.

If anyone can help shine a light on what's causing this issue, I would appreciate it so much :D

Thanks in advance!

traq
07-21-2009, 04:50 AM
well, you don't have "session_start();" written anywhere that I can see, for one.
Try putting it here:


<?php session_start();
include("functions.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-gb" http-equiv="Content-Language" />
<meta content="text/html; charset=windows-1252" http-equiv="Content-Type" />
<meta name="description" content="XboxDaily.co.uk - Your daily resource for Xbox 360 Games, Xbox 360 Videos, Xbox 360 Previews, and much more!!!" />
<meta name="keywords" content="xbox, microsoft, xbox 360, microsoft xbox, microsoft xbox 360, 360, my xbox, game, gaming, games, videos, daily, MS, daily gaming" />
<meta name="author" content="Xbox Daily, Visual Blue" />
<meta name="revised" content="08/02/2009" />
<title>:: Xbox Daily :: Your daily resource for Xbox 360 Games, Xbox 360 Videos,
Xbox 360 Previews...</title>

hyde360
07-21-2009, 04:55 AM
Oh sorry, I forgot to meantion that part session_start() is within "session.php"... Sorry if I caused any confusen there :p

traq
07-21-2009, 05:26 AM
I would still suggest the above placement. By putting it in "session.php", you've got all this


#debugging purposes only

# error_reporting(E_ALL);

//Xbox Daily Function File

// $version : 1.1.0
// $filename : functions.php
// $website : www.xboxdaily.co.uk

//Let"s create the userBar object

//We need to define two vars for our userBar() object.

function userBar() {

require $_SERVER['DOCUMENT_ROOT']."/users/mk/session.php";
going before. "session_start();" needs to be the very first thing parsed by the server, or you'll get the "headers already sent" error you described above.

hyde360
07-21-2009, 05:51 AM
Right, I've fixed the main issue with session_start(); however now I got the issue of this:

Notice: Undefined variable: session in /path/to/functions.php

Which is the part on the script:



<?php
if (!$session->logged_in){
echo "sign in now!!!";
} else {
echo "hello $session->username";
}
?>


How would I get my function to get information from a include, as of what I've done is now placed the include at the very top of the functions.php file like this:



<?php
include $_SERVER['DOCUMENT_ROOT']."/path/to/session.php";
?>
<?php

//functions file

?>


Any ideas?

JasonDFR
07-29-2009, 10:26 AM
The $session variable inside your function is a local variable. Meaning that it only exists inside the function.

You have a couple options. In place of $session you can use $_SESSION[] (the php global session array). This is probably the most common way.

Or you could pass the session array or object to your userbar() function as an argument.

Whatever you do, never start a session inside of a function without first checking if a session exists. Better yet, do not start sessions inside functions at all. It is not a good design practice.