Page 1 of 2

PHP sessions frustration

Posted: Mon Dec 08, 2003 1:28 pm
by phpNUKED
Hey all,

New here, so please bear with me....

Having problem with php sessions. I have tried the below code...

<?
session_start();
session_register('count');
$count = $_SESSION[count]++;
?>
You have been to this page <?=$count ?> times.<br>

and the following code....

<?
session_start();
if(!isset($count)) {
echo "first<br>";
if(session_is_registered("count"))
{
echo 'second<br>';
session_register("count");
//$count = session_register("count");
}}
else{ echo 'third<br>'; $count = 1; }

?>
You have been to this page <?=$count?> times.<br>
<?
echo 'fourth<br>';
$count++;
echo $count;
?>

but to no avail..... :cry:

What happens is that each time I refresh the browser, a new session is made with 1 being the int count, which means that my counter never gets any bigger. :evil: :evil: :evil: It seems that something is falling apart somewhere in that it isnt even checking to see if I have a session already.

Now, I am not attempting to make a counter with this, I am simply trying to gain an understanding of how sessions work.

This is basically the last step for my ecom site, and it has me hung up severely, any assistance would be greatly appreciated.

I am using localhost to test my site.

Posted: Mon Dec 08, 2003 1:51 pm
by microthick

Code: Select all

session_register('count');
The session_register() function will only work if register_globals is on.

If register_globals is off, you'll have to assign by using the $_SESSION[] array.

done

Posted: Tue Dec 09, 2003 8:48 am
by phpNUKED
Globals already turned on.

Posted: Tue Dec 09, 2003 9:04 am
by Saethyr
no changes just highlighting code so I can look at it.

Code: Select all

<? 
session_start(); 
session_register('count'); 
$count = $_SESSION[count]++; 
?> 
You have been to this page <?=$count ?> times.<br> 

and the following code.... 

<? 
session_start(); 
if(!isset($count)) { 
echo "first<br>"; 
if(session_is_registered("count")) 
{ 
echo 'second<br>'; 
session_register("count"); 
//$count = session_register("count"); 
}} 
else{ echo 'third<br>'; $count = 1; } 

?> 
You have been to this page <?=$count?> times.<br> 
<? 
echo 'fourth<br>'; 
$count++; 
echo $count; 
?>

Posted: Tue Dec 09, 2003 9:10 am
by Saethyr
take a look at

Code: Select all

<?php
session_start(); 

$_SESSION["count"] += 1; 

echo $count; 
?>
This will give you an idea of where to go.


Saethyr

Posted: Tue Dec 09, 2003 9:18 am
by JayBird
this line

Code: Select all

$count = $_SESSION[count]++;
should be

Code: Select all

$count = $_SESSION['count']++;
Mark

Posted: Tue Dec 09, 2003 1:28 pm
by phpNUKED
OK, here is what i have done thusfar.....

<?
session_start();
session_register('count');
$count = $_SESSION['count']++;
?>
You have been to this page <? echo $count ?> times.<br>


and the output I get is....

You have been to this page times.

The session file contains the following...

count|N;


Now have also tried the below....

<?
session_start();
$_SESSION["count"] += 1;
echo $count;
?>


And get the following output....

NOTHING ON SCREEN


Any other ideas please? Thanks in advance...

Posted: Tue Dec 09, 2003 2:08 pm
by microthick
Dunno if this would matter, but would you need to initialize the counter at 0 first? Otherwise you're adding + 1 to an empty var?

Posted: Tue Dec 09, 2003 2:13 pm
by phpNUKED
Hey micro,

From what I understand, PHP variables dont require initialization as do c++ variables.

Posted: Tue Dec 09, 2003 2:25 pm
by microthick
Yeah, I remember that. I also know that there aren't distinct variable types (int, string, etc) but is PHP smart enough to interpret $count += 1; when $count doesn't exist yet? Wouldn't this basically be the same as $count = NULL + 1 basically if $count doesn't exist before this line?

Posted: Tue Dec 09, 2003 3:48 pm
by phpNUKED
Well, about ready to toss this session crap right out the window.

Took Toms advice, and it would finally save the int variable in the session, however, still cannot increment it, and cannot get the method session_is_registered to work, shouldnt it tell me what variable is in the session, for example, if sesscount was in the session, and showed in the session txt file, should the method not be able to pick that up, that it is in the session already?

OK, my head just exploded, ill be back after i clean up the floor.

Posted: Tue Dec 09, 2003 4:36 pm
by Bill H

Code: Select all

<?php
session_start(); 
$_SESSION["count"] += 1; 
echo $count;
?>
With the above code, $count is null which is why nothing prints.
You are mixing two different versions.

Code: Select all

<?php
session_start(); 
session_register('count');   // is deprecated -- don't use it
$_SESSION['count'] = 0;   // is the way you want to init the session var
$count = $_SESSION['count']++;  // increments the session var
                                                  // and creates and loads the $count var
?>
You have been to this page <? echo $count ?> times.<br> 
// this line will now work

Posted: Wed Dec 10, 2003 7:43 am
by phpNUKED
THx all for your assistance.

Posted: Wed Dec 10, 2003 7:52 am
by aquila125

Code: Select all

<?php 
session_start(); 
session_register('count');   // is deprecated -- don't use it 
$_SESSION['count'] = 0;   // is the way you want to init the session var 
$count = $_SESSION['count']++;  // increments the session var 
                                                  // and creates and loads the $count var 
?> 
You have been to this page <? echo $count ?> times.<br> 
// this line will now work
You have to change the $_SESSION['count']=0; to

Code: Select all

if (!isset($_SESSION['count'])) $_SESSION['count']=0;
Else you always get count to be 1 after the script..

Posted: Wed Dec 10, 2003 8:29 am
by Bill H
aquila, I wasn't attempting to write the whole script for him, but merely to provide pointers as to how to use the $_SESSION array. I think my comments reveal that, as obviously I don't think that one would initialize and increment the variable at the same time.