473,625 Members | 2,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot get a session counter to work

Hi
I cannot get sessions to work.
I have the following code
</body>

<?
session_start() ;
$counter++;
print "You have visited this page $counter times during this session";
session_registe r("counter");
?>
</html>

And get the following output
Warning: session_start() [function.sessio n-start]: Cannot send session
cookie - headers already sent by (output started at /home/ted/
public_html/store/test/testsession.php :15) in /home/ted/public_html/
store/test/testsession.php on line 16

Warning: session_start() [function.sessio n-start]: Cannot send session
cache limiter - headers already sent (output started at /home/ted/
public_html/store/test/testsession.php :15) in /home/ted/public_html/
store/test/testsession.php on line 16
You have visited this page 1 times during this session

What is going wrong????

-Ted
Oct 5 '08 #1
3 2312
..oO(te*******@ gmail.com)
>Hi
I cannot get sessions to work.
I have the following code
</body>

<?
session_start( );
$counter++;
print "You have visited this page $counter times during this session";
session_regist er("counter");
?>
</html>

And get the following output
Warning: session_start() [function.sessio n-start]: Cannot send session
cookie - headers already sent by (output started at /home/ted/
public_html/store/test/testsession.php :15) in /home/ted/public_html/
store/test/testsession.php on line 16

Warning: session_start() [function.sessio n-start]: Cannot send session
cache limiter - headers already sent (output started at /home/ted/
public_html/store/test/testsession.php :15) in /home/ted/public_html/
store/test/testsession.php on line 16
You have visited this page 1 times during this session

What is going wrong????
A lot:

* You can't have any output before the session_start() call. Move it to
the top of the page _before_ any HTML. This should fix the "headers
already sent" issue.

* Use <?php instead of <?. Short open tags are unreliable.

* Check your php.ini and make sure that error_reporting is set to E_ALL
and display_errors are on. Your code above should throw an E_NOTICE
error.

* You don't need session_registe r() anymore. Use something like this to
properly initialize and increase the counter:

session_start() ;
if (!isset($_SESSI ON['counter'])) {
$_SESSION['counter'] = 1;
} else {
$_SESSION['counter']++;
}

Later simply use

print $_SESSION['counter'];

to output it.

HTH
Micha
Oct 5 '08 #2
-Thank you, it works now!!!!
-Ted
On Oct 5, 1:26*pm, Michael Fesser <neti...@gmx.de wrote:
.oO(tedpot...@g mail.com)


Hi
I cannot get sessions to work.
I have the following code
</body>
<?
session_start() ;
$counter++;
print "You have visited this page $counter times during this session";
session_registe r("counter");
?>
</html>
And get the following output
Warning: session_start() [function.sessio n-start]: Cannot send session
cookie - headers already sent by (output started at /home/ted/
public_html/store/test/testsession.php :15) in /home/ted/public_html/
store/test/testsession.php on line 16
Warning: session_start() [function.sessio n-start]: Cannot send session
cache limiter - headers already sent (output started at /home/ted/
public_html/store/test/testsession.php :15) in /home/ted/public_html/
store/test/testsession.php on line 16
You have visited this page 1 times during this session
What is going wrong????

A lot:

* You can't have any output before the session_start() call. Move it to
the top of the page _before_ any HTML. This should fix the "headers
already sent" issue.

* Use <?php instead of <?. Short open tags are unreliable.

* Check your php.ini and make sure that error_reporting is set to E_ALL
and display_errors are on. Your code above should throw an E_NOTICE
error.

* You don't need session_registe r() anymore. Use something like this to
properly initialize and increase the counter:

session_start() ;
if (!isset($_SESSI ON['counter'])) {
* $_SESSION['counter'] = 1;} else {

* $_SESSION['counter']++;

}

Later simply use

print $_SESSION['counter'];

to output it.

HTH
Micha- Hide quoted text -

- Show quoted text -
Oct 6 '08 #3
..oO(te*******@ gmail.com)
>-Thank you, it works now!!!!
You're welcome.

Micha
Oct 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
18315
by: BashiraInTrouble | last post by:
Hi Friends, I have tried almost everything but I cant seem to shrink the transaction log. Executing DBCC SQLPERF(LOGSPACE) gives me this info: Database Log Size (MB) Log Space Used (%) Status MY_eems 368.49219 16.034182 0 I made a complete backup of the database and transaction log and then executed this statement: DBCC SHRINKFILE (MYeems_log, 1)
8
5462
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
2
1857
by: Dinkster | last post by:
BackGround: - We are new to ASP - We thought we would experiment with using the session object (in proc) to store a small amount of data. - We get different results when cookieless is set to "true" veses "false". - Our simple test app involved updating a counter that was stored as an item in the Session Object. We allow resetting the counter to zero, and adding or subtracting one from the counter. - We were trying to determine when a "NEW...
4
2513
by: N. Demos | last post by:
Hello, I'm learning ASP.NET, and am having a strange problem with some example code from the book I'm using. The code increments and displays the value stored in a session variable when the "Add" button is clicked. In addition, the session variable is reset to zero when the "Empty" button is pressed. The problem is if the value is non-zero and the page is reloaded the value is incremented. It appears as if the "Add" onClick event...
5
5274
by: fbwhite | last post by:
I know this issue has been brought up many times, but I have tried many of the solutions to no avail. I wanted to give my specific case to see if someone could be of any help. We are using the sessionstate inproc mode and users are randomly losing their session. I do not believe it is happening across all users at one time. It seems to happen to different users at different times, but I am only going off heresay. The aspnet worker...
0
2106
by: SeanGallavan | last post by:
Our Environment: Two network load balanced (using Microsoft NLB software) webservers with session maintained in a SQL Server database. NLB is configured with no affinity settings and two network cards in each machine. Machine keys are identical on both webservers. There is no anti-virus software installed. SQL Server database is storing session information as specified by Microsoft guidelines.
10
3499
by: tshad | last post by:
I have been using the default session state (InProc) and have found that I have been loosing my information after a period of time (normally 20 minutes). Is there anyway to find out how much more time I have on a session? If I do a refresh, does reset the session clock? Do you have have to go to another page to reset the session timeout or will a postback also do it? This is important as we have a few pages that a user
6
1589
by: mosscliffe | last post by:
I am testing for how/when a page is posted back and I decided to use a ViewState variable in PageLoad to set up a counter, but it appears, the ViewState is cleared on each PageLoad. So then I used SESSION and that worked. Am I correct in assuming ViewState is cleared on each PageLoad or is my code incorrect. VIEWSTATE If IsNothing(ViewState("PbCounter")) Then
9
1460
by: Suman | last post by:
Hi All, I am trying to experiment with sessions and they seem to be resetting everytime i visit the page. here is the code of the page <?php session_start(); $count = $_SESSION + 1;
0
8253
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.