Web Analytics Made Easy -
StatCounter Unable to set session variable - CodingForum

Announcement

Collapse
No announcement yet.

Unable to set session variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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!
    diplominiai darbai

  • #2
    On every script that uses the SESSION variable, you need this at the top:

    <?php
    session_start();


    Do your other scripts have that?


    .

    Comment


    • #3
      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.
      diplominiai darbai

      Comment


      • #4
        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; Sep 13, 2011, 08:12 AM.

        Comment


        • #5
          Have you tried performing a var_dump($_SESSION) to see whats going on?
          "Tango says double quotes with a single ( ' ) quote in the middle"
          '$Name says single quotes with a double ( " ) quote in the middle'
          "Tango says double quotes ( \" ) must escape a double quote"
          '$Name single quotes ( \' ) must escape a single quote'

          Comment


          • #6
            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?
            diplominiai darbai

            Comment


            • #7
              It shouldn't be.
              "Tango says double quotes with a single ( ' ) quote in the middle"
              '$Name says single quotes with a double ( " ) quote in the middle'
              "Tango says double quotes ( \" ) must escape a double quote"
              '$Name single quotes ( \' ) must escape a single quote'

              Comment


              • #8
                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.
                PHP Code:
                header('HTTP/1.1 420 Enhance Your Calm'); 
                Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                Comment

                Working...
                X
                😀
                🥰
                🤢
                😎
                😡
                👍
                👎