FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Re: Counter reset or not? UPDATE
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Counter reset or not? UPDATE [message #182492] Thu, 08 August 2013 19:10 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
Hi,

In troubleshooting the above, I've ended up creating two pages of as
minimal code as I know how to make it <g>. I now have working code,
BUT(t), it's throwing a warning I don't know how to address. ALL work is
being done on my Local Apache Server (XAMPP) and PHP 5.3 (same as my
Remote Server uses).

The session is almost working, and it IS incrementing properly. This is
a code sample I picked up at php.net and it's supposedly applicable to
version 4 and 5. I've removed the href to link to the next page and
instead am now using a Submit Button: Same identical results whether the
link line exists or not so I removed it and things still work the same.
By being as basic with the code as I have been, I've removed the
implications of anything else in the code effecting it.

Page 1: PHP code:\
============
<?php
session_start();
// counter1.php
if (empty($_SESSION['cntr'])) {
$_SESSION['cntr'] = 1;
} else {
$_SESSION['cntr']++;
}
?>
<p>
Hello visitor, you have seen this page <?php echo $_SESSION['cntr']; ?>
times.
</p>
<form action="counter2.php" method="post">
Type something: <input type="text" name = "type" <br />
<p> <input type="submit" value="NEXT">
<input type= "reset" value="Clear Form" /> <br />
</form>
</body>
</html>
======= end =========

Page 2: PHP:
<?php
session_start();
// counter2.php
echo $_SESSION["cntr"]. " Before increment. <br />";
$_SESSION["cntr"] = $_SESSION["cntr"]+ "1";
echo $_SESSION["cntr"] . " After increment";
?>
Next (Submit button) here.
========== end ==========


It's acting strangely, at least to me. The first time I run, nothing
initialized, Counter1.php, the results are:

======== start =================
Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at
C:\xampp\htdocs\counter1.php:2) in C:\xampp\htdocs\counter1.php on line 3

Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
C:\xampp\htdocs\counter1.php:2) in C:\xampp\htdocs\counter1.php on line 3

Hello visitor, you have seen this page 1 times.

Type something:

============ end =============

The input statement and Forms structure is only so I can use Submit
instead of the link the original code used.
======== start =================
If I click Submit (Next), the second page displays:

Notice: Undefined index: cntr in C:\xampp\htdocs\counter2.php on line 4
Before increment: <nothing>

Notice: Undefined index: cntr in C:\xampp\htdocs\counter2.php on line 5
1 After increment

============ end =============

Now, reloading/refreshing counter1.php results in the following screen
display:

============== start ==========
Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
C:\xampp\htdocs\counter1.php:2) in C:\xampp\htdocs\counter1.php on line 3

Hello visitor, you have seen this page 2 times.

Next (Submit)
=============== end =========

Now counter2.php, the 2nd page, works as I expected and shows:
=========== start ============
2 Before increment.
3 After increment
=========== End =============

And so on, adinfinitum.
So the counter IS incrementing and displaying properly on the
counter1.php page with the exception now only one Warning appears, not
two as in the first time it's run, where there were two statements
returned.

An interesting sidelight is:
Should I run a different script, the original one I asked questions
with, IT ALSO shows the incrementing count! I suppose it makes sense
since the session is never Unset in the new code, but a cute happening
anyway IMO; something to keep in mind!

So, to synopsize:
The very first time counter1.php runs, the counter show "1" as
expected, and two Warnings about headers already sent.
Clicking to go to the next page, show the two errors as above,
And after that, the first page shows only one Warning, and the 2nd
page works flawlessly. That will continue until the session is closed or
the browser closed.

Any solutions, thoughts, comments certainly appreciated!

Twayne`
Re: Counter reset or not? UPDATE [message #182497 is a reply to message #182492] Thu, 08 August 2013 22:28 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 8/8/2013 12:10 PM, Twayne wrote:
> Hi,
>
> In troubleshooting the above, I've ended up creating two pages of as
> minimal code as I know how to make it <g>. I now have working code,
> BUT(t), it's throwing a warning I don't know how to address. ALL work is
> being done on my Local Apache Server (XAMPP) and PHP 5.3 (same as my
> Remote Server uses).
>
> The session is almost working, and it IS incrementing properly. This is
> a code sample I picked up at php.net and it's supposedly applicable to
> version 4 and 5. I've removed the href to link to the next page and
> instead am now using a Submit Button: Same identical results whether the
> link line exists or not so I removed it and things still work the same.
> By being as basic with the code as I have been, I've removed the
> implications of anything else in the code effecting it.
>
> Page 1: PHP code:\
> ============
> <?php
> session_start();
> // counter1.php
> if (empty($_SESSION['cntr'])) {
> $_SESSION['cntr'] = 1;
> } else {
> $_SESSION['cntr']++;
> }
> ?>
> <p>
> Hello visitor, you have seen this page <?php echo $_SESSION['cntr']; ?>
> times.
> </p>
> <form action="counter2.php" method="post">
> Type something: <input type="text" name = "type" <br />
> <p> <input type="submit" value="NEXT">
> <input type= "reset" value="Clear Form" /> <br />
> </form>
> </body>
> </html>
> ======= end =========
>
> Page 2: PHP:
> <?php
> session_start();
> // counter2.php
> echo $_SESSION["cntr"]. " Before increment. <br />";
> $_SESSION["cntr"] = $_SESSION["cntr"]+ "1";
> echo $_SESSION["cntr"] . " After increment";
> ?>
> Next (Submit button) here.
> ========== end ==========
>
>
> It's acting strangely, at least to me. The first time I run, nothing
> initialized, Counter1.php, the results are:
>
> ======== start =================
> Warning: session_start() [function.session-start]: Cannot send session
> cookie - headers already sent by (output started at
> C:\xampp\htdocs\counter1.php:2) in C:\xampp\htdocs\counter1.php on line 3
>
> Warning: session_start() [function.session-start]: Cannot send session
> cache limiter - headers already sent (output started at
> C:\xampp\htdocs\counter1.php:2) in C:\xampp\htdocs\counter1.php on line 3
>
> Hello visitor, you have seen this page 1 times.
>
> Type something:
>
> ============ end =============
>
> The input statement and Forms structure is only so I can use Submit
> instead of the link the original code used.
> ======== start =================
> If I click Submit (Next), the second page displays:
>
> Notice: Undefined index: cntr in C:\xampp\htdocs\counter2.php on line 4
> Before increment: <nothing>
>
> Notice: Undefined index: cntr in C:\xampp\htdocs\counter2.php on line 5
> 1 After increment
>
> ============ end =============
>
> Now, reloading/refreshing counter1.php results in the following screen
> display:
>
> ============== start ==========
> Warning: session_start() [function.session-start]: Cannot send session
> cache limiter - headers already sent (output started at
> C:\xampp\htdocs\counter1.php:2) in C:\xampp\htdocs\counter1.php on line 3
>
> Hello visitor, you have seen this page 2 times.
>
> Next (Submit)
> =============== end =========
>
> Now counter2.php, the 2nd page, works as I expected and shows:
> =========== start ============
> 2 Before increment.
> 3 After increment
> =========== End =============
>
> And so on, adinfinitum.
> So the counter IS incrementing and displaying properly on the
> counter1.php page with the exception now only one Warning appears, not
> two as in the first time it's run, where there were two statements
> returned.
>
> An interesting sidelight is:
> Should I run a different script, the original one I asked questions
> with, IT ALSO shows the incrementing count! I suppose it makes sense
> since the session is never Unset in the new code, but a cute happening
> anyway IMO; something to keep in mind!
>
> So, to synopsize:
> The very first time counter1.php runs, the counter show "1" as
> expected, and two Warnings about headers already sent.
> Clicking to go to the next page, show the two errors as above,
> And after that, the first page shows only one Warning, and the 2nd
> page works flawlessly. That will continue until the session is closed or
> the browser closed.
>
> Any solutions, thoughts, comments certainly appreciated!
>
> Twayne`
>
>
>
>
>
>
>
>
>
>

I have not read the entire post but just from the error it seems you may
be sending output PRIOR to session_start.

session_start MUST be the first line after the PHP delimiter.

*************
<?php
session_start();


.....
?>
****************

One of the biggest overlooked mistakes is having a blank space prior to
the delimiter. This is sometimes caused by the text editor.




***************
<- blank space will be sent.
<?php
session_start();


?>
*******************
Re: Counter reset or not? UPDATE [message #182499 is a reply to message #182497] Thu, 08 August 2013 22:58 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
Scott Johnson wrote:

> One of the biggest overlooked mistakes is having a blank space prior to
> the delimiter. This is sometimes caused by the text editor.

If one is working with UTF-8 encoded PHP files, there may be even an
"invisible" BOM before the opening PHP tag; so one should always save
those files without a BOM.

However, if a BOM or any whitespace in front of the opening PHP tag
causes any trouble is mostly depending on the PHP ini setting
output_buffering[1]. I usually set this to "Off" in the development
environment, so I can catch these "headers already sent" errors early.

[1]
< http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffer ing>

--
Christoph M. Becker
Re: Counter reset or not? UPDATE [message #182503 is a reply to message #182499] Fri, 09 August 2013 00:44 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/8/2013 6:58 PM, Christoph Michael Becker wrote:
> Scott Johnson wrote:
>
>> One of the biggest overlooked mistakes is having a blank space prior to
>> the delimiter. This is sometimes caused by the text editor.
>
> If one is working with UTF-8 encoded PHP files, there may be even an
> "invisible" BOM before the opening PHP tag; so one should always save
> those files without a BOM.
>
> However, if a BOM or any whitespace in front of the opening PHP tag
> causes any trouble is mostly depending on the PHP ini setting
> output_buffering[1]. I usually set this to "Off" in the development
> environment, so I can catch these "headers already sent" errors early.
>
> [1]
> < http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffer ing>
>

In general, buffering should be set to off in production systems. With
proper design of your code, you don't need output buffering, and it just
adds one more layer of processing to the scripts.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Counter reset or not? UPDATE [message #182516 is a reply to message #182497] Fri, 09 August 2013 18:05 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-08-08 6:28 PM, Scott Johnson wrote:
> On 8/8/2013 12:10 PM, Twayne wrote:
>> Hi,
>>
....

>> Page 1: PHP code:\
>> ============
>> <?php
>> session_start();
>> // counter1.php
>> if (empty($_SESSION['cntr'])) {
>> $_SESSION['cntr'] = 1;
>> } else {
>> $_SESSION['cntr']++;
>> }
>> ?>
>> <p>
>> Hello visitor, you have seen this page <?php echo $_SESSION['cntr']; ?>
>> times.
>> </p>
>> <form action="counter2.php" method="post">
>> Type something: <input type="text" name = "type" <br />
>> <p> <input type="submit" value="NEXT">
>> <input type= "reset" value="Clear Form" /> <br />
>> </form>
>> </body>
>> </html>
>> ======= end =========
>>
>> Page 2: PHP:
>> <?php
>> session_start();
>> // counter2.php
>> echo $_SESSION["cntr"]. " Before increment. <br />";
>> $_SESSION["cntr"] = $_SESSION["cntr"]+ "1";
>> echo $_SESSION["cntr"] . " After increment";
>> ?>
>> Next (Submit button) here.
>> ========== end ==========
>>
>>

....

>>
>> Any solutions, thoughts, comments certainly appreciated!
>>
>> Twayne`

>>
>
> I have not read the entire post but just from the error it seems you may
> be sending output PRIOR to session_start.
>
> session_start MUST be the first line after the PHP delimiter.
>
> *************
> <?php
> session_start();
>
>
> ....
> ?>
> ****************
>
> One of the biggest overlooked mistakes is having a blank space prior to
> the delimiter. This is sometimes caused by the text editor.
>
>
>
>
> ***************
> <- blank space will be sent.
> <?php
> session_start();
>
>
> ?>
> *******************

Thanks for that, really, although I have it properly positioned.

The situation is now resolved, although I cannot (easily) tell you what
changes I made; I forgot to zip them up.
Basically I deleted the whole code and rewrote from scratch, as I
thought it should be done. And that started to work flawlessly. The
basic code is now:
==========
if(isset($_SESSION['reload']))
$_SESSION['reload']=$_SESSION['reload']+1;
else
$_SESSION['reload']=0;
// echo "This Page reloads = ". $_SESSION['reload']."<br />";
if($_SESSION['reload'] >= 3) {
echo "You have reloaded this page too many times. Statistics indicate
that you <br />most likely are not a legitimate visitor. If you are,
close and reopen your <br />browser, and go to the website, to restart
the form process <br /> after a half hour or so." ;
session_destroy();
die();
}
===========

Page reloads isn't as good as getting specific errors, but I'm working
on that too.

So, thanks anyway, it's appreciated input.

Twayne`
Re: Counter reset or not? UPDATE [message #182517 is a reply to message #182499] Fri, 09 August 2013 18:07 Go to previous message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 2013-08-08 6:58 PM, Christoph Michael Becker wrote:
> Scott Johnson wrote:
>
>> One of the biggest overlooked mistakes is having a blank space prior to
>> the delimiter. This is sometimes caused by the text editor.
>
> If one is working with UTF-8 encoded PHP files, there may be even an
> "invisible" BOM before the opening PHP tag; so one should always save
> those files without a BOM.
>
> However, if a BOM or any whitespace in front of the opening PHP tag
> causes any trouble is mostly depending on the PHP ini setting
> output_buffering[1]. I usually set this to "Off" in the development
> environment, so I can catch these "headers already sent" errors early.
>
> [1]
> < http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffer ing>
>

Sounds like good information; thanks for that.

Twayne`
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: sql how to have the total rows number in this case (this is correct question)
Next Topic: fetching value from related multiple rows & columns in PHP SQL
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 04:03:13 GMT 2024

Total time taken to generate the page: 0.02501 seconds