Connecting Tech Pros Worldwide Help | Site Map

cookies and php - basics

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:20 AM
deko
Guest
 
Posts: n/a
Default cookies and php - basics

This is about the simplest example I could come up with, but for some reason
(not my browser's settings) it's not working...


[top of page]
<?php
setcookie($cookie_name, "cookie_content", time()+3600);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>cookie test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>
<?php
if (isset($cookie_name))
{
print "Welcome! <br>";
}
else
{
print "Go away.";
}
?>
</p>
</body>
</html>

Am I missing something? thx



  #2  
Old July 17th, 2005, 04:20 AM
deko
Guest
 
Posts: n/a
Default Re: cookies and php - basics

Ah Ha... got it working

<?php
$cookie_name = "the_cookie_name"; <== this is what was missing
$cookie_content = "the_cookie_content";
setcookie($cookie_name, $cookie_content, time()+3600);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>cookie test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>
<?php
if (isset($cookie_name))
{
print "Welcome! <br>";
}
else
{
print "Go away.";
}
?>
</p>
</body>
</html>

As for best practices, should the cookie code always be at the top of the page
like this?


  #3  
Old July 17th, 2005, 04:20 AM
deko
Guest
 
Posts: n/a
Default Re: cookies and php - basics

umm... on second thought... back to the drawing board
"deko" <dje422@hotmail.com> wrote in message
news:0lw7c.41201$OG6.20733@newssvr25.news.prodigy. com...[color=blue]
> Ah Ha... got it working
>
> <?php
> $cookie_name = "the_cookie_name"; <== this is what was missing
> $cookie_content = "the_cookie_content";
> setcookie($cookie_name, $cookie_content, time()+3600);
> ?>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> <html>
> <head>
> <title>cookie test</title>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
> </head>
> <body>
> <p>
> <?php
> if (isset($cookie_name))
> {
> print "Welcome! <br>";
> }
> else
> {
> print "Go away.";
> }
> ?>
> </p>
> </body>
> </html>
>
> As for best practices, should the cookie code always be at the top of the page
> like this?
>
>[/color]


  #4  
Old July 17th, 2005, 04:20 AM
deko
Guest
 
Posts: n/a
Default Re: cookies and php - basics

Here's beta 2 - but something is still not right. I still get in without
waiting 2 hours....

<?php
$cookie_name = "www.mysite.com";
setcookie($cookie_name, time(), time()+7200); //say cookie is received at
3:00pm
//question: if the cookie has NOT yet expired, will it be set again?
//if yes, how do I prevent cookie from being set if it has not expired?
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>cookie test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>
<?php
$cookie_content = $HTTP_COOKIE_VARS[$cookie_name];
if ($cookie_content > (time()-7200))
//say it's now 4:00pm - if your cookie timestamp (3:00pm) is greater than
//(i.e. after) 2:00pm (now minus 2 hours) then you can't come in
{
print "Go away! You are allowed in here only once every 2 hours.";
}
else
{
print "Welcome! You have not been here more than once in the past 2 hours.";
}
?>
</p>
</body>
</html>



  #5  
Old July 17th, 2005, 04:20 AM
Alvaro G Vicario
Guest
 
Posts: n/a
Default Re: cookies and php - basics

*** deko wrote/escribió (Mon, 22 Mar 2004 07:02:23 GMT):[color=blue]
> This is about the simplest example I could come up with, but for some reason
> (not my browser's settings) it's not working...
>
>
> [top of page]
> <?php
> setcookie($cookie_name, "cookie_content", time()+3600);
> ?>[/color]

Please note you never give any value to $cookie_name variable so cookie is
created without any name, just a value.

Set-Cookie: =cookie_content; expires=Mon, 22-Mar-04 10:30:51 GMT



--
--
-- Álvaro G. Vicario - Burgos, Spain
--
  #6  
Old July 17th, 2005, 04:20 AM
Alvaro G Vicario
Guest
 
Posts: n/a
Default Re: cookies and php - basics

Sorry, I hadn't read your followups.

*** deko wrote/escribió (Mon, 22 Mar 2004 09:07:13 GMT):[color=blue]
> //question: if the cookie has NOT yet expired, will it be set again?[/color]

Sure. Why shouldn't?
[color=blue]
> //if yes, how do I prevent cookie from being set if it has not expired?[/color]

if()

Check first whether cookie exists.

[color=blue]
> if ($cookie_content > (time()-7200))[/color]

Try this:

echo $cookie_content

You'll notice what fails. In general, printing the value of variables helps
a lot. You'll also make good use of print_r():

echo "<pre>";
print_r($HTTP_COOKIE_VARS);
echo "</pre>";

It's not a good idea to use dots in cookie names.



--
--
-- Álvaro G. Vicario - Burgos, Spain
--
  #7  
Old July 17th, 2005, 04:20 AM
deko
Guest
 
Posts: n/a
Default Re: cookies and php - basics

Hi and thanks for the reply. What I am trying to do is very basic, I'm sure.

Again, here is my code - see comments for questions.

<?php
$cookie_name = "wwwmydomaincom";
setcookie($cookie_name, time(), time()+7200); //say cookie is received at
3:00pm
//here you say that the cookie will be set again, even if it has expired.
//the problem is, when I try to check to see if the cookie exists -
// using $HTTP_COOKIE_VARS - I get some error about the header...
//so what souldd teh code look like?
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>cookie test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>
<?php
$cookie_content = $HTTP_COOKIE_VARS[$cookie_name];
if ($cookie_content > (time()-7200))
//say it's now 4:00pm - if your cookie timestamp (3:00pm) is greater than
//(i.e. after) 2:00pm (now minus 2 hours) then you can't come in
{
print "Go away! You are allowed in here only once every 2 hours.";
}
else
{
print "Welcome! You have not been here more than once in the past 2 hours.";
}
?>
</p>
</body>
</html>


  #8  
Old July 17th, 2005, 04:20 AM
Alvaro G Vicario
Guest
 
Posts: n/a
Default Re: cookies and php - basics

*** deko wrote/escribió (Mon, 22 Mar 2004 10:49:34 GMT):[color=blue]
> //here you say that the cookie will be set again, even if it has expired.
> //the problem is, when I try to check to see if the cookie exists -
> // using $HTTP_COOKIE_VARS - I get some error about the header...
> //so what souldd teh code look like?[/color]

Something like this should work:

if($_COOKIE['wwwmydomaincom']!=''){
}



--
--
-- Álvaro G. Vicario - Burgos, Spain
--
  #9  
Old July 17th, 2005, 04:21 AM
deko
Guest
 
Posts: n/a
Default Re: cookies and php - basics

Okay, I think I've got it:

<?php
setcookie("cookie_name",time(),0,"/");
$cookie_check = $HTTP_COOKIE_VARS["cookie_name"];
if ($cookie_check > (time()-7200))
{
print "Try again in 2 hours.";
exit;
}
else
{
echo "the code";
}
?>

This seems to be working as desired. The problem is how to accommodate visitors
from different time zones? For example, if your time zone is 3 hours behind me,
"the code" will never run for you. Is there a way to determine the time zone
offset of my visitors?


  #10  
Old July 17th, 2005, 04:21 AM
Alvaro G Vicario
Guest
 
Posts: n/a
Default Re: cookies and php - basics

*** deko wrote/escribió (Mon, 22 Mar 2004 21:55:10 GMT):[color=blue]
> This seems to be working as desired. The problem is how to accommodate visitors
> from different time zones?[/color]

There's no need to. You are setting the time in the server.


--
--
-- Álvaro G. Vicario - Burgos, Spain
--
  #11  
Old July 17th, 2005, 04:21 AM
deko
Guest
 
Posts: n/a
Default Re: cookies and php - basics

10-4

thanks for the help. I think I'm read for cookies 102...

cheers,

deko

"Alvaro G Vicario" <alvaro_QUITAR_REMOVE@telecomputeronline.com> wrote in
message news:iitzqset7r70.oujlgbpjxznw$.dlg@40tude.net...[color=blue]
> *** deko wrote/escribió (Mon, 22 Mar 2004 21:55:10 GMT):[color=green]
> > This seems to be working as desired. The problem is how to accommodate[/color][/color]
visitors[color=blue][color=green]
> > from different time zones?[/color]
>
> There's no need to. You are setting the time in the server.
>
>
> --
> --
> -- Álvaro G. Vicario - Burgos, Spain
> --[/color]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.