Page 1 of 1

Frustrated with sessions

Posted: Wed Jan 07, 2004 7:39 am
by voodoo9055
I think I try everything. I just can't get a session variables to pass to the next page.

I am currently using PHP 4.3.3.

Here are my session settings from phpinfo().
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path C:\PHP\sessiondata C:\PHP\sessiondata
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off
I even set my browser to accept all cookies. Still no session on the next page.

page 1

Code: Select all

session_start();

$_SESSIONї'user'] = 1;
page 2

Code: Select all

session_start();

echo $_SESSIONї'user'];
I noticed the sessions are showing up in the sessiondata folder. From page one, they are always 1K. On page two, they are always 0K.

Any help will be appreciated.

Posted: Wed Jan 07, 2004 7:42 am
by malcolmboston
okay ill have a go at a possible fix (this is what i use anyway)

say u have a login form

username is a field and password also

Code: Select all

<?php
session_start();
$_SESSION['user'] = $_POST['username'];
?>
then to retrieve on PAGE 2!

Code: Select all

<?php
session_start();
print $_SESSION['user'];
?>
hope that helps

Posted: Wed Jan 07, 2004 3:01 pm
by voodoo9055
Still no luck.

Posted: Wed Jan 07, 2004 3:10 pm
by voodoo9055
Update:

For some reason, I managed to get it to work by placing

Code: Select all

ob_start();
at the top of the page. Could someone explain why this would cause this to work?

Posted: Wed Jan 07, 2004 4:09 pm
by scorphus
Just a question: are you using Apache? What version?

Thanks,
Scorphus.

Posted: Wed Jan 07, 2004 5:38 pm
by Gen-ik
The best thing to do is to check out the PHP manual (try this... [php_man]session[/php_man]) because you can use sessions if you have register_globals on or off. The only difference is the way you read/write the session variables.

For example...
With register_globals turned on you can set a session variable simply using $_SESSION["name"] = "bob" but with register_globals turned off you do session_register("name"); $name = "bob"

Read the manual and all will be revealed :)

Posted: Wed Jan 07, 2004 5:53 pm
by JAM
Gen-ik means the other way around;

Code: Select all

<pre>
<?php
    session_start();
    $_SESSION['foo'] = 'bar';
    echo 'Register Globals: '.(ini_get('register_globals') == '' ? 'Off' : 'On')."\n";
    print_r($_SESSION);
?>
Result:

Code: Select all

Register Globals: Off
Array
(
    &#1111;foo] =&gt; bar
)
ob_start, very non technicaly described, is that it stores the script and sends everything that needs to be sendt first (session_start(), header() etc.) before the other output.
So if that works for you, you likely have session_start after a newline (might be before the <?php tag), have text/output before it or similiar.

Check that the page you are using really begins as:

Code: Select all

<?php
    session_start();
...

Posted: Wed Jan 07, 2004 8:32 pm
by Gen-ik
JAM wrote:Gen-ik means the other way around
Doh! Oh well... it's one way or the other ;)

Posted: Thu Jan 08, 2004 11:30 am
by voodoo9055
Maybe I wasn't clear.

Before my pages where basically...

Code: Select all

<?php
session_start();

...some code

echo $_SESSION['user'];
?>
and it didn't work. Now it is ..........

Code: Select all

<?php
ob_start();
session_start();

....some code

echo $_SESSION['user'];
?>
and now it works.

I will read more on ob_start() since I am not familar with it, but I was under the impression that I should be able to only use session_start();. It is working locally so I am not complaining. :wink: Registered globals were already off BTW.

Posted: Thu Jan 08, 2004 5:58 pm
by JAM
You where clear enough, I was just making sure.

Now I'm just intrigued to know what makes this fubar. I know that I have read about a similiar issue somewhere, but . I . cant . remember . where...

If you (or we or anyone) find a solution, please post it for future usage.