suess0r Posted December 17, 2006 Share Posted December 17, 2006 OK, so i have 2 pages...mpg1.php (main page)mcity.php (cookie setting page)i want to load mpg1.php and check to see if a cookie has been set for the city, if it's set i want to echo the city and proceed with the rest of the page. If it's not set I want to have a link that says "Please choose a city" that takes you to mcity.php to set it... unfortunately i'ts not working. I'm POSTing the choice from mcity to mpg1 and here's how i'm trying to test it...[quote] <?php $time = time(); $city = $_REQUEST['cookie_city'];$visits = 1;$cookie_string = $city.'&'.$visits; if($city != ""){ setcookie("cookie_city", $cookie_string, $time+3600);}if (!isset($cookie_city)) { echo '<a href="/mcity.php">Please select a city"</a>';} else { echo 'Your cookie is set to '.$city.'';}?>[/quote]i'm getting errors "Warning: Cannot modify header information - headers already sent by (output started at /Users/jif/MClb411/mcooktest.php:11) in /Users/jif/MClb411/mcooktest.php on line 30" and line 30 is the setcookie() line... it's not setting the cookie or showing it, and keeps showing the link to set my cookie... can anyone shed some light? ??? Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/ Share on other sites More sharing options...
moshdj2uk Posted December 17, 2006 Share Posted December 17, 2006 [code]<?php$time = time();$city = $_REQUEST['cookie_city'];$visits = 1;$cookie_string = $city.'&'.$visits; if($city != ""){ setcookie("cookie_city", $cookie_string, $time+3600);}if (!isset($_COOKIE['cookie_city'])) { echo '<a href="/mcity.php">Please select a city"[/url]';} else { echo 'Your cookie is set to '.$city.'';}?>[/code]Try that? Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143037 Share on other sites More sharing options...
Daniel0 Posted December 17, 2006 Share Posted December 17, 2006 Won't work. You just need to ensure that nothing is outputted before you attempt to set your cookie or you can add ob_start() to the very top of your script. Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143041 Share on other sites More sharing options...
suess0r Posted December 17, 2006 Author Share Posted December 17, 2006 So i moved the following code to the top of mpg1.php...[quote]<?php $time = time(); $city = $_REQUEST['cookie_city'];$visits = 1;$cookie_string = $city.'&'.$visits; if($city != ""){ setcookie("cookie_city", $cookie_string, $time+3600);}if (!isset($cookie_city)) { echo '<a href="/mcity.php">Please select a city"</a>';} else { echo 'Your cookie is set to '.$city.'';}?>[/quote] i picked 'Orlando' in the dropdown in my mcity.php and POSTed it to mpg1.php and put that code at the top but i'm getting the following error ;( "Warning: Cannot modify header information on line 8" Line 8 is the setcookie() function, and it's saying "Please select a city" instead of Your cookie is set to Orlando. Can someone help?!??! Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143137 Share on other sites More sharing options...
suess0r Posted December 18, 2006 Author Share Posted December 18, 2006 can anyone help out?? Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143289 Share on other sites More sharing options...
mlin Posted December 18, 2006 Share Posted December 18, 2006 If your still getting that error, something is being sent to the browser. Could you post more code? Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143298 Share on other sites More sharing options...
suess0r Posted December 18, 2006 Author Share Posted December 18, 2006 [quote]<?php require_once('Connections/backendform.php'); ?> <?php$time = time(); $city = $_REQUEST['cookie_city'];$visits = 1;$cookie_string = $city.'&'.$visits; if($city != ""){ setcookie("cookie_city", $cookie_string, $time+3600);}if (!isset($cookie_city)) { echo '<a href="/mcity.php">Please select a city</a>';} else { echo 'Your cookie is set to '.$city.'!';}mysql_select_db($database_backendform, $backendform);$query_landmark = "SELECT landmark FROM landmark ORDER BY landmark ASC";$landmark = mysql_query($query_landmark, $backendform) or die(mysql_error());$row_landmark = mysql_fetch_assoc($landmark);$totalRows_landmark = mysql_num_rows($landmark); ?> <!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">[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143303 Share on other sites More sharing options...
mlin Posted December 18, 2006 Share Posted December 18, 2006 backendform.php must be sending something to the browser.try putting require_once('Connections/backendform.php'); below setcookie() Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143423 Share on other sites More sharing options...
suess0r Posted December 18, 2006 Author Share Posted December 18, 2006 OK, first of all thanks for everyone's help... here's the issue as of now...mpg1.php -> Always says "Please select a city..." instead of echoing the Cookie... here's my code for mpg1....*If i echo the $_COOKIE['choose_city']; at the top where i set the cookie it displays it, unfortunately when I have it in the if statement it always says "please select a city.." is there a problem in my syntax???*[quote]<?php$time = time(); $city = $_REQUEST['cookie_city'];$visits = 1;$cookie_string = $city.'&'.$visits; if($city != ""){ setcookie("cookie_city", $cookie_string, $time+3600);}?><?php require_once('Connections/backendform.php'); mysql_select_db($database_backendform, $backendform);$query_landmark = "SELECT landmark FROM landmark ORDER BY landmark ASC";$landmark = mysql_query($query_landmark, $backendform) or die(mysql_error());$row_landmark = mysql_fetch_assoc($landmark);$totalRows_landmark = mysql_num_rows($landmark);?> <!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"> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <meta name="generator" content="CLB411"/> <!-- TemplateBeginEditable name="doctitle" --> <title>CLB 411</title> <!-- TemplateEndEditable --> <style type="text/css"> @import url(mobile.css) screen, handheld;.style3 {color: #FFFFFF}.style6 {font-size: small} .style7 {font-size: small}.style7 {font-size: small} </style> <!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable --> </head> <? include('mheader.php');?> <table width="180" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td colspan="3" align="center" valign="top"><form action="mpg2.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> <div id="choose_article"> <div align="center"><span class="style3"><br /> <?php if (!isset($_COOKIE['choose_city'])) { echo '<a href="/mcity.php">Please select a city</a>'; } else { echo 'Your cookie is set to '.$_COOKIE['choose_city'].'!'; } ?>[/quote]*I'm capable of echoing the $_COOKIE['choose_city']; at the very top but when i have it in this bottom if statement it doesn't seem to want to take...Here's the code on mcity.php that i'm POSTing the variables to mpg1.php from....[quote]<form action="mcooktest.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> <div id="choose_article"> <div align="left"><span class="style3"><br> Choose your city: </span> <select name="cookie_city" class="field select medium" style="width: 160px;"> <option value=""></option> <option value="Fashion Square">Gainsville, FL</option> <option value="kissimmie airport">Jacksonville, FL</option> <option value="Fashion Square">Miami, FL</option> <option value="orlando">Orlando, FL</option> <option value="gator land">St. Pete, FL</option> <option value="florida mall">Tallahassee, FL</option> </select> </div> </div> <br> <div id="choose_article"> <div align="left"></div> </div> <div id="choose_article"><br /> <input name="submit" type="submit" class="style7" value="Submit my city" />[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143589 Share on other sites More sharing options...
suess0r Posted December 18, 2006 Author Share Posted December 18, 2006 can someone have a peak? ;x Quote Link to comment https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/#findComment-143731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.