Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post

    Unable to set session variable

    Hello everyone,

    I'm stucked with a very little piece of code.

    PHP Code:
    session_start();
    $connection mysql_connect('localhost''user''pass');
    mysql_select_db('database');
    $query "select * from table where `code` = '" $_GET['kodas'] . "' && `used` = '0'";
    $result mysql_query($query);
    if(
    mysql_num_rows($result) !== '0') {
    $_SESSION['code'] = "" $_GET['kodas'] . "";

    The problem is that if I use this piece of code session variable is being set only for one page. After refreshing it disapears. If I create the session variable directly (without checking for data in database, everything goes fine.

    I would be grateful for any help!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,522
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    On every script that uses the SESSION variable, you need this at the top:

    <?php
    session_start();


    Do your other scripts have that?


    .

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Everything is fine with that session_start();, cause all the action is going on one page.

    The structure of my page is like that:

    PHP Code:
    <?
    session_start
    ();
    if(!isset(
    $_SESSION['code'])) {
    // some actions to take to get $_SESSION['code'] set.
    }
    else {
    //showing html form.
    }
    ?>
    After $_SESSION variable is set I reload the page. And that reload should show me the HTML form (else part of script), but nothing happens, because for some reason session variable gets lost. Everything works fine if I don't check the database for accuracy of the code. (The code of this check is in the first post of topic.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,522
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    Put the SESSION setting part in it's own script
    and after running it, redirect back. See if that works.

    sess.php
    PHP Code:
    <?php
    session_start
    ();
    $connection mysql_connect('localhost''user''pass');
    mysql_select_db('database');
    $query "select * from table where `code` = '" $_GET['kodas'] . "' && `used` = '0'";
    $result mysql_query($query);
    if(
    mysql_num_rows($result) !== '0') {
    $_SESSION['code'] = "" $_GET['kodas'] . "";
    }  
    header ("location: index.php");
    ?>

    PHP Code:
    <?php
    session_start
    ();
    if(isset(
    $_SESSION['code'])) {
    // do nothing ... just drop down to the rest of the script.
    }
    else {
    // go set the session
    header ("location: sess.php");
    }
    ?> 

    <html>
    Rest of the HTML
    </html>

    Edit:
    Or I got it backwards ...
    anyhow, the idea is to create an extra header.
    Maybe the SESSION is getting dropped by not getting it sent in the header.
    The redirect is transparent to the user, but creates the header.


    .
    Last edited by mlseim; 09-13-2011 at 02:12 PM.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,395
    Thanks
    61
    Thanked 535 Times in 522 Posts
    Have you tried performing a var_dump($_SESSION) to see whats going on?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    New Coder
    Join Date
    Apr 2011
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    I found out that all the problem is caused by database selection line. No matter what, if I select database, session lasts only for one page. Can it be bacouse of my local server configuration?

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,395
    Thanks
    61
    Thanked 535 Times in 522 Posts
    It shouldn't be.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This is always true regardless of what the results are:
    PHP Code:
    if(mysql_num_rows($result) !== '0') { 
    Mysql_num_rows returns an integer, not a string. That should be != not !==.
    So depending on how this is used depends on if you will always have the session variable overwritten.


  •  

    Posting Permissions

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