Be a Supporter!

Retreiving a COOKIE in PHP!

  • 348 Views
  • 14 Replies
New Topic Respond to this Topic
RichL
RichL
  • Member since: Apr. 2, 2000
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Retreiving a COOKIE in PHP! 2001-08-06 09:44:42 Reply

Firstly- thank god the BBS is back up ;)!

I have a simple forum, recently people have been spamming it and it takes time to delete their entries! I know how to set a cookie, and this is what i have:

setcookie ("eforum", "checkpost",time()+3600);

But i have no idea how to retrive that cookie (if it has been set) and display a message saying they cannot post twice in a hour! Say i have a "formprocess.php" page. The Setcookie code is right at the top. So when they post a message it will set a cookie on their machine. I need the code to put under the Setcookie code which will check for that cookie- and not allow them to post again!

Im sure its simple enough- i cant find any help on it anywhere else!

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-06 14:03:22 Reply

At 8/6/01 09:44 AM, RichL wrote: I have a simple forum, recently people have been spamming it and it takes time to delete their entries! I know how to set a cookie, and this is what i have:

setcookie ("eforum", "checkpost",time()+3600);

But i have no idea how to retrive that cookie (if it has been set) and display a message saying they cannot post twice in a hour! Say i have a "formprocess.php" page. The Setcookie code is right at the top. So when they post a message it will set a cookie on their machine. I need the code to put under the Setcookie code which will check for that cookie- and not allow them to post again!

Im sure its simple enough- i cant find any help on it anywhere else!

You could have tried looking on the PHP quickref website, which has loads of examples.

Anyway, here goes:

if (isset($eforum)) {
// Error
} else {
// Do the vandango
}

That's all you needed. Cookies are automatically removed after the time allotted (and if a time isn't specified in the first place, the cookie expires when the browser window is closed).

Trouble with cookies, as with javascript, is the fact that people are able to set their browser not to use them - so you may be best off tracking IP's as well.

Pecos
Pecos
  • Member since: Dec. 29, 1999
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-06 15:12:49 Reply

At 8/6/01 02:03 PM, liljim wrote: Trouble with cookies, as with javascript, is the fact that people are able to set their browser not to use them - so you may be best off tracking IP's as well.

Right on, Jimster. Cookies are not 100% reliable. That's why I'd prefer using the session object (in Java anyway, I don't know if there's something similar in PHP). That way the session is kept track of until the browser closes or the session times out.

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-06 17:09:30 Reply

At 8/6/01 03:12 PM, MassiveBit wrote: I don't know if there's something similar in PHP). That way the session is kept track of until the browser closes or the session times out.

Are they anything like this function, and associated functions?

Pecos
Pecos
  • Member since: Dec. 29, 1999
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-06 19:54:41 Reply

At 8/6/01 05:09 PM, liljim wrote:
At 8/6/01 03:12 PM, MassiveBit wrote: I don't know if there's something similar in PHP). That way the session is kept track of until the browser closes or the session times out.
Are they anything like this function, and associated functions?

Yeah, that might be it.

RichL
RichL
  • Member since: Apr. 2, 2000
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 07:32:20 Reply

At 8/6/01 07:54 PM, MassiveBit wrote:
At 8/6/01 05:09 PM, liljim wrote:
At 8/6/01 03:12 PM, MassiveBit wrote: I don't know if there's something similar in PHP). That way the session is kept track of until the browser closes or the session times out.
Are they anything like this function, and associated functions?
Yeah, that might be it.

Right- heres the thing. I have got my cookie setting page working, it sets a cookie- and i have set the script to kill the page from loading if the cookie is already set (therefore stopping users from posting..assuming they have coookies enabled), now- when i put the code into my forum submit template, it isnt processed- and when you view the source (simply View > View Source) it gives the script as it is (e.g. //? cookie set...php stuff ?\) and does not set the cookie! The pages WERE named .ihtml (templates) but i renamed them all to .php to try and get that script working..but it still doesnt process it!

Please- any ideas, i dont understand why it doesnt work now!

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 07:40:09 Reply

At 8/7/01 07:32 AM, RichL wrote: Please- any ideas, i dont understand why it doesnt work now!

Post a url, and a place to view your code. It's no good going into these things blind.

RichL
RichL
  • Member since: Apr. 2, 2000
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 09:19:43 Reply

At 8/7/01 07:40 AM, liljim wrote:
At 8/7/01 07:32 AM, RichL wrote: Please- any ideas, i dont understand why it doesnt work now!
Post a url, and a place to view your code. It's no good going into these things blind.

Im a tad weary of keeping it up, as im testing the code out blankly straight onto my sites forum! If you goto: http://echeats.net/phorum (its not actually the PHP program phorum) and click on "New Thread"..it will take you too a form. If you then view the source you will see what i mean- the cookie PHP script will be completely visible to you.

All filenames are now .php..its VERY strange..

RichL
RichL
  • Member since: Apr. 2, 2000
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 13:57:50 Reply

Jim. I noticed you posted some tests in my forum- do you have any ideas, PLEASE!

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 17:20:18 Reply

At 8/7/01 01:57 PM, RichL wrote: Jim. I noticed you posted some tests in my forum- do you have any ideas, PLEASE!

Well, firstly, you need to work on coding style. If you're going to use braces to put blocks of code in, at least do it consistently. This is not good:

if (isset($eforum))
die ("You Have Tried To Post Twice In One Hour. This is not allowed. ");

else {

setcookie ("eforum", "checkpost",time()+3600);

}

This is better (though I despise the use of die in cases like this):

if (isset($eforum)) { // opening brace
die ("You Have Tried To Post Twice In One Hour. This is not allowed. ");

} else { // close the first brace, open another

setcookie ("eforum", "checkpost",time()+3600);

} // close the second one

However, this is not the cause of the problem, as your code isn't even being parsed (if it were, I think it highly likely that you'd be getting a parse error), which is the odd part. I won't be able to tell what's going on unless I see some of the actual code, which hasn't already been parsed by a browser (which is what I asked for in my last post ;P).

RichL
RichL
  • Member since: Apr. 2, 2000
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 18:21:51 Reply

I cant give you the raw code yet- as i am on a differect computer, and even so i would prefer to email it to you than to show the world ;). If you could give me your email address. I think the problem may be because the forum uses templates..a template.php file..its all very complicated- and i think the problem may be because the script obviously wont allow users to post any PHP and let it run..its not a great forum- and im thinking of re-doing it, but if i could get this working it would be great. As i said- please, if you wouldnt mind- give me your email address and i will send you the raw code (ill put all the neccessary files in a ZIP file) for you to take a peek at.

Thanks again.

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 18:43:07 Reply

Umm, you have seen the "email author" link on every BBS post, haven't you? I think you have, as you've contacted me that way before.

RichL
RichL
  • Member since: Apr. 2, 2000
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 18:47:14 Reply

At 8/7/01 06:43 PM, liljim wrote: Umm, you have seen the "email author" link on every BBS post, haven't you? I think you have, as you've contacted me that way before.

Lol- just making sure. I still have my 2 year old email address on here...should change that soon ;). Ill email it to you sometime tommorow morning (BST)

RichL
RichL
  • Member since: Apr. 2, 2000
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Retreiving a COOKIE in PHP! 2001-08-07 19:15:40 Reply

Jim- was giving it some thought. Could we code it in Javascript instead of PHP? The page would then definately process it (as i already use some javascript in the forum- works well). Do you know the Javascript code for setting and searching for cookies?

<deleted>
Response to Retreiving a COOKIE in PHP! 2001-08-12 08:02:53 Reply

lol