Jump to content

Recommended Posts

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!
Link to comment
https://forums.phpfreaks.com/topic/30285-simple-problem/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139361
Share on other sites

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!
Link to comment
https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139373
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139383
Share on other sites

[code]<?php
session_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.php

here'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  ;)
Link to comment
https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139388
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139404
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-139405
Share on other sites

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:23
Watch 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]
Link to comment
https://forums.phpfreaks.com/topic/30285-simple-problem/#findComment-140080
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.