Jump to content

When I Move THIS Below, It Goes Bunk


Bean Boy

Recommended Posts

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

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?

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.';
}
?>

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.