How to use session

Discussion in 'PHP' started by kharearch, Mar 5, 2008.

  1. #1
    I am using session to use variable of one page to another. I have written two program one for registering variable another for printing the value of registered variable. But when I am printing registered variable it is showing nothing.

    my programes are

    <?php
    session_start();
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>

    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>

    <?php

    session_register("product1");
    $product1 = "abcd";
    print session_encode();
    print "\nproduct registered";

    ?>

    </body>


    </html>



    Second program is ----



    <?php
    session_start();
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>


    <body>

    <?php
    print "your chosen produts are \n\n";
    print $product1;


    ?></body>
    </html>



    Please solve my problem.
     
    kharearch, Mar 5, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Forget about all this session_register() crap. It only works if register_globals is enabled, and besides that, it's horribly ugly.

    Use sessions like this:
    (Page 1)
    
    <?php
    
    session_start();
    
    $_SESSION['foo'] = 'bar';
    
    ?>
    
    PHP:
    (Page 2)
    
    <?php
    
    sessio_start();
    
    echo $_SESSION['foo'];
    
    ?>
    
    PHP:
    Simple as that.
     
    nico_swd, Mar 5, 2008 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    In a similar topic, is it safe to put login data (usernames and passwords) into a session?
     
    blueparukia, Mar 5, 2008 IP
  4. kharearch

    kharearch Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Thanks it is working.
     
    kharearch, Mar 5, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Why would you store a password at all?

    It's as safe as the method you use to protect/handle your sessions.
     
    nico_swd, Mar 5, 2008 IP