Solved

Session variable values not passing to the next page

Posted on 2004-10-27
267 Views
Last Modified: 2008-03-04
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
0
Question by:debbieau1
    19 Comments
     
    LVL 3

    Expert Comment

    by: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
    0
     
    LVL 1

    Author Comment

    by:debbieau1
    I have<?php session_start();?> at the the top of each page.  I think that is what you mean?
    0
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    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.
    0
     
    LVL 7

    Expert Comment

    by:BenMorel
    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
    0
     
    LVL 1

    Author Comment

    by:debbieau1
    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
    0
     
    LVL 7

    Expert Comment

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

    Ben
    0
     
    LVL 1

    Author Comment

    by:debbieau1
    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.
    0
     
    LVL 2

    Expert Comment

    by:TheUnkind
    can you post your code?
    0
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    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.
    0
     
    LVL 1

    Author Comment

    by:debbieau1
    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.
    0
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    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.
    0
     
    LVL 2

    Expert Comment

    by:TheUnkind
    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?

    0
     
    LVL 1

    Author Comment

    by:debbieau1
    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.  
    0
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    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")
    0
     
    LVL 2

    Expert Comment

    by:suresh_asp
    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
     
    0
     
    LVL 29

    Expert Comment

    by:fibo
    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
    0
     
    LVL 29

    Expert Comment

    by:fibo
    ... 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...
    0
     
    LVL 1

    Author Comment

    by:debbieau1
    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

    0
     
    LVL 29

    Accepted Solution

    by:
    As long as session_start is at the top, you can put your session_register and session_unregister at the "logical place "in your page.

    HOWEVER be careful not to mix
              register constructs with
              constructs like  $xx=$_SESSION['key']; or $_SESSION['key']="MyValue";
    since that fails in some occasions.
    I have personnally dropped the register-like type in favor of the $_SESSION['key'] type.
    To unregister 'key' I simply put $_SESSION['key']=""; and test for isempty instead of isset
    You can also use unset($_SESSION['key']) but I think I've got po=rpoblems on some servers
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    As you all know PHP got one problem, before using other files classes or functions you need to include or use require function. But now in PHP 5 there's solution for this, it's called __autoload() function. You can read more about function in PHP ma…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now