PHP sessions frustration

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

phpNUKED
Forum Newbie
Posts: 7
Joined: Mon Dec 08, 2003 1:28 pm

PHP sessions frustration

Post 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.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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.
phpNUKED
Forum Newbie
Posts: 7
Joined: Mon Dec 08, 2003 1:28 pm

done

Post by phpNUKED »

Globals already turned on.
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post 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; 
?>
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

this line

Code: Select all

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

Code: Select all

$count = $_SESSION['count']++;
Mark
phpNUKED
Forum Newbie
Posts: 7
Joined: Mon Dec 08, 2003 1:28 pm

Post 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...
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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?
phpNUKED
Forum Newbie
Posts: 7
Joined: Mon Dec 08, 2003 1:28 pm

Post by phpNUKED »

Hey micro,

From what I understand, PHP variables dont require initialization as do c++ variables.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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?
phpNUKED
Forum Newbie
Posts: 7
Joined: Mon Dec 08, 2003 1:28 pm

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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
phpNUKED
Forum Newbie
Posts: 7
Joined: Mon Dec 08, 2003 1:28 pm

Post by phpNUKED »

THx all for your assistance.
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post 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..
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
Post Reply