SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 73

Thread: Sessions

Hybrid View

  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Sessions

    I am using the below line:

    PHP Code:
    if ($_session['loggedin'] == 1) {

    do 
    this

    }else{

    do 
    this



    The problem occurs when i view the page, I keep getting an undefined index error for "loggedin" saying it does not exist but obviously it might not exisit as it might not have been created yet. What can i do to stop these errors?

    Thanks

    Neil

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    variables in php are case sensistive.

    $_SESSION

    to check if a variable or array index exists before you use it, see isset() and empty()

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rctneil View Post
    obviously it might not exisit as it might not have been created yet. What can i do to stop these errors?
    PHP Code:
    if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == 1) { 
    Short-circuit ("lazy") evaluation of conditions means the first failed portion of ANDed conditions will cause the entire condition to immediately fail. That means if isset() fails because $_SESSION['loggedin'] doesn't exist, the rest of the condition won't be evaluated at all, so no notices are generated.

  4. #4
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right, Thanks,
    I have on my page this:

    echo $_SESSION['loggedin'];


    even though i have this:

    $_SESSION['loggedin'] == 0;

    at the top of the page, 1 still keeps being echoed out?

    why?

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    == is a comparion operator
    = is a assignment operator

    tip, do not use echo to see the values of your variables.
    var_dump() offers added value. view your html source instead of letting the browser render, because rendering as html can change/hide certain characters.

  6. #6
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if($_SESSION['loggedin'] == 0)
    { code}

    $_SESSION['loggedin'] = 0;

  7. #7
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but using $_SESSION['loggedin'] = 0; or unset($_SESSION['loggedin']); does not make any differnce. 1 is always echoed out as being the value for the loggedin session variable!

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that would be because you are probably overwriting the variable with a new value.

  9. #9
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but i'm not?

  10. #10
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can we see complete lines of code and values of those ?

  11. #11
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You probably have done this but just to make sure, have you got session_start() prior to checking session variables?

    Al

  12. #12
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you arent overwriting it, then you arent ever unsetting it or setting it to 0.

  13. #13
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, session_start() is at the top of the page. any ideas?

  14. #14
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can we see complete lines of code and values of those ?

  15. #15
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll post the whole code soon

  16. #16
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    we r waiting

  17. #17
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Here is the whole code of the page:

    PHP Code:
    <?php
    $_SESSION
    ['loggedin'] = 0;
    ?>
    <!--Default page display-->

    <?php 
    if (!isset($_GET['action'])) {
        if (isset(
    $_SESSION['loggedin']) && $_SESSION['loggedin'] == 0) { 

    ?> 

    <!--Intro Text--> 
    <p>Welcome to the Alton Towers Mania Membership System. Registering as part of the membership system will allow you to be more integrated with the site and give you access to do more exciting features, like submitting your high score on Duel to the high score board to tracking what rides you have ridden as well as which are your favourite rides at the park!</p>
    <!--End Intro Text-->

    <!--New Lines-->
    <br /><br />
    <!--End New Lines-->

    <p class="center mediumheadings"><a href="membership.php?action=login">Log In</a>&nbsp;&nbsp;&nbsp;: :&nbsp;&nbsp;&nbsp;<a href="membership.php?action=register">Register</a></p>



    <?php
    } else {
    ?>

    <!--If Logged in-->

    <!--Intro Text--> 
    <p>Welcome to the Alton Towers Mania Membership System. Now you have logged in you can visit the various links below to access differnent parts of the site which support the membership system. Any areas of this site where you see the membership system icon, allow you to use your account with them.</p>
    <!--End Intro Text-->

    <!--New Lines-->
    <br /><br />
    <!--End New Lines-->

    <ul>
    <li>Ride Tracker</li>
    <li>Favourites</li>
    <li>Duel Score Board</li>
    </ul>

    <?php
    }
    }
    ?>

    <!--New Lines-->
    <br /><br />
    <!--End New Lines-->

    <?php

    echo $_SESSION['loggedin'];

    ?>






    <!--New Lines-->
    <br /><br />
    <!--End New Lines-->
    AS you can see, near the top i am setting loggedin to be 0 but when the page is run the echo loggedin, (near the bottom), always outputs 1?

  18. #18
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where is ur session_start in this file

  19. #19
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    wht the o/p for this ?

    PHP Code:
    <?php session_start();
    $_SESSION['loggedin'] = 0;
    echo 
    $_SESSION['loggedin']."--";

    ?>
    <!--Default page display-->

    <?php
    if (!isset($_GET['action'])) 
    {
        if (isset(
    $_SESSION['loggedin']) && $_SESSION['loggedin'] == 0) {

        echo 
    "in if";

    else 
    {
        echo 
    "else";
        }
    }


    echo 
    $_SESSION['loggedin'];

    ?>

  20. #20
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Try this

    PHP Code:
    if ((isset($_SESSION['loggedin'])) && ($_SESSION['loggedin'] == 0)) 

  21. #21
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which of your last two posts should i try and what differences are there? Also, i should have metioned that session_start() is above that code i posted but i didn't include on here.

  22. #22
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, if you want me to try the second bit of code you posted where should i place it?

  23. #23
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Try This One

    PHP Code:
    <?php session_start();
    $_SESSION['loggedin'] = 0;
    echo 
    $_SESSION['loggedin']."--";

    ?>
    <!--Default page display-->

    <?php
    if (!isset($_GET['action'])) 
    {
        if ((isset(
    $_SESSION['loggedin'])) && ($_SESSION['loggedin'] == 0)) 
        {
            echo 
    "in if";
        } 
        else 
        {
            echo 
    "else";
        }
    }


    echo 
    $_SESSION['loggedin'];

    ?>
    Diff in the if conditions with few brackets which doesnt mak any difference in ur case but just use them and print ur o/p for us

    Thanks

  24. #24
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,897
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for not replying but i am trying tof ix it and getting there slowly. I have found that i am having trouble with testing if a variable (either SESSION or GET) isset if i have error reporting set to all. If i do this then i get errors saying that certain variables are not defined when they are not surposed to be. If i turn the error reporting off then i can test to see what values variables are even if they are not defined yet. Can i do this? - PLEASE READ CAREFULLY AND UNDERSTAND BEFORE REPLYING!

  25. #25
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes you can, but its better for you to make your code work properly with error reporting maxed. you should at least understand how to do it.

    you can set error reporting to the default level which will show errors, but not notices. see the manual for error_reporting() to see how.


Bookmarks

Posting Permissions

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