Frustrated with sessions

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
User avatar
voodoo9055
Forum Commoner
Posts: 51
Joined: Sat Apr 26, 2003 3:27 pm
Location: Montgomery, AL

Frustrated with sessions

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
voodoo9055
Forum Commoner
Posts: 51
Joined: Sat Apr 26, 2003 3:27 pm
Location: Montgomery, AL

Post by voodoo9055 »

Still no luck.
User avatar
voodoo9055
Forum Commoner
Posts: 51
Joined: Sat Apr 26, 2003 3:27 pm
Location: Montgomery, AL

Post 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?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Just a question: are you using Apache? What version?

Thanks,
Scorphus.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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();
...
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

JAM wrote:Gen-ik means the other way around
Doh! Oh well... it's one way or the other ;)
User avatar
voodoo9055
Forum Commoner
Posts: 51
Joined: Sat Apr 26, 2003 3:27 pm
Location: Montgomery, AL

Post 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.
Last edited by voodoo9055 on Thu Jan 08, 2004 11:11 pm, edited 1 time in total.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Post Reply