Bean Boy Posted October 8, 2008 Share Posted October 8, 2008 Hi guys, Does anyone know why this records the session: <?php session_start(); $_SESSION['count']='SET'; echo $_SESSION['count']; ?> But this doesn't record the session: <?php session_start(); echo $_SESSION['count']; $_SESSION['count']='SET'; ?> It's for a page counter. Since the first view counts the visit, the session needs to be not set. After that, the session is set so that if the page is refreshed the counter won't add anothet count. When finished, it would look more like this: <?php session_start(); echo $_SESSION['count']; if(!isset($_SESSION['count'])){ echo "Added count."; } else{ echo "Didn't add count."; } $_SESSION['count']='SET'; ?> Link to comment https://forums.phpfreaks.com/topic/127579-when-i-move-this-below-it-goes-bunk/ Share on other sites More sharing options...
ratcateme Posted October 8, 2008 Share Posted October 8, 2008 have you tried putting that code and nothing else on a page and refreshing it? there is no reason to say that wouldn't work unless sessions are configure strangely and i don't even know if that is possible. Scott. Link to comment https://forums.phpfreaks.com/topic/127579-when-i-move-this-below-it-goes-bunk/#findComment-660188 Share on other sites More sharing options...
Bean Boy Posted October 8, 2008 Author Share Posted October 8, 2008 Yup, I've stripped it down to what I displayed above. And in my example: 1) If it's above the echo, the $_SESSION is set. 2) But if it's below the echo, it isn't set when I refresh the page. Any idea why it would no longer be set? Link to comment https://forums.phpfreaks.com/topic/127579-when-i-move-this-below-it-goes-bunk/#findComment-660283 Share on other sites More sharing options...
ratcateme Posted October 8, 2008 Share Posted October 8, 2008 i don't know what would do this i put your code on my server and it worked fine. the only thing i could put it down to is how you have sessions configured but i don't know how that could effect it Scott. Link to comment https://forums.phpfreaks.com/topic/127579-when-i-move-this-below-it-goes-bunk/#findComment-660360 Share on other sites More sharing options...
Bean Boy Posted October 9, 2008 Author Share Posted October 9, 2008 I appreciate the feedback Scott. I ran the code and it worked for me too. Then when I took a look at my real code, I remembered I use a variable for my session variable, like: $count = num; echo $_SESSION[$count]; $_SESSION[$count] = "Set"; I know it works to put $count into a $_SESSION in other circumstances. But it seems that in this situation, it causes the code to not work. Is what I'm trying to do not even possible with PHP? Link to comment https://forums.phpfreaks.com/topic/127579-when-i-move-this-below-it-goes-bunk/#findComment-660454 Share on other sites More sharing options...
xtopolis Posted October 9, 2008 Share Posted October 9, 2008 This works for me. only when $count is an actual number, like $count = 1; does it stop working. Could that be your problem? <?php session_start(); $count = num; if(!isset($_SESSION[$count])) { $_SESSION[$count] = 'SET'; echo 'Added to count.'; }else{ echo 'Didn\'t add to count.'; } ?> Link to comment https://forums.phpfreaks.com/topic/127579-when-i-move-this-below-it-goes-bunk/#findComment-660497 Share on other sites More sharing options...
iversonm Posted October 9, 2008 Share Posted October 9, 2008 trying making $count something strange and unique the server saves certain sessions for use of the the server alone, if $_SESSION[num] is being used by the server it wont register it try using $_SESSION['FDSFWEFWE'] if you get what im saying i bet that will work Link to comment https://forums.phpfreaks.com/topic/127579-when-i-move-this-below-it-goes-bunk/#findComment-660503 Share on other sites More sharing options...
Bean Boy Posted October 9, 2008 Author Share Posted October 9, 2008 Wow, thanks a million, guys!! I've been stuck on this problem for over a day and if I still watched Mr. Dressup, the Word Bird's word of the day would have "frustration". Yes, it does seem like the problem was with my $ids being actual numbers. They're actually ids I pull from a MySQL table. So all I had to do was: $id = 1 . "dshfckjshdfs"; // tag random stuff at the end Thanks again! Link to comment https://forums.phpfreaks.com/topic/127579-when-i-move-this-below-it-goes-bunk/#findComment-660542 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.