Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Set Cookie.. not setting


Set Cookie.. not setting

Closed Thread
  #1 (permalink)  
Old 01-27-11, 01:39 PM
timbaker1991's Avatar
timbaker1991 timbaker1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Location: London, UK
Posts: 38
Thanks: 2
Thanked 3 Times in 3 Posts
Question Set Cookie.. not setting

I've got this code to set a cookie with information from a GET form but just doesn't work? Tested in Firefox, Chrome and Opera, oh an IE.

If anyone could offer any advice be grateful

PHP Code:

<!DOCTYPE html><html lang="en"><head>

<meta charset="utf-8"><meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<LINK href="style.css" rel="stylesheet" type="text/css">
</head><body><?php include error_reporting(0); ?>
<div id="content"><div id="headerholder"><div id="location">
&nbsp; <?php echo $_GET['loc']; ?><!--<form method="post" action="request.php"><input type="submit" name="submit" value="Change"></form>--></div>
<div id="options"><form action="changes.php"><input type="submit" name="submit" value="Updates"></form></div></div>
<div id="elementholder"></div>
<?
$location 
$_GET['loc'];
setcookie("weather",$location);

?>
  
  </body>
</html>
I know the get form works as I can see the results in the url of the page
  #2 (permalink)  
Old 01-27-11, 04:14 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Quote:
Originally Posted by http://www.php.net/setcookie
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
PHP: setcookie - Manual

Your code is probably throwing errors, but via the php settings error showing is probably turned off by default. That's why it seemed like it worked, but wasn't. To enable error showing, place the following lines at the top of your script:
PHP Code:

ini_set ('display_errors''1');

error_reporting (E_ALL E_STRICT); 
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

  #3 (permalink)  
Old 01-27-11, 04:33 PM
timbaker1991's Avatar
timbaker1991 timbaker1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Location: London, UK
Posts: 38
Thanks: 2
Thanked 3 Times in 3 Posts
Hi, I've done so and I now have this error produced

Warning: Cannot modify header information - headers already sent by (output started at /home/content/22/6620922/html/webos/apps/weather/sandbox/rememberlocation/display.php:4) in /home/content/22/6620922/html/webos/apps/weather/sandbox/rememberlocation/display.php on line 22

What would you think that would mean in real terms?

Thanks, Tim
  #4 (permalink)  
Old 01-27-11, 05:34 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I am assuming you are getting to this page from another page because I see no input element named "loc".
But the problem is that you have output before the setcookie() command is executed.
That is not allowed.
Put that code before everything else and it should work.
PHP Code:

<?
$location 
$_GET['loc'];
setcookie("weather",$location);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"><meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<LINK href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include error_reporting(0); ?>
<div id="content">
 <div id="headerholder">
  <div id="location">
  &nbsp; <?php echo $_GET['loc']; ?>
  <!--
  <form method="post" action="request.php">
  <input type="submit" name="submit" value="Change">
  </form>-->
  </div>
 <div id="options">
 <form action="changes.php">
 <input type="submit" name="submit" value="Updates">
 </form>
 </div>
</div>
<div id="elementholder"></div>
</body>
</html>
__________________
Jerry Broughton
The Following User Says Thank You to job0107 For This Useful Post:
timbaker1991 (01-27-11)
  #5 (permalink)  
Old 01-27-11, 05:49 PM
timbaker1991's Avatar
timbaker1991 timbaker1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Location: London, UK
Posts: 38
Thanks: 2
Thanked 3 Times in 3 Posts
Yeh it does come from another page! thanks works like a treat
  #6 (permalink)  
Old 01-27-11, 06:05 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
One more thing.
I just noticed that you are using short php opening tags (<?).
Though it may work with the server you are using, some servers don't allow the short tags.
You should get used to using the long tag (<?php), that way you won't have problems if you need to switch servers that don't allow the short tag.
__________________
Jerry Broughton
Closed Thread

Bookmarks

Tags
cookies


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookie problem, not being set. stormshadow PHP 5 12-10-06 03:55 PM
set cookie for subfolders hinch PHP 7 10-21-06 08:04 AM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
Passing info between pages w/ URL HairySpider JavaScript 6 01-09-05 08:47 AM
Setting a cookie within an HTML file using SSI abtimoteo Perl 1 06-19-04 02:09 AM


All times are GMT -5. The time now is 08:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.