stolzussen Posted November 19, 2006 Share Posted November 19, 2006 Hi everyone this is my first post. I have just recently entered the world of PHP coding and as anyone would expect i am having some problems. Cutting to the chase, here is my misunderstanding [hr]I have this very simple script:[b]<!doctype html public "-//W3C//DTD HTML 4.0//EN"><html><head> <title>Untitled web-page</title></head><body><?php// initialize a sessionsession_start();// increment a session counter$_SESSION['counter']++;// print valueecho "You have viewed this page " . $_SESSION['counter'] . " times";?></body></html>[/b]In the php.ini file i have the session_savepath to a local folder on my computer. I need to use a specific destination related to the webserver in order for the script to work properly?My problem is that i always get the same value each time i load the page, namely 1, and when i check the sessions folder i see a new session file is created each time i load the page. What should i change?I am running Apache 2 webserver, PHP 5. I've tried browsing with Firefox and IE but i have the same problem.Thanks you. Link to comment https://forums.phpfreaks.com/topic/27772-session-related-question-noob-here-dont-shoot/ Share on other sites More sharing options...
esukf Posted November 19, 2006 Share Posted November 19, 2006 [code]<?phpsession_start();?><!doctype html public "-//W3C//DTD HTML 4.0//EN"><html><head> <title>Untitled web-page</title></head><body><?php// increment a session counter$_SESSION['counter']++;// print valueecho "You have viewed this page " . $_SESSION['counter'] . " times";?></body></html>[/code]If you didn't get any errors loading your code, you should set error_reporting = E_ALL & ~E_NOTICE in php.ini, it would have told you what the problem was. Link to comment https://forums.phpfreaks.com/topic/27772-session-related-question-noob-here-dont-shoot/#findComment-127046 Share on other sites More sharing options...
stolzussen Posted November 19, 2006 Author Share Posted November 19, 2006 Good suggestion. I get the error:Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\NETSERVER\www\counter.php:7) in C:\NETSERVER\www\counter.php on line 10Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\NETSERVER\www\counter.php:7) in C:\NETSERVER\www\counter.php on line 10You have viewed this page 1 timesI'll look that up in the FAQ, i noticed many people get this error. Link to comment https://forums.phpfreaks.com/topic/27772-session-related-question-noob-here-dont-shoot/#findComment-127110 Share on other sites More sharing options...
Orio Posted November 19, 2006 Share Posted November 19, 2006 Make sure there's no output before the opening "<?php"- even not a single white space or line a line break.Orio. Link to comment https://forums.phpfreaks.com/topic/27772-session-related-question-noob-here-dont-shoot/#findComment-127111 Share on other sites More sharing options...
stolzussen Posted November 19, 2006 Author Share Posted November 19, 2006 [code]<!doctype html public "-//W3C//DTD HTML 4.0//EN"><html><head> <title>Untitled web-page</title></head><body><?php// initialize a sessionsession_start();// increment a session counter$_SESSION['counter']++;// print valueecho "You have viewed this page " . $_SESSION['counter'] . " times";?></body></html>[/code]Tis is exactly how my php file looks like. Should i also omit the <body> tags? Link to comment https://forums.phpfreaks.com/topic/27772-session-related-question-noob-here-dont-shoot/#findComment-127159 Share on other sites More sharing options...
stolzussen Posted November 19, 2006 Author Share Posted November 19, 2006 I completely removed all html tags from the document and it seems to work.. So no HTML at all? That's a big lesson learnt. Thanks a lot guys. Glad i worked it out. Great forum.. i'll be posting a lot of questions here.... ;D Link to comment https://forums.phpfreaks.com/topic/27772-session-related-question-noob-here-dont-shoot/#findComment-127164 Share on other sites More sharing options...
jawapro Posted November 19, 2006 Share Posted November 19, 2006 You can have HTML.You just cant have any BEFORE the session_start();Why? Well its really because its part of the header, and when you output something else, it closes the header. But you really dont need to know that - just know that the session_start() needs to be above any output (i.e. HTML).So as long as you do the session start at the top - it should all work fine. Such as the following.[code]<?php// initialize a sessionsession_start();?><!doctype html public "-//W3C//DTD HTML 4.0//EN"><html><head> <title>Untitled web-page</title></head><body><?php// increment a session counter$_SESSION['counter']++;// print valueecho "You have viewed this page " . $_SESSION['counter'] . " times";?></body></html>[/code]Which works fine on my server.JP Link to comment https://forums.phpfreaks.com/topic/27772-session-related-question-noob-here-dont-shoot/#findComment-127211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.