Jump to content

Recommended Posts

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?  ???
Link to comment
https://forums.phpfreaks.com/topic/30997-cookie-problems-setting/
Share on other sites

[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?
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]<?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]
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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.