Forum Moderators: coopster

Message Too Old, No Replies

Help! I need Cookie!

Cookies and Sessions don't work

         

RedBaron

6:32 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



I'm pretty new to PHP. It's definitely my first time using php cookies. At first I kept getting an error and found out I have to use the session_save_path because I don't have access to the php.ini file. My program still didn't work so I found the simple code below in an online tutorial and I tried to get it to work... no luck. The cookie is suppose to increment every time the page reloads but it doesn't. Is it my code, is it my server? I have a feeling it may be an id10t error. I would prefer to use sessions, but if I can get cookies to work I'll go with that. Any help will be GREATLY appreciated.

<?php
session_save_path("/nfs/cust/5/0/1/red/PHP/chem/sessions/a/tmp/");
session_start();

$_SESSION['count'] = $_POST['count'];

// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
echo "Count = ".$_SESSION['count']
?>

P.S. Btw, I also tried replacing $_SESSION with $HTTP_SESSION_VARS but still no luck.

RedBaron

7:25 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



I vow not to eat until I figure this out. Plz help! I'm hungry. Thanks in advance.

jatar_k

7:34 pm on Sep 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, is the $_SESSION getting anything at all?

have you tried outputting the contents of the SESSION to see if anything iss getting written to it?

RedBaron

10:58 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



the last line of the code...

echo "Count = ".$_SESSION['count']

produces the following output every time I hit reload.

Count = 0

jatar_k

11:06 pm on Sep 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about just dumping the whole session?

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

RedBaron

11:15 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



I get this:

Array
(
[count] => 0
)

jatar_k

1:22 am on Sep 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well then we are part way there, the session var is getting set, it just happens to not incrementing

a couple of error checking options then, we need to see what part of the if statement it is hitting since that is the portion that is incrementing the count var. Maybe try this

if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
echo 'count not set';
} else {
echo 'incrementing count var from ',$_SESSION['count'];
$_SESSION['count']++;
echo ' to ',$_SESSION['count'];
}

RedBaron

1:23 am on Sep 7, 2005 (gmt 0)

10+ Year Member



Yes! I can finally eat! I tried different block of code from another tutorial (http://builder.com.com/5102-31-5077713.html) and the code below works beautifully. So I'm going to model my code from their example.

Thanks jatar_k for taking the time to try and help me.