zang8027 Posted February 3, 2009 Share Posted February 3, 2009 What I am trying to do is when the session "cart" is created, create a constant with it. If it is already created, then the constant is already set, so use what is set. These constants will only be deleted when the user closes the site. Here is my code <?php // TESTING OUT CONSTANTS ****************** $restID = $_REQUEST['restId']; if(! isset($_SESSION['cart'])) { $_CONSTANTS['restaurant'] = $restID; foreach($_CONSTANTS as $key => $val) define($key, $val); $myConst = constant('restaurant'); }else{ $myConst = constant('restaurant'); } ?> Now, first time I go to the site, everything is fine. But once i refresh the page, it says it can not find the constant. Why can't it find it? It is supposed to be global. Quote Link to comment https://forums.phpfreaks.com/topic/143633-solved-sessions/ Share on other sites More sharing options...
waynew Posted February 3, 2009 Share Posted February 3, 2009 Is session_start() at the top of your page? Also, using $_REQUEST is risky. Quote Link to comment https://forums.phpfreaks.com/topic/143633-solved-sessions/#findComment-753635 Share on other sites More sharing options...
zang8027 Posted February 3, 2009 Author Share Posted February 3, 2009 yes it is up there. How come it is risky, should i just use get? Quote Link to comment https://forums.phpfreaks.com/topic/143633-solved-sessions/#findComment-753646 Share on other sites More sharing options...
zang8027 Posted February 3, 2009 Author Share Posted February 3, 2009 im trying to use a new session now. <?php $_CONSTANTS['restaurant'] = $restID; foreach($_CONSTANTS as $key => $val) define($key, $val); $myConstant = constant('restaurant'); $_SESSION['rest'] = $myConstant; //now session 'rest' is equal to my constant value }else{ } Now I want to set that session to a variable if possible to reference. Is that possible? See, I am using the value of this session to be passed to functions and pull information out of a database. Can i just pass a session through a function? function name($_SESSION['cart']) ??? Quote Link to comment https://forums.phpfreaks.com/topic/143633-solved-sessions/#findComment-753678 Share on other sites More sharing options...
premiso Posted February 3, 2009 Share Posted February 3, 2009 You can, but you do not need to. Inside the function you can access that by $_SESSION['cart']. It depends on if you want your function to handle more than just session or not. Quote Link to comment https://forums.phpfreaks.com/topic/143633-solved-sessions/#findComment-753681 Share on other sites More sharing options...
zang8027 Posted February 3, 2009 Author Share Posted February 3, 2009 so would $query="Select * from myTable WHERE id=$_session['rest']"; be valid? Ill try that here in a few. Quote Link to comment https://forums.phpfreaks.com/topic/143633-solved-sessions/#findComment-753686 Share on other sites More sharing options...
premiso Posted February 3, 2009 Share Posted February 3, 2009 $query="Select * from myTable WHERE id={$_SESSION['rest']}"; That would be. Quote Link to comment https://forums.phpfreaks.com/topic/143633-solved-sessions/#findComment-753688 Share on other sites More sharing options...
zang8027 Posted February 3, 2009 Author Share Posted February 3, 2009 omg you are a life saver.. literally! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/143633-solved-sessions/#findComment-753697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.