p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Chapter 8 session problem


Message #1 by "Eddy Hartshorn" <eddyh@f...> on Fri, 7 Feb 2003 16:12:59
Hey all, another newbie question here. Im trying to get the pagecount 
example working wiht regsiter globals as off. However im beggered if i can 
work out what i should be doing. all I get at the moment is the 
wonderful "The specified CGI application misbehaved by not returning a 
complete set of HTTP headers. The headers it did return are:" message. I 
think it might be something to do with the $GLOBALS being used or the 
$PHP_SELF but i dont know what. Cheers for the help guys.

Eddy
Message #2 by "Nikolai Devereaux" <yomama@u...> on Fri, 7 Feb 2003 13:28:12 -0800
> Hey all, another newbie question here. Im trying to get the pagecount
> example working wiht regsiter globals as off. However im beggered if i can
> work out what i should be doing. all I get at the moment is the
> wonderful "The specified CGI application misbehaved by not returning a
> complete set of HTTP headers. The headers it did return are:" message. I
> think it might be something to do with the $GLOBALS being used or the
> $PHP_SELF but i dont know what. Cheers for the help guys.

Hmm.  That's weird.  PHP should, by default, return at least the
"Content-type: text/html" header if you don't specify any others.

Can you include some of your code?  perhaps it's not cookies that's the
problem.

Look at my post (#2 in this thread) for a small example illustrating how to
use cookies.

    http://p2p.wrox.com/archive/beginning_php/2002-12/14.asp


Take care,

Nik

Message #3 by "Eddy Hartshorn" <eddyh@f...> on Sat, 8 Feb 2003 21:02:19
Hmm.  That's weird.  PHP should, by default, return at least the
"Content-type: text/html" header if you don't specify any others.

Can you include some of your code?  perhaps it's not cookies that's the
problem.

Look at my post (#2 in this thread) for a small example illustrating how 
to
use cookies.

    http://p2p.wrox.com/archive/beginning_php/2002-12/14.asp


Take care,

Nik


hi, ok then here comes the bizzarrenss i typed out the code again and 
didnt get that message, However i am confused with what code to be using 
witth the global register off. Below is a snippet of the code as it is in 
the book followed by guess as to what should be there. one thing i did 
notice was that the first line being echo'd, doesnt echo and the page 
counts dont increase. I know im being thick but hey im new here :D
original code----
<?php
session_register("view1count");
session_register("view2count");
session_register("view3count");
session_register("view4count");
?>

<?php
//page_count.php

if (!$view1count) $view1count = 0;
if (!$view2count) $view2count = 0;
if (!$view3count) $view3count = 0;
if (!$view4count) $view4count = 0;

echo "<HTML><HEAD><TITLE>Web Page Hit Counter</TITLE></HEAD><BODY>";

if ($whichpage) {
  echo "<B>You are currently on page $whichpage.</B><BR><BR>\n";
  $GLOBALS["view${whichpage}count"]++;
}

for ($i = 1; $i <= 4; $i++) {

  if ($whichpage == $i) {
    echo "<B><A HREF=\"$PHP_SELF?".SID."&whichpage=$i\">Page $i</A></B>";
  } else {
    echo "<A HREF=\"$PHP_SELF?".SID."&whichpage=$i\">Page $i</A>";
  }
  echo ", which you have chosen ".$GLOBALS["view${i}count"]." 
times.<BR>\n";

my butchered code---------
<?php
session_register("view1count");
session_register("view2count");
session_register("view3count");
session_register("view4count");
?>
<?php
//page_count.php

if (!$_SESSION['view1count']) $view1count = 0;
if (!$_SESSION['view2count']) $view2count = 0;
if (!$_SESSION['view3count']) $view3count = 0;
if (!$_SESSION['view4count']) $view4count = 0;

echo "<HTML><HEAD><TITLE>Web Page Hit Counter</TITLE></HEAD><BODY>";

if ($whichpage) {
  echo "<B>You are currently on page $whichpage.</B><BR><BR>\n";
  $GLOBALS["view${whichpage}count"]++;
}

for ($i = 1; $i <= 4; $i++) {

  if ($whichpage == $i) {
    echo "<B><A HREF=\"{$_SERVER['PHP_SELF']}?".SID."&whichpage=$i\">Page 
$i</A></B>";
  } else {
    echo "<A HREF=\"{$_SERVER['PHP_SELF']}?".SID."&whichpage=$i\">Page 
$i</A>";
  }
  echo ", which you have chosen ".$GLOBALS["view${i}count"]." 
times.<BR>\n";
}

Thanks again for all the help you provide to everyone.
Eddy
Message #4 by "Eddy Hartshorn" <eddyh@f...> on Mon, 10 Feb 2003 10:32:31
Ok, so i might be getting somewhere, im just not quite sure where. After 
noticing that the whichpage was being posted like a $_GET statement would 
i changed the code accordingly and now that part of the script works and 
displays what page I am on. However, now the one bit I cant get my head 
round is this bit ---

$GLOBALS["view${whichpage}count"]++;
}
for ($i = 1; $i <= 4; $i++) {

  if ($whichpage == $i) {
    echo "<B><A HREF=\"{$_SERVER['PHP_SELF']}?".SID."&whichpage=$i\">Page 
$i</A></B>";
  } else {
    echo "<A HREF=\"{$_SERVER['PHP_SELF']}?".SID."&whichpage=$i\">Page 
$i</A>";
  }
  echo ", which you have chosen ".$GLOBALS["view${i}count"]." 
times.<BR>\n";
}

All that happens now is the page count says i have chosen the page 1 time 
when i click on that page and changes to 1 on the other pages when i click 
on them but it doesnt increase when i click on it more. Any ideas.
Thanks
Eddy
Message #5 by "Nikolai Devereaux" <yomama@u...> on Mon, 10 Feb 2003 20:00:55 -0800
> hi, ok then here comes the bizzarrenss i typed out the code again and
> didnt get that message, However i am confused with what code to be using
> witth the global register off. Below is a snippet of the code as it is in
> the book followed by guess as to what should be there. one thing i did
> notice was that the first line being echo'd, doesnt echo and the page
> counts dont increase. I know im being thick but hey im new here :D


Hmm.  Seems you're confusing when to use $GLOBALS, $_SESSION, and $_GET.  If
you get into the habit of programming with register_globals = off, you'll
realize that your scripts are a lot less confusing because you're forcing
yourself to organize data instead of trying to remember which globals belong
where (session, forms, cookies, etc)

new code:

<?php
session_start();

// initialize session counters.
for($i = 1; $i <= 4; ++$i)
{
    $idxname = "view{$i}count";

    if(! isset($_SESSION[$idxname]))
    {
        $_SESSION[$idxname] = 0;
    }
}

// initialize current_page.  If $_GET['whichpage'] not submitted, set to 1.
$current_page = (isset($_GET['whichpage'])? $_GET['whichpage'] : 1;

echo "<HTML><HEAD><TITLE>Web Page Hit Counter</TITLE></HEAD><BODY>";


// Display current page and increment counter.
echo "<B>You are currently on page $current_page.</B><BR><BR>\n";
$_SESSION["view{$current_page}count"]++;



// Display links:
for ($i = 1; $i <= 4; $i++)
{
    if ($current_page == $i)
    {
        echo "<B><A HREF=\"$PHP_SELF?".SID."&whichpage=$i\">Page $i</A></B>";
    }
    else
    {
        echo "<A HREF=\"$PHP_SELF?".SID."&whichpage=$i\">Page $i</A>";
    }
    echo ", which you have chosen ".$_SESSION["view${i}count"]."
times.<BR>\n";
}


?>


I haven't tested it, but it looks reasonable to me.


Take care,

Nik

Message #6 by "digihoo" <digihoo@h...> on Wed, 12 Feb 2003 14:48:32
> Hmm.  That's weird.  PHP should, by default, return at least the
"> Content-type: text/html" header if you don't specify any others.

> Can you include some of your code?  perhaps it's not cookies that's the
p> roblem.

> Look at my post (#2 in this thread) for a small example illustrating how 
t> o
u> se cookies.

>     http://p2p.wrox.com/archive/beginning_php/2002-12/14.asp

> 
T> ake care,

> Nik

> 
h> i, ok then here comes the bizzarrenss i typed out the code again and 
d> idnt get that message, However i am confused with what code to be using 
w> itth the global register off. Below is a snippet of the code as it is 
in 
t> he book followed by guess as to what should be there. one thing i did 
n> otice was that the first line being echo'd, doesnt echo and the page 
c> ounts dont increase. I know im being thick but hey im new here :D
o> riginal code----
<> ?php
s> ession_register("view1count");
s> ession_register("view2count");
s> ession_register("view3count");
s> ession_register("view4count");
?> >

> <?php
/> /page_count.php

> if (!$view1count) $view1count = 0;
i> f (!$view2count) $view2count = 0;
i> f (!$view3count) $view3count = 0;
i> f (!$view4count) $view4count = 0;

> echo "<HTML><HEAD><TITLE>Web Page Hit Counter</TITLE></HEAD><BODY>";

> if ($whichpage) {
 >  echo "<B>You are currently on page $whichpage.</B><BR><BR>\n";
 >  $GLOBALS["view${whichpage}count"]++;
}> 

> for ($i = 1; $i <= 4; $i++) {

>   if ($whichpage == $i) {
 >    echo "<B><A HREF=\"$PHP_SELF?".SID."&whichpage=$i\">Page $i</A></B>";
 >  } else {
 >    echo "<A HREF=\"$PHP_SELF?".SID."&whichpage=$i\">Page $i</A>";
 >  }
 >  echo ", which you have chosen ".$GLOBALS["view${i}count"]." 
t> imes.<BR>\n";

> my butchered code---------
<> ?php
s> ession_register("view1count");
s> ession_register("view2count");
s> ession_register("view3count");
s> ession_register("view4count");
?> >
<> ?php
/> /page_count.php

> if (!$_SESSION['view1count']) $view1count = 0;
i> f (!$_SESSION['view2count']) $view2count = 0;
i> f (!$_SESSION['view3count']) $view3count = 0;
i> f (!$_SESSION['view4count']) $view4count = 0;

> echo "<HTML><HEAD><TITLE>Web Page Hit Counter</TITLE></HEAD><BODY>";

> if ($whichpage) {
 >  echo "<B>You are currently on page $whichpage.</B><BR><BR>\n";
 >  $GLOBALS["view${whichpage}count"]++;
}> 

> for ($i = 1; $i <= 4; $i++) {

>   if ($whichpage == $i) {
 >    echo "<B><A HREF=\"{$_SERVER
['PHP_SELF']}?".SID."&whichpage=$i\">Page 
$> i</A></B>";
 >  } else {
 >    echo "<A HREF=\"{$_SERVER['PHP_SELF']}?".SID."&whichpage=$i\">Page 
$> i</A>";
 >  }
 >  echo ", which you have chosen ".$GLOBALS["view${i}count"]." 
t> imes.<BR>\n";
}> 

> Thanks again for all the help you provide to everyone.
E> ddy
Message #7 by "digihoo" <digihoo@h...> on Wed, 12 Feb 2003 14:54:56
< sorry for the above empty post. >

what i wanted to tell you, Eddy (in case you are still having this 
problem), is that it looks like your session variables are not persisting. 

this is probably because sessions are not successfully enabled on your 
server. check your php.ini file "session.save_path" variable to make sure 
that:

1. the file exists
2. you have write access to it.

hth

Message #8 by "Eddy Hartshorn" <eddyh@f...> on Thu, 13 Feb 2003 10:07:22
> 
<>  sorry for the above empty post. >

> what i wanted to tell you, Eddy (in case you are still having this 
p> roblem), is that it looks like your session variables are not 
persisting. 

> this is probably because sessions are not successfully enabled on your 
s> erver. check your php.ini file "session.save_path" variable to make 
sure 
t> hat:

> 1. the file exists
2> . you have write access to it.

> hth

HI, after reading a few things posted by nic and actually thinging about 
it. I came up with the code below works. Whether it is the "correct" way 
of getting it to work or not I don't know. But it works for me :D
<?php
session_register("view1count");
session_register("view2count");
session_register("view3count");
session_register("view4count");
?>
<?php
//page_count.php
$whichpage = $_GET['whichpage'];
if (!$_SESSION['view1count']) $_SESSION['view1count'] = 0;
if (!$_SESSION['view2count']) $_SESSION['view2count'] = 0;
if (!$_SESSION['view3count']) $_SESSION['view3count'] = 0;
if (!$_SESSION['view4count']) $_SESSION['view4count'] = 0;

echo "<HTML><HEAD><TITLE>Web Page Hit Counter</TITLE></HEAD><BODY>";

if ($whichpage) {
  echo "<B>You are currently on page $whichpage.</B><BR><BR>\n";
  $_SESSION["view${whichpage}count"]++;
}

for ($i = 1; $i <= 4; $i++) {

  if ($whichpage == $i) {
    echo "<B><A HREF=\"{$_SERVER['PHP_SELF']}?".SID."&whichpage=$i\">Page 
$i</A></B>";
  } else {
    echo "<A HREF=\"{$_SERVER['PHP_SELF']}?".SID."&whichpage=$i\">Page 
$i</A>";
  }
  echo ", which you have chosen ".$_SESSION["view${i}count"]." 
times.<BR>\n";
}

echo "\n\n<BR><BR>\n\n";
echo "</BODY></HTML>";

?>
Message #9 by "Nikolai Devereaux" <yomama@u...> on Thu, 13 Feb 2003 12:08:24 -0800
> HI, after reading a few things posted by nic and actually thinging about
> it. I came up with the code below works. Whether it is the "correct" way
> of getting it to work or not I don't know. But it works for me :D


Glad it's working, but I should mention something:  Don't mix
session_register() and $_SESSION.  In fact, don't use session_register()!
Replace your session_register() calls with a single call to session_start().

Other than that, I don't see too much difference between your working version
and the one I gave you, so that's reassuring that I still know what I'm
talking about!  =)


Take care,

Nik


  Return to Index