Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Configuring for Sessions

Status
Not open for further replies.

jdaniels

Technical User
Apr 23, 2002
63
GB
Hi,

I am running PHP4 (the latest stable release) on Windows ME with Apache 1.3.22 (Win). It works OK except for sessions. When I create a session, it gives the following errors in the browser:
===============================
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at c:\program files\apache group\apache\htdocs\session.php:7) in c:\program files\apache group\apache\htdocs\session.php on line 9

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at c:\program files\apache group\apache\htdocs\session.php:7) in c:\program files\apache group\apache\htdocs\session.php on line 9

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
===============================

The code that I use is this:
==============================
<html>
<head>
<title>Session Test</title>
</head>
<body>

<?php

session_start();

$thisVar = &quot;test&quot;;

session_register(&quot;thisVar&quot;);

?>

Variable: '<?php echo $_SESSION['thisVar']; ?>'

</body>
</html>
==============================

I am guessing I need to set a configuration flag, but where? httpd.conf? .htaccess?

Thanks,

Jonathan Daniels
 
OK - I put the session creation code in front of the opening HTML tag - that got rid of the 2 errors relating to 'session_start'. Thankyou for that. However I cannot actually see the session variable; if I try to display it I get a blank. Also the last error is still there:

=======================================
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
=======================================

Any ideas where I set 'register_globals' ?

Thanks
 
Solved! For some reason my browser ditched the first 2 errors, then waited a while to ditch the last and show the variable. Cheers for your help.
 
jdaniels:

One thing to be aware of....

The PHP online manual states: If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister()

I recommend using $_SESSION, because it does not make your code dependent on the setting of register_globals, and it makes your code more maintainable.

After issuing session_start(), just use the line &quot;$_SESSION['thekey'] = thevalue&quot;. The session variable will be automatically registered. You can then reference the value in any other script after issuing session_start(). Use something like array_splice() to remove the key and value from the array, effectively unregistering it. Want the best answers? Ask the best questions: TANSTAAFL!
 
OK I was just going to ask about that. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top