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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Session

  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Session

    Hello, I am having problem passing a session veriable to another page.

    Singin.php contains :
    session_start();
    $_SESSION['user'] = $user;
    echo "<a href=check_session.php>next page</a>";

    In the check_session.php page contains :
    session_start();
    $user=$_SESSION['user']; //This is line 3
    echo "the user name is $user";

    When I clicked on the link to check_session.php, it doesn't display the user name, but with the notice message :
    Notice: Undefined index: user in C:\user\share\public\web\check_sesion.php on line 3

    Also, when should I use the session_register?

    Thank you all

    Mike

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Which version of PHP are you using?

    Try adding print_r($_SESSION); into the page, that'll tell you if there's anything in the session. If you don't get anything, I'm assuming you're on an older version of php and you might want to use $HTTP_SESSION_VARS['user'] instead of $_SESSION

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    nightfire

    Thanks for your reply, I had tried everything you told me to.

    but none of them seem to work.

    when I put print_r($_SESSION); it outputs
    Array ( )
    //empty array....

    So it mean, I am not passing the Session value properly.

    Notice: Undefined index: user in C:\user\share\public\web\check_session.php on line 3

    I am using the newest version php.

    Thank you,

    Mike

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    In your signin page echo out the session variable after you set it just to make sure it is even getting put into the session:

    $_SESSION['user'] = $user;
    echo "<a href=check_session.php>next page</a>";
    echo "User = " . $_SESSION['user'];
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Spookster

    Yup, when I echo the variable in the singin.php page, it works fine, just not in the check_session.php

    don't know why, I can't pass session variable to another page..

    can someone make a small testing session script for me (working script)? I just want to make sure my server works with session.

    Do I have to do any setting with the php.ini file?

    Thank you all,

    Mike

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    The code you have posted should work just fine. If you can access the session variable in the one page then everything is working just fine. There must be something else wrong. Paste the exact contents of the check_session.php page here in this thread.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try printing $_SESSION['user'] in the check session page, just to make sure its not somewhere else, I would try that.

    Id does look like it should work fine though, so like spooks said, post the code.

  • #8
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ok, here is my code

    Login.php
    case 1:
    session_start();
    $ip = $_SERVER['REMOTE_ADDR'];
    $_SESSION['user'] = "mikemike";
    $_SESSION['ip'] = $ip;
    echo "<a href=\"check_session.php\">check</a>";

    echo "user : $_SESSION[user] and ip : $_SESSION[ip]"; //works fine here, display both perfect...
    break;

    check_session.php
    <?php
    session_start();
    $users=$HTTP_SESSION_VARS['user'];
    echo "hello user : $users"; //doesn't display $users

    $ips=$_SESSION['ip'];
    echo "<br />the ip is $ips"; //doesn't display $ips

    print_r($_SESSION); //it prints out Array()
    ?>

    Thank you all for your help,

    Mike

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ummm. Looks fine to me.

    2 things:

    Why are you using break? I might have missed something, but there seems to be no reason for it.

    Why are you using $_SESSION AND $HTTP_SESSION_VARS?


    There seems to be no reason why it isn't working.

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, missed the case1: bit.

    Try printing the session var After break. Shouldnt make a difference, but its worth a try.

  • #11
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Missing-score

    Nope, it didn't work...

    Do you think I have to do some changes in the php.ini file?

  • #12
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Possibly, but I wouldn't have thought so. If the session is recognising on one page, it should on the other too.

  • #13
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Ok when you are debugging a problem such as this it is best to try something basic and simple. You can do this in those pages or do this in two other pages.

    In one page just create a session and set a variable:

    PHP Code:

    <html>
    <body>

    <?php

    session_start
    ();
    $_SESSION["test"] = "testing";

    ?>

    <a href="otherpage.php">Other page</a>

    </body>
    </html>
    in the other page:

    PHP Code:

    <html>
    <body>

    <?php

    session_start
    ();
    echo 
    "The test variable = " $_SESSION["test"];
    session_destroy();

    ?>

    </body>
    </html>
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #14
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    spookster

    I tried your testing pages. I took out the html tags because can't start session after the tags.

    anyway, same error message:

    Notice: Undefined index: test in C:\user\share\public\web\developer\account\test2.php on line 4
    The test variable =

  • #15
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Ok paste these into new pages and test them on your server. I have them on mine right now and everything works fine.

    contents of session1.php

    PHP Code:

    <?php
    session_start
    ();
    ?>

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

    <body>

    <?php 
    $_SESSION
    ["test"] = "testing";
    ?>

    <a href="session2.php">Session2.php</a>
    </body>
    </html>
    contents of session2.php

    PHP Code:

    <?php
    session_start
    ();
    ?>

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

    <body>

    <?php 
    echo "The test variable = " $_SESSION["test"];
    ?>

    </body>
    </html>
    If that doesn't work then something else is obviously wrong. You can see them in action on my server to see what should happen:

    http://www.designqueue.com/session1.php
    http://www.designqueue.com/session2.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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