Link to home
Start Free TrialLog in
Avatar of debbieau1
debbieau1Flag for United States of America

asked on

Session variable values not passing to the next page

I have a problem with session variables from a form field to another page.  I use Dreamweaver PHP mysql and extensions Impakt.  I find the session variable are not passed to the next page.  However, if I submit and don't fill in all the "Required" fields in the form, and then fill in the fields and resubmit the page, then it does actually work.  
 
I have<?php session_start();?> at the the top of each page


my form field is called 'contact'  and I am using this

<?php $contact=$_SESSION['ss_contact']; $_SESSION['ss_contact'] = $_POST['contact'];?>.

This code is located just after the </form> tag  I have tried moving it to other locations but have not had any luck.

Thank you
Avatar of KvdnBerg
KvdnBerg

The problem here might be that the session id is not being passed. From the manual:  If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser.
You can check the session settings in the phpinfo page, which you can get by saving a file with just:
<? phpinfo(); ?>
Look for session.use_cookies, see if it's on. If not, it's possible you have to pass the session id with the pages. You can find more info on that here: http://nl3.php.net/manual/en/ref.session.php#session.idpassing
Avatar of debbieau1

ASKER

I have<?php session_start();?> at the the top of each page.  I think that is what you mean?
no, not exactly, as it states in the manual : session_start() creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie.
So if the session id is not passed in some way, it cannot continue the session and it will start a new one.
Hello,

If you browser does not currently accept cookies, your script won't work as expected.
You need in this case to set php option session.use_trand_sid to on, for example in a .htaccess file in your directory:
php_flag session.use_trans_sid on

this will add session variable in all your links/forms.

Regards,
Ben
I have gone from bad to worse!  Can someone please help.  I have not had a chance to check the php.ini, but I think those settings are not set in our php.ini from memory, so thank you for the last suggestion.  I will look at the shortly.

I made some changes though as per the earlier suggestion link and it is giving me much more problems, than I had before.  Remember I am using extensions with Dreamweaver.  How do I fix this?

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/test/bookdetails.php:6) in /var/www/html/includes/functions.inc.php on line 232

I have increased the points, because I need this solved urgently.  I someone can help.  I need to fix this without it interfering with my extensions.

Deb
I you get this error that's because you did NOT put session_start() at the BEGINNING of your script ! ;)

Ben
session_start() was actually in both files.  I checked.   I ended up resolving it by just undo last actions, till I got both pages back to where they were before the error was produced.  But both files had the session_start in there.  

I am hoping you have the solved my initial problem with your previous answer though thanks.
can you post your code?
Debbie, the error you got happens when something is outputted before the headers. This sometimes means just more headers. In this case I think functions.inc.php tried to add headers even though you already did session_start(); in bookdetails.php Perhaps if you could tell us what functions.inc.php is, we'd be able to figure it out.
Thank you very much for the further input.  the functions.inc.php is a file generated from a Dreamweaver extension. I have resolved the problem I had with the headers for now and would am interested to know, but this is posting is probably getting to long now. However, The most important thing for me is to resolve my inital question re the session variable from not passing to the page, without the submit pressed, as per mentioned initially.

Re posting the code, there is quite a bit in the file, and I think it is too long to post here.  I was hoping that I had posted the most relevant code.  I am really grateful for all the assistance and will be glad to have this solved.  I cannot check the php.ini file as Ben's suggestion yet, because our tech guy is not in today, will tomorrow though.
The thing that confuses me is I seem to have a real hit and miss with success with session variables.  I'm pretty sure I am entering it correctly, because sometimes it works, and sometimes it doesn't and I have followed the guildeines from the php books.  I am not sure if my problems are to do with the dreamweaver extensions, or where I actually place the session code which stores the session value.  I always enter session_start() at the beginning of the page.  I am hoping the mods to php.ini will resolve it, unless there is some other likely reason it won't work.
Like I've said before, you can check the php ini settings by simply placing a file on the server containing the following

<?
phpinfo();
?>

I usually call this file phpinfo.php
Simply place it on the server and call it from the browser and you will get a page containing a lot of information on the php configuration on your server, including the above mentioned session settings.

Please check  the following:

session.use_cookies
session.use_trans_sid      

And let us know if these are set to yes or no, and then maybe we can help you further.
i encountered the same problem before... what i did is put something like check whether the form is submitted then check for the session variable value if it is set.. do you have this kind of code in your script that checks whether the form is submitted or not?

You guys are fantastic.  I have posted i think the most relevant stuff from the (from the phpinfo()).  Would you mind checking to see if there is anything not right here.    Also, Would it be possible to post the code to check with this form is submitted or not as you mentioned.  I find it difficult using extensions as they generate a lot more code, and makes it more difficult to debug.
PHPINFO
session.auto_start  Off Off , 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_maxlifetime  3600 3600, session.gc_probability  1 1 , session.name  PHPSESSID PHPSESSID , session.referer_check  no value no value , session.save_handler  files files , session.save_path
 /tmp /tmp , session.serialize_handler  php php, session.use_cookies  On On, session.use_trans_sid  1 1

thanks again.  
Go ahead and post the code, I'll try it out and see what happens
session cookies are on so that shouldn't be a problem
What you could try to see if a cookie is set at all is change your browser settings to ask for every cookie to be manually accepted (in IE : Tools->Internet Options, Privacy, Advanced, set both cookie settings to "Prompt")
Try this,

<?php session_start();?>

<?php
if (! isset($_SESSION['ss_contact'])) {
    $_SESSION['ss_contact'] = $_POST['contact'];
}
?>

if works fine, else check the following

1.If you enable register_globals in php.ini file, session_unregister() should be used.
2.If you are using $_SESSION and disable register_globals.
3.There is a defect in PHP 4.2.3 and earlier. If you register a new session variable by using session_register(), the entry in the global scope and the $_SESSION entry will not reference the same value .

Regards

suresh
 
Avatar of Bernard Savonet
You might consider displaying debug info with something like
<?php session_start; // we always need the first anyway
$debug = 1 ; // change to 0 when not debugging
if ($debug == 1 ) {
   print '<pre>';
   var_dump($_SESSION);
   print '</pre>';
};
?>
THis will ruin your page but will show you everithing that is available in the $_SESSION array.
While you are at this, you might consider going the full debug route and use in the {} something like:
   print "Debug Info : SESSION<br>\n";
   print '<pre>';
   var_dump($_SESSION);
   print '</pre><br>';

   print "Debug Info : POST<br>\n";
   print '<pre>';
   var_dump($_POST);
   print '</pre><br>';

   print "Debug Info : GET<br>\n";
   print '<pre>';
   var_dump($_GET);
   print '</pre>';

This should clarify what is really happening.
Also, pleas check that you are not using Zone Alarm.... this would create some problems
... an afterthought...
You said that your code was AFTER the </form> instruction...
Why?
As was pointed before, this would certainly better near the top of the page...
Thanks again for the further comments.  Re why I had put the code after the </form>, was just lack of understanding of sessions.  I knew session_start() had to be at the top of the page, but didn't realise the other so that will help.  Also, I am using Version 4.2.2  and registered globals is enabled.  I also noticed the functions.inc.php does have quite a bit of code to do with the sessions.    The pages really are far too long to post here, with being extensions they generate a lot more code.  Could you tell me where I need to put session_unregister().  I am hoping that might fix it.

Thank you so much

ASKER CERTIFIED SOLUTION
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial