472,986 Members | 3,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

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
Jul 17 '05 #1
10 1672
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?
Jul 17 '05 #2
umm... on second thought... back to the drawing board
"deko" <dj****@hotmail.com> wrote in message
news:0l*******************@newssvr25.news.prodigy. com...
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?

Jul 17 '05 #3
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>

Jul 17 '05 #4
*** deko wrote/escribió (Mon, 22 Mar 2004 07:02:23 GMT):
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);
?>


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
--
Jul 17 '05 #5
Sorry, I hadn't read your followups.

*** deko wrote/escribió (Mon, 22 Mar 2004 09:07:13 GMT):
//question: if the cookie has NOT yet expired, will it be set again?
Sure. Why shouldn't?
//if yes, how do I prevent cookie from being set if it has not expired?
if()

Check first whether cookie exists.

if ($cookie_content > (time()-7200))


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
--
Jul 17 '05 #6
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>
Jul 17 '05 #7
*** deko wrote/escribió (Mon, 22 Mar 2004 10:49:34 GMT):
//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?


Something like this should work:

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

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #8
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?
Jul 17 '05 #9
*** deko wrote/escribió (Mon, 22 Mar 2004 21:55:10 GMT):
This seems to be working as desired. The problem is how to accommodate visitors
from different time zones?


There's no need to. You are setting the time in the server.
--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #10
10-4

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

cheers,

deko

"Alvaro G Vicario" <al******************@telecomputeronline.com> wrote in
message news:ii****************************@40tude.net...
*** deko wrote/escribió (Mon, 22 Mar 2004 21:55:10 GMT):
This seems to be working as desired. The problem is how to accommodate visitors from different time zones?


There's no need to. You are setting the time in the server.
--
--
-- Álvaro G. Vicario - Burgos, Spain
--

Jul 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Ruscoe | last post by:
Hi chaps, I'm relatively new to the language, but I want to create a simple order form using PHP and cookies. Please let me know if there's a better way in this situation, database isn't...
4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
7
by: Brian Burgess | last post by:
Hi all, Anyone see anything wrong with the following: *************************************************************** <%@ LANGUAGE="VBSCRIPT" %> <% Option Explicit Dim nAcctNbr Dim...
5
by: learner | last post by:
hey, iam new to HTML.I know only the basics. i want to know about cookies and how to set them? where the cookies are used? A general overview on cookies and their uses .. and how to use...
3
by: Martin Mrazek | last post by:
Hi, how can one HTML document create more than one cookie? I have bloody long html form, to save all its values in 4KB of one cookie is impossible... MM
5
by: Mr Newbie | last post by:
I am having trouble writing cookies despite adding the cookie to the Response.Cookeies collection. The Request.Browser.Cookies returns TRUE so I know the browser is storing cookies and I can even...
2
by: Dst | last post by:
I'm trying to interact with some old asp code. I'm logging into the asp page by posting username/password to the asp page using webrequest / webresponse. Now this works ok, the response back is...
2
by: David Thielen | last post by:
Hi; 1) What is the easiest way to show in an aspx page if they have scripts enabled? 2) What is the easiest way to show in an aspx page if they have cookies enabled? This is for a test...
4
by: Jeff | last post by:
I have a vb.net application (2005) requiring session variables and want to test to make certain that the user's cookies are enabled. I can set a test session variable on one page and attempt to...
13
by: Jon Slaughter | last post by:
How does one handle cookies!!! I have a login script and the user inputs the information but whats the point if I cannot save the stuff because the headers are already sent. I guess I have to...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.