View Full Version : Session problem
gruszczy
December 20th, 2005, 11:58 PM
I have some problems with sessions in php. This is the message I receive:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/usos/logowanie.php:4) in /var/www/usos/logowanie.php on line 13
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /var/www/usos/logowanie.php:4) in /var/www/usos/logowanie.php on line 13
The php code looks likes this:
<?php
$database = pg_connect("host=localhost dbname=bdlab user= password=");
$login = $_POST['login'];
$password = $_POST['password'];
$query = "SELECT zaloguj('$login','$password');";
$query = pg_query($database, $query);
$result = pg_fetch_row($query);
pg_close($database);
if ($result[0] != -1){
session_start();
}
if ($result[0] == 1)
$typ = "student";
// echo '<meta http-equiv="refresh" content="20 URL=index.php">';
if ($result[0] == 10)
$typ = "administrator";
// echo '<meta http-equiv="refresh" content="0 URL=panel_administratora.php">';
?>
Can anyone help me and tell, what's the problem? I would really appreciate some help.. I cannot start session, and can't register any session variables.
tbrownaw
December 21st, 2005, 03:09 PM
Is there anything before the <?php tag? (Including whitespace, etc.) The '<' character in the '<?php' should be the first character in the file.
sapo
December 21st, 2005, 03:25 PM
You have the wrong idea about sessions dude..
You should aways start the session at the beginning of the file.. to avoid problems.. try something like it:
<?php
session_start();
if($_POST) {
$database = pg_connect("host=localhost dbname=bdlab user= password=");
$login = addslashes($_POST['login']);
$password = addslashes($_POST['password']);
$query = "SELECT zaloguj('$login','$password');";
$query = pg_query($database, $query);
$result = pg_fetch_row($query);
pg_close($database);
if ($result[0] != -1){
$_SESSION['whatever'] = true;
}
if ($result[0] == 1) {
$_SESSION['typ'] = "student";
header("Location: another.php");
}
if ($result[0] == 10) {
$_SESSION['typ'] = "administrator";
header("Location: forthehooooooooorde.php");
}
} else {
die("Error!");
}
?>
And in the other page use something like this:
<?
session_start();
if($_SESSION['typ'] == "admininistrator") {
echo "You are the admin dude!";
} elseif($_SESSION['typ'] == "student") {
echo "You are a student!"
} else {
echo "You are NOTHING!";
}
?>
And dont use meta-refresh, its ugly =x
gruszczy
December 23rd, 2005, 03:25 PM
I went home for christmas, but as soon as I get to my other home I'll try it. Thanks for your help.
gruszczy
January 3rd, 2006, 08:26 PM
Well, it seems that you are perfectly right - I just didn't understand how the session works. Thanks a lot, I'm step closer to the end ;)
NetCutter
August 22nd, 2006, 12:51 PM
Hi,
I have one problem with the php session!
They just does not works!
Look: These 2 scripts does not works!!!
<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
?>
<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
unset($_SESSION['count']);
?>
indytim
August 23rd, 2006, 07:25 AM
And the error message is..... (besides just doesn't work)??
IndyTim
NetCutter
August 23rd, 2006, 07:50 AM
No error message!Just the session doesn't work!
Look this code:
Script1.php:
<?php
session_start();
$_SESSION['color']="blue";
?>
<a href="script2.php">Go to Script2.php</a>
script2.php:
<?php
session_start();
if(isset($_SESSION['color']))
echo "Value of color is: ".$_SESSION['color'];
else echo "Color is NOT set";
?>
And the browser give me:
Color is NOT set:(
indytim
August 23rd, 2006, 10:58 AM
I'm not sure what variables are depreciated in php5 (if you're using). Try something like the following to get you started
session_start();
$color="blue";
session_register("color");
call your next program
<?PHP
session_start;
print '<br> color='.$color;
?>
indytim
August 23rd, 2006, 12:51 PM
second program should be:
<?PHP
session_start();
blah and blah
Sorry for the creeping digititus!
NetCutter
August 23rd, 2006, 02:09 PM
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
NetCutter
August 26th, 2006, 12:50 PM
So?Can somebody help me?
ifokkema
August 26th, 2006, 06:21 PM
Hi,
I have one problem with the php session!
They just does not works!
Look: These 2 scripts does not works!!!
<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
?>
<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
unset($_SESSION['count']);
?>
These scripts run fine on my install. Seems like your configuration is not working properly. Try running a single script, putting something in $_SESSION and incrementing that. Refresh that page and use a var_dump on $_SESSION to see what you've got.
hagen00
August 27th, 2006, 07:22 AM
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
Don't worry about that. That's just a warning. It says your script possibly relies on a session side-effect. But it doesn't. Turn the warning off by editing php.ini by (like the message says) setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. Then restart apache.
ifokkema
August 27th, 2006, 08:23 AM
The original script didn't create the warning, because it wasn't using session_register(). I always just use the $_SESSION array. No idea though what the pros and cons are of using $_SESSION above session_register() and such.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.