Problem with session variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • helloroger@iname.com

    Problem with session variables

    Hi folks

    I'm new to php an currently trying to insall my first php-Session. I've
    written the following code which uses the 2 variables cat and langua,
    but somehow they are not correctly registered in the session. When
    pressing the links to set the 2 session variables, they don't move at
    all. There values are always 1 and 9.
    Does anybody have an idea how I can set the value of a session variable
    using a <a href ...>?

    Any help appreciated.

    Sunnyboy



    <?php
    session_start() ;

    session_registe r("cat");
    session_registe r("langua");
    ?>

    <HTML>
    <HEAD>
    </HEAD>
    <BODY>

    <?php

    echo "langua = $langua<br>";
    echo "cat = $cat<br>";

    // Read variable langua
    if (!isset ($_SESSION["langua"]))
    {
    echo "langua not registerd in session<br>";
    $langua = 1;
    }
    else
    {
    echo "langua registerd in session<br>";
    $langua = ($_SESSION['langua']);
    }

    // Read variable cat
    if (!isset ($_SESSION["cat"]))
    {
    echo "cat not registerd in session<br>";
    $cat = 9;
    }
    else
    {
    echo "cat registerd in session<br>";
    $cat = ($_SESSION["cat"]);
    }

    echo "langua = $langua<br>";
    echo "cat = $cat<br>";
    ?>

    <br><br>
    <a href="?langua=2 ">set langua=2</a><br>
    <a href="?langua=3 ">set langua=3</a>
    <br><br>

    <a href="?cat=3">s et cat=3</a><br>
    <a href="?cat=4">s et cat=4</a>
    <br><br>

    <?php
    echo "cat=$cat<b r>";
    echo "langua=$langua <br>";

    session_write_c lose();
    ?>

    </BODY>
    </HTML>

  • samudasu

    #2
    Re: Problem with session variables

    First of all, welcome to PHP. Before starting on sessions, i suggest
    doing some more reading on variables "Chapter 12", and especially 12.9
    for this particular script in the online edition of the PHP manual.


    I'm not sure if you're learing from an old tutorial or book but there
    have been several changes in the way PHP works compared to previous
    versions. Here is one example.
    When you check the value of langua after clicking on "<a
    href="?langua=2 ">set langua=2</a>", you'll have to check it by doing
    $_GET['langua'] instead of $langua.

    Keep it simple when starting out.

    page1.php...... ......
    <html>
    <body>
    send a variable to page2<br />
    <a href="page2.php ?color=blue">se nd blue</a><br />
    <a href="page2.php ?color=red">sen d red</a><br />
    </body>
    </html>

    page2.php...... .......
    <?php
    session_start() ;
    // assign color from page 1 into session
    $_SESSION['current_color'] = $_GET['color'];
    ?>
    <html>
    <body>
    <?php
    print $_SESSION['current_color'];
    }
    ?>
    </body>
    </html>

    Comment

    Working...