View Full Version : php cookies
keyboard
07-07-2011, 08:41 AM
Hi everyone,
I have been fidling with php cookies and came up with this
<?php
if(isset($_COOKIE['authorization']))
header('location: index.php');
else
?>
<?php
$cheese = $_POST['name'];
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit',$cheese, $inTwoMonths);
?>
<html>
<head>
<meta http-equiv="Refresh" content="3;url=http://randomhelp4you.heliohost.org/indexthesecond.php" />
</head>
<body>
<center>
<p><font size="+2">Thankyou, you will be redirected shortly</font></p>
<p>If you are not redirected, please click <a href="http://randomhelp4you.heliohost.org/indexthesecond.php">here</a>.</p>
</center>
</body>
</html>
The idea behind it is to gather input from a form and save that input as a cookie. However I also wanted to make sure that they are coming from the page that has the form on it. If not they are redirected to a different page. It comes up with an error. Is it because I have two cookie thingys on one page. Any help would be appreciated
JoeDaStudd
07-07-2011, 09:22 AM
Without seeing the exact error its hard to say.
Try this.
<?php
if(isset($_COOKIE['authorization']))
header('location: index.php');
else{
$cheese = $_POST['name'];
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit',$cheese, $inTwoMonths);
?>
<html>
<head>
<meta http-equiv="Refresh" content="3;url=http://randomhelp4you.heliohost.org/indexthesecond.php" />
</head>
<body>
<center>
<p><font size="+2">Thankyou, you will be redirected shortly</font></p>
<p>If you are not redirected, please click <a href="http://randomhelp4you.heliohost.org/indexthesecond.php">here</a>.</p>
</center>
</body>
</html>
<?php
}
?>
You might also want some validation against the $_POST['name'].
Your question is unclear.
Please provide more information, and be as specific as possible. What do you want to accomplish? What have you already tried? What problems did you encounter?
Also, please be sure that you have included all relevant code and/or a link to the page in question.
Please post the content of the error you are receiving.
If there is any other code which affects this script, please post that as well (for example, you're trying to check for a cookie named "authorization," but you don't show the code that sets that cookie. Your html form is missing as well).
I have no idea what you mean by "cookie thingys" (which, BTW, should be spelled thingies).
Is this the same code you were trying to develop in your other thread (http://dynamicdrive.com/forums/showthread.php?t=63145)?
If so, you should post there; it helps keep things organized and will get you a better-informed response.
keyboard
07-09-2011, 12:39 AM
The first page of my website is this
<?php
if(isset($_COOKIE['lastVisit']))
header('location: indexthesecond.php');
else
$visit = $_COOKIE['lastVisit'];
?>
<html>
<head>
<title>Randomhelp4you</title>
<?php
file_put_contents('ips.log', $_SERVER['REMOTE_ADDR'] . "\n", FILE_APPEND);
?>
<?php
$george = 60 * 60 + time();
setcookie('Authorization','no', $george);
?>
<body style="background-color:lightblue">
</head>
<body>
<form action="setcookie.php" method="post">
Name <input name="name" type="text" maxlength="10" />
<input type="submit"/>
</form>
</body>
</html>
The second page looks like this
<?php
if(isset($_COOKIE['authorization']))
header('location: index.php');
else
?>
<?php
$cheese = $_POST['name'];
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit',$cheese, $inTwoMonths);
?>
<html>
<head>
<meta http-equiv="Refresh" content="3;url=http://randomhelp4you.heliohost.org/indexthesecond.php" />
</head>
<body>
<center>
<p><font size="+2">Thankyou, you will be redirected shortly</font></p>
<p>If you are not redirected, please click <a href="http://randomhelp4you.heliohost.org/indexthesecond.php">here</a>.</p>
</center>
</body>
</html>
and the third page looks like this
<?php
if(isset($_COOKIE['lastVisit']))
$visit = $_COOKIE['lastVisit'];
else
header('location: index.php');
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="basic.css" />
<title>Randomhelp4you</title>
</script>
<body style="background-color:lightblue">
</head>
<body>
<h1><center>Randomhelp4you</center></h1>
<?php
echo "Hey ". $visit;
?>
</body>
</html>
The idea behind it is that the go onto he index page (php) and enter their name. This form is procesed by setcookie.php . This page sets a cookie with their name on it, then redirects them to indexthesecond.php , were it displays their name. If they try to acess indexthesecond.php without the cookie, they should be redirected to index.php . If they try to go onto index.php with the cookie then they are redirected to indexthesecond.php . This all works fine but I am trying to add in an extra cookie with a 60 minute timeout which is assigned by index.php . This is to make sure that when they go to setcookie.php they came from index.php . I'm trying to explain this easily but if it dosen't make sense just tell me.
keyboard
07-09-2011, 12:41 AM
The error on index.php is
Warning: Cannot modify header information - headers already sent by (output started at /home1/keyboard/public_html/index.php:12) in /home1/keyboard/public_html/index.php on line 18
The error on setcookie.php is
Warning: Cannot modify header information - headers already sent by (output started at /home1/keyboard/public_html/setcookie.php:9) in /home1/keyboard/public_html/setcookie.php on line 12
By the way i did try the code that joedastudd posted (thankyou) but I could still acess the setcookie.php page without the cookie set by index.php.
Also joedastudd suggested that I add validation against $_POST['name']. I have no idea how to do this. Could anyone give me some tips please.
bluewalrus
07-09-2011, 02:44 AM
Is this line 12
header('location: indexthesecond.php');
If so what are lines 1-10 you can't output any content before you use the header.
also,
$_COOKIE['Authorization']
// and
$_COOKIE['authorization']
// are _not_ the same cookies
keyboard
07-09-2011, 06:46 AM
Blue walrus, Which page were you reffering to the first second or third. I'm sorry but i don't what the error is that you are pointing out. Could you please try to explain what you meant to me. Thankyou everyone who has helped. Also Traq, I changed the a to an A.
bluewalrus
07-09-2011, 06:15 PM
The error on index.php is
Warning: Cannot modify header information - headers already sent by (output started at /home1/keyboard/public_html/index.php:12) in /home1/keyboard/public_html/index.php on line 18
The error on setcookie.php is
Warning: Cannot modify header information - headers already sent by (output started at /home1/keyboard/public_html/setcookie.php:9) in /home1/keyboard/public_html/setcookie.php on line 12
The "Cannot modify header information - headers already sent by" means some content has already been outputted so the header can not be used.
For example
Valid:
<?php
if(isset($_COOKIE['lastVisit']))
header('location: indexthesecond.php');
else
$visit = $_COOKIE['lastVisit'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
Invalid:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
if(isset($_COOKIE['lastVisit']))
header('location: indexthesecond.php');
else
$visit = $_COOKIE['lastVisit'];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
Valid:
<?php
if(isset($_COOKIE['lastVisit']))
header('location: indexthesecond.php');
else
$visit = $_COOKIE['lastVisit'];
$content = "Blah";
echo $content;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
Invalid
<?php
$content = "Blah";
echo $content;
if(isset($_COOKIE['lastVisit']))
header('location: indexthesecond.php');
else
$visit = $_COOKIE['lastVisit'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
This also goes with including files if the file included outputs something it will error the header as well.
Also, do you mean you changed the a to an A in all other instances or here
setcookie('Authorization','no', $george); ?
keyboard
07-10-2011, 09:33 AM
I'm sorry, I don't understand. What is the difference between your examples of valid and invalid. Thankyou for helping.
bluewalrus
07-10-2011, 09:40 AM
In the valid everything being outputted to the browser is outputted after the header declaration. In the invalids things are being outputted before the header declaration. If you provide your full code we can be more specific, you can censor out secure parts with '****'.
perhaps a little clarification on how webpages are served (http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Example_session).
The headers come first in the server response. after they're done, there is a blank line, and then the content of the webpage follows (which the browser renders). For example:
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="generator" content="vBulletin 3.8.1" />
<meta name="keywords" content=" php cookies, dhtml, javascript, css, dynamic html, xml, html, php, cgi" />
<meta name="description" content="Page 2- php cookies PHP" />
<link rel="shortcut icon" href="favicon.ico" />
<!-- blah blah blah etc etc..........-->in this example, the last header is the content-type header. the blank line below signals the end of the headers, and the following line (in this case, with the doctype) is the first line of content.
once php outputs anything that is not a header (e.g., text, error messages, html markup, even whitespace or html comments), then the headers end on no more headers can be sent. When you try to use a function that sets headers (like header() or setcookie()) after content has been output, you get the "...headers already sent" error, because you can't "back up" and serve another header after that section of the server response is complete.
keyboard
07-14-2011, 07:54 AM
I have a different question but on the same cookie. This code
<?php
$george = 60 * 60 + time();
setcookie('Authorization','no', $george);
?>
I tried to set the timeout to 60 minutes. Did I get it right?
yup.
just think: seconds,minutes,hours,days, ...etc
60 /*seconds*/ * 60 /*minutes*/ * 24 /*hours*/ * 365 /*days*/
keyboard
07-18-2011, 01:26 AM
I have 1 more question
for this code
<?php
if(isset($_COOKIE['Authorization']))
$cheese = $_POST['name'];
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit',$cheese, $inTwoMonths);
else
header('location: index.php');
?>
it comes up with this error
Parse error: syntax error, unexpected T_ELSE in /home1/keyboard/public_html/setcookie.php on line 6
I don't understand what i've done wrong any help would be appreciated
bluewalrus
07-18-2011, 01:58 AM
You need curly braces if your conditional is more than 1 line
<?php
if(isset($_COOKIE['Authorization'])) {
$cheese = $_POST['name'];
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit',$cheese, $inTwoMonths);
} else
header('location: index.php');
?>
This would also work.
<?php
if(isset($_COOKIE['Authorization'])) {
$cheese = $_POST['name'];
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit',$cheese, $inTwoMonths);
} else {
header('location: index.php');
}
?>
The curly braces aren't necessary if what the conditional controls is only one line.
keyboard
07-18-2011, 02:31 AM
Thanks, it now works perfectly. How can I set this thread to resolved
bluewalrus
07-18-2011, 03:26 AM
Go to your first post (http://www.dynamicdrive.com/forums/showpost.php?p=256026&postcount=1), click edit, click go advanced, add the resolved prefix to the thread title, and you should be all set.
The curly braces aren't necessary if what the conditional controls is only one line.
Still, I always use them (and recommend the same to everyone).
keyboard
07-18-2011, 05:46 AM
I clicked on the link but there is no edit option, there is for my other posts and yes, i am logged in.
bluewalrus
07-18-2011, 02:36 PM
Sometimes it doesn't allow you to edit it, I'm not sure why but have gotten that before...
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.