Jump to content

[SOLVED] Quess about session_start()?


JJohnsenDK

Recommended Posts

Hey

Im trying to use the function session_start() but it doesnt seems to work. I get these Warnings:


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Programmer\xampp\xampp\htdocs\test\count.php:9) in C:\Programmer\xampp\xampp\htdocs\test\count.php on line 10

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Programmer\xampp\xampp\htdocs\test\count.php:9) in C:\Programmer\xampp\xampp\htdocs\test\count.php on line 10
Du har besøgt denne side 1 gange.
Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

I run the script at a PHP 5 server. Anyone who can tell me whats wrong?
Link to comment
https://forums.phpfreaks.com/topic/31960-solved-quess-about-session_start/
Share on other sites

Typically, session_start() is going to be one of the very first things on the page. You [b]cannot[/b] call that function after any header information is output by the script, whether that be actual HTML or other information. This is one of the things your errors are pointing out.
Allright, i got the first fixed, thanks for that obsidian, but i still have problems with the last warning:

Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

Anyone who can help me out with this?
Okay thanks... I think i have problems with my server setup cause i cant get sessions to work propperly.

I got these to script where track_prefs1.php should pass on the sessions to track_prefs2:

[i]track_prefs1.php[/i]

[code]
<?
session_start();
session_register("first");
session_register("last");
session_register("email");
session_register("news_prefs");
?>

<head>
<title>Velkommen</title>
</head>
<style type="text/css">
p, ul, h3 {font-family:Verdana, Arial, Helvetica, sans-serif;}
.enabled {font-weight: bold; color: green;}
.disabled {font-weight: bold; color: red;}
</style>
</head>
<body>
<?
function load_user_data(){
global $first, $last, $email, $news_prefs;
$first = "Karen";
$last = "Hansen";
$email = "[email protected]";
$news_prefs = array(
"Politik" => 0,
"Familie" => 1,
"Børn" => 1,
"Tegneserier" => 0,
);
}
load_user_data();
?>
<h3>Velkommen</h3>
<p>Velkommen tilbage <b><?= $first ?></b></p>
<p>Dine indstillinger er indstillet.</p>
<p><a href=track_prefs2.php>Se oversigt over dine valg</a></p>
</body>
</html>

[/code]

[i]track_prefs2.php[/i]

[code]
<?
session_start()
?>
<html>
<head>
<title>Indstillinger</title>
<style type="text/css">
p, ul, h3 {font-family:Verdana, Arial, Helvetica, sans-serif;}
.ja {font-weight: bold; color: green;}
.nej {font-weight: bold; color: red;}
</style>
</head>
<body>
<h3>Indstillinger</h3>
<p>Hej <b><?=$first?> <?=$last?></b></p>
<p>Email: <?=$email?></p>
<p>Dine valg:<ul>
<?
while(list($key, $value) = each($news_prefs)){
if($value){
$value = "Ja";
}else{
$value = "Nej";
}
print ("<li class=$value>$key: $value</li>");
}
?>
</ul>
</p>
</body>
</html>
[/code]

When i click the link i get this error insted of the array list:
[i]Warning: Variable passed to each() is not an array or object in C:\Programmer\xampp\xampp\htdocs\test\track_prefs2.php on line 19[/i]

Because it says that the variable isnt a array i assume that the variable from track_prefs1.php isnt passed on to track_prefs2.php or am i wrong?
You've made the variables in the first script 'global'.  They are still only available in the first script.  To make a variable part of a session, you would need to define it like this:

$_SESSion['first']
$_SESSION['news_prefs']
etc.

Session variables are, by definition, super globals so you don't need to specify global when you define them this way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.