dekonstruct Posted December 11, 2006 Share Posted December 11, 2006 Hello, I'm quite a noob at all this php stuff, here's my problem:I have some simple strings:<?php$day = date(z);$counter = 1;echo $day;echo $day - $counter;?>and I'm wondering how to create a link that adds 1 to the counter everytime you click it. I know that ++ is incremental but php in links completely baffles me at the moment. any prodding in the right direction would be most appreciated. THANK YOU! Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/ Share on other sites More sharing options...
marcus Posted December 11, 2006 Share Posted December 11, 2006 You can do a session and have a value to it.[code]<?phpsession_start();if(isset($_SESSION['counted'])){$new = $_SESSION['counted'] + 1;$_SESSION['counted'] = $new;echo $_SESSION['counted'];}else {$_SESSION['counted'] = 1;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139357 Share on other sites More sharing options...
.josh Posted December 12, 2006 Share Posted December 12, 2006 note that sessions will only work for as long as the user's browser is open. if you are looking for a more permanent solution, you need to look into storing the number in a flatfile or a database, retrieving the # from there, and updating it when you click on it. Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139361 Share on other sites More sharing options...
dekonstruct Posted December 12, 2006 Author Share Posted December 12, 2006 hmmm sessions seem to be what I need. I tried one out and got this error: Warning: session_start(): Cannot send session cookie - headers already sent by... I don't see how anything is being sent when there's only one session on the darn page. I am viewing this off a server through firefox if that's any help to anyone. do i need to setup my server differently to be able to handle sessions perhaps?Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139373 Share on other sites More sharing options...
.josh Posted December 12, 2006 Share Posted December 12, 2006 it would help if you actually showed your code. But based on your error, you are attempting to output html before sending header information. You can't do that. Not even a blank line. headers must come before any html output. If you absolutely have to have html output before headers in your code, use ob_start() at the very beginning of your script and ob_end_flush() at the end. This will create a buffer for your html code so that the header info can be parsed first. Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139383 Share on other sites More sharing options...
dekonstruct Posted December 12, 2006 Author Share Posted December 12, 2006 [code]<?phpsession_start();if(isset($_SESSION['counted'])){$new = $_SESSION['counted'] + 1;$_SESSION['counted'] = $new;echo $_SESSION['counted'];}else {$_SESSION['counted'] = 1;}?>[/code]I just cut and pasted what mgallforever had posted... there's nothing else on the page, and it's called phptest.phphere's the error:[b]Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /xxx/xxx/xxx/xxx/xxx/xxx/xxx/test/phptest.php:1) in /xxx/xxx/xxx/xxx/xxx/xxx/xxx/test/phptest.php on line 2[/b]sorry if this all seems too easy... The only language-ish thing I've known before trying php was actionscript so I'm a little broken ;) Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139388 Share on other sites More sharing options...
.josh Posted December 12, 2006 Share Posted December 12, 2006 so you don't have anything before that <?php ? no blank lines? is this included in another file? Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139389 Share on other sites More sharing options...
dekonstruct Posted December 12, 2006 Author Share Posted December 12, 2006 nope absolutely nothing before it. I used notepad in WinXP maybe it needs to be a certain type of encoding? Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139403 Share on other sites More sharing options...
.josh Posted December 12, 2006 Share Posted December 12, 2006 nope. notepad is fine. for shits and grins i actually physically tried the code and it works fine for me. so you are telling me you don't have anything else in your script. what about this part?[code]<?php$day = date(z);$counter = 1;echo $day;echo $day - $counter;?>[/code]where is that? Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139404 Share on other sites More sharing options...
dekonstruct Posted December 12, 2006 Author Share Posted December 12, 2006 that code was actually embedded in a different page in the main body of some html. Oddly enough that code worked fine, but it was simple enough I guess. I am trying to figure out what flavor of php is installed on what kind of server at my host, methinks that might be the culprit. Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139405 Share on other sites More sharing options...
dekonstruct Posted December 12, 2006 Author Share Posted December 12, 2006 it would seem I'm running php 4.4.1 on a linux/apache server. would there be something with my server restricting sessions? Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139409 Share on other sites More sharing options...
dekonstruct Posted December 13, 2006 Author Share Posted December 13, 2006 Low and behold I finally found a fix to my problem. I found it here:[url=http://us2.php.net/function.session-start]http://us2.php.net/function.session-start[/url] Turns out that when you edit it in notepad, you shouldn't select utf-8 encoding because it puts two hidden characters before the document starts, thus you can't start a session. everything works beautifully for me, thanks everyone for your help!here's what the smart dude that found this out said: [quote] saykyo (mail at gmail dot com)12-Jul-2006 08:23Watch out for using UTF-8 encoding in your php scripts!I don't know about other enviroments, but in Windows XP, if you edit a document and set it to be UTF-8, the editor (notepad for exapmle) inserts two invisible bytes at the beginning of the file (they read FF FE in hex for me). I suppose this happens so Windows can identify the file as UTF-8.Since these two bytes are placed before anything else in the file, including <? ?> tags, when you execute the php script, they get outputed to the browser (even tough they won't be shown in the source of the result document, they're there!) before your php code gets a chance to run anything. This effectively cripples functions like session_start() ($_COOKIE to be exact).The solution is to save the php file in a different encoding, or to manually remove them (I prefer the former).I hope I helped somebody.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-140080 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.