Not setting session

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
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Not setting session

Post by pinehead18 »

<?php
// Cookie testing page

if ($submit) {
session_start();
if(!isset($_SESSION['vars'])) {
$vars['pic'] = "pic";
}
}
?>
<form method=post action=sessions.php>
<input type=submit name=submit value='set session'>
</form>

--------------------------------------------------------------------------------



This is suppose to set a session.. However it does not .. Any suggestions?



PHP:
--------------------------------------------------------------------------------

<?PHP

session_start();
$vars = $_SESSION['vars'];
$whatever = $vars['pic'];

?>

--------------------------------------------------------------------------------



That is session2.php where i view the session that the script above created.

Thank you for any help you can provide.

- Anthony
amarquis
Forum Newbie
Posts: 10
Joined: Fri Dec 19, 2003 3:53 pm
Location: Switzerland

Post by amarquis »

session_start(); needs to stand on your page before any other header... this means that session_start(); must be called before any headers are sent out !

If the above script is included in another page it's very likeley that the other page already sent headers out - so you should call session_start() there...

You can also look at this post that helped me to understand how it works with forms... :wink:

http://www.phpb.com/forums/read.php?f=6&i=168&t=161

Hope this helps...
Post Reply