Professional Web Applications Themes

Session problem: session exists, but session variables don't lasts - PHP Development

Greetings! I have some troubles with session variables. I can easily create a session, but the variable I append to global session array ( $_SESSION) lasts only until the end of current function. I prepared an example that can demonstrate the problem (see code bellow): The program flow is this: (1) It starts with "session_start()" and displays "login_form()". (2) When user enters his username and password, it goes to "login()" function which sets $_SESSION variables. (3) This function also "Echoes" session_id and user name to the screen. (4) Then it goes to "MyProgram()" function. It only "Echoes" the same variables ...

  1. #1

    Default Session problem: session exists, but session variables don't lasts

    Greetings!

    I have some troubles with session variables. I can easily create a session,
    but the variable I append to global session array ( $_SESSION) lasts only
    until the end of current function.

    I prepared an example that can demonstrate the problem (see code bellow):
    The program flow is this:
    (1) It starts with "session_start()" and displays "login_form()".
    (2) When user enters his username and password, it goes to "login()"
    function which sets $_SESSION variables.
    (3) This function also "Echoes" session_id and user name to the screen.
    (4) Then it goes to "MyProgram()" function. It only "Echoes" the same
    variables to the screen.

    Strange is, that the session_id is the same in (3) and (4), but the user
    name ($_SESSION["username"]) is only in (3), but not in (4). By other
    words - $_SESSION["username"] variable filled up by "login()" function is
    immediately cleared.

    I don't know why.

    There must be something missing, but after 3 days of watching it, I can not
    find a problem here.

    I will really appreciate any help!
    Pavel

    ===========================================
    <?
    Function login($uzivatel, $heslo)
    {
    $_SESSION["username"] = $uzivatel;
    $_SESSION["pass"] = $heslo;
    $U_Name=$_SESSION['username'];
    echo "Login username: $U_Name<BR>";
    echo "Login session ID: ";
    echo session_id();
    echo "<BR>";
    }

    Function login_form()
    {
    echo ("<DIV ALIGN='center'>
    <H1>Test</H1>
    <P>Please enter your username and password:</P>
    <form action='test.php' method='get'>
    <INPUT TYPE='HIDDEN' NAME='login' VALUE=TRUE>
    <TABLE BORDER='0'>
    <TR>
    <TD ALIGN='right'><U>U</U>ser name:</TD>
    <TD><INPUT NAME='uzivatel' SIZE=25 ACCESSKEY='U' TYPE='TEXT'></TD>
    </TR>
    <TR>
    <TD align='right'><U>P</U>assword:</TD>
    <TD><INPUT NAME='heslo' SIZE=25 ACCESSKEY='P' TYPE='PASSWORD'></TD>
    </TR>
    <TR>
    <TD COLSPAN=2 ALIGN='RIGHT'><INPUT NAME='OK' ACCESSKEY='O' TYPE='SUBMIT'
    VALUE='OK' TABINDEX=3></TD>
    </TR>
    </TABLE>
    </FORM></DIV></HTML>");
    }

    Function MyProgram()
    {
    $U_Name=$_SESSION['username'];
    echo "Program username: $U_Name<BR>";
    echo "Program session ID: ";
    echo session_id();
    echo "<BR>";
    }

    {
    session_start();
    if ($_SESSION['username']):
    MyProgram();
    else:
    {
    if (!$login):
    login_form(0);
    else:
    login($uzivatel, $heslo);
    MyProgram();
    endif;
    }
    endif;
    }
    ?>
    ===========================================



    Pavel Guest

  2. #2

    Default Re: Session problem: session exists, but session variables don't lasts

    PS: I forgot to mention I have an "archaic" version of PHP installed
    (4.0.2). ....

    Pavel

    "Pavel Vetesnik" <cz> píše v diskusním příspěvku
    news:btu0v6$6hi$felk.cvut.cz... 
    session, 
    not 
    TYPE='SUBMIT' 


    Pavel Guest

  3. #3

    Default Re: Session problem: session exists, but session variables don'tlasts

    Uhm... yeah... upgrade your version of PHP. Asking for help on archaic
    versions is often futile. New versions are released to correct bugs,
    etc! Upgrade!!!

    P.S. $_POST, $_SESSION, $_GET, $_FILES, $_REQUEST, $_ENV weren't added
    until 4.1.0.

    Upgrade to 4.3.4 dude...

    Pavel Vetesnik wrote: 
    >
    > session,

    >
    > not

    >
    > TYPE='SUBMIT'

    >
    >
    >[/ref]

    Agelmar Guest

  4. #4

    Default Re: Session problem: session exists, but session variables don't lasts

    It works now!!!!

    Pavel



    "Agelmar" <net> píse v diskusním príspevku
    news:btuhf0$blo4g$news.uni-berlin.de... 
    > >
    > > session,
    > > [/ref][/ref]
    only [/ref][/ref]
    bellow): [/ref][/ref]
    is 
    > >
    > > not
    > > 
    > >
    > > TYPE='SUBMIT'
    > > 
    > >
    > >
    > >[/ref]
    >[/ref]


    Pavel Guest

Similar Threads

  1. Replies: 17
    Last Post: September 12th, 11:42 PM
  2. Session Changing Another Session's Variables
    By FDSailor in forum Macromedia ColdFusion
    Replies: 2
    Last Post: March 14th, 11:48 PM
  3. #25574 [Bgs]: empty session.save_path = problem (session.use_only_cookies = 1)
    By markus dot welsch at suk dot de in forum PHP Development
    Replies: 0
    Last Post: September 18th, 06:13 AM
  4. #25574 [Opn]: empty session.save_path = problem (session.use_only_cookies = 1)
    By markus dot welsch at suk dot de in forum PHP Development
    Replies: 0
    Last Post: September 17th, 02:19 PM
  5. Replies: 0
    Last Post: July 23rd, 12:40 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139