SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Some help with phpsessions

    I'm really confused at the moment. I don't unserstand why this doesn't work, it's a simple script and it should work. I guess I have done something stupid again =/

    PHP Code:
    /*********************************************
    #
    #    session start function. Include it on top of every page
    #
    *********************************************/

    function start_session() {
        
        
    session_start();
        
        
    //is the userid set?        
        
    if(!isset($_SESSION['userid'])){
            
    session_create();
        } elseif (
    $_SESSION['userid'] > 0) {
            
    // looks like the session is from the right origin lets create a 
            
    define(LOGGED_INtrue);
        }
    }

    /********************************************
    #
    #    create session
    #
    ********************************************/
    function session_create($userid 0){// pass session vars through the function
        
    $_SESSION['userid'] = $userid;
    }

    // the code in the page comes now
    if(!LOGGED_IN) {
        
    $Smarty->assign('menu'"not logged in");
    } else {
        
    $Smarty->assign('menu'"logged in");

    My problem is that $Smarty->assign('menu', "logged in"); always is run

    (yeah, I'm using the smarty template engine)
    chrome is a wrapper that combines a browser with spyware

  2. #2
    SitePoint Addict GeertDD's Avatar
    Join Date
    Feb 2005
    Location
    Belgium
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't forget the quotes.

    PHP Code:
    define('LOGGED_IN'true); 
    You may want to read the little part about "Consistent Constants" at http://devzone.zend.com/node/view/id/168.
    Last edited by GeertDD; May 28, 2006 at 02:17. Reason: link added

  3. #3
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, always easy to spot mistakes that makes you grind your teeth for hours cos you can't see them. Thanks

    EDIT:
    Looks like there is some other error in there too, still got the same problem :S
    chrome is a wrapper that combines a browser with spyware

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your conditionally defining a constant. if the constant doesnt exist, php will treat it as a string. the string 'LOGGED_IN' will always evaluate to boolean true. so you need to check if the constant was defined or not.

    PHP Code:

    if(!defined('LOGGED_IN')) {


    another possibility would be to check the value of the constant, using the strict comparion operators. but i would not recomend this way.

    PHP Code:

    if(LOGGED_IN !== true) {



  5. #5
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /*********************************************
     #
     #    session start function. Include it on top of every page
     #
     *********************************************/
     
     
    function start_session() {
         
         
    session_start();
         
         
    //is the user logged in?        
         
    if ($_SESSION['userid'] > 0) {
             
    define('LOGGED_IN'true);
         } else {
             if(!isset(
    $_SESSION['userid'])){
                 
    session_create();
             }
             
    define('LOGGED_IN'false);
         }
     } 
    with that function it should work right?
    chrome is a wrapper that combines a browser with spyware


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
  •