I set my cookie on a previouse page, this page here i call it and set it to a var, then when i echo it it says undefined var. Just curious.. im missing something simple here.. not sure what it is. Cookie is set, i verified that. and if i echo the cookie directly it works fine, its just when i echo the var that i assigned cookie value to that is undefined. any advise is greatly appreciated. thanks

<html>
<head></head>
<body>
<?php
if(isset($_COOKIE["num_tape"]) ){
   $num_tape= $_COOKIE["num_tape"];
   $supplies= TRUE;
}

Echo $num_tape;
?>
</body>
</html>

Recommended Answers

All 6 Replies

Do a simple test in order to understand what is happening in this point of flow (some editors have php debuggers , not easy to set but really useful)

<html>
<head></head>
<body>
<?php
if( isset($_COOKIE["num_tape"]))
{
   $num_tape	= $_COOKIE["num_tape"];
   $supplies	= TRUE;
   var_dump($num_tape);
}
else 
{
	echo "undefined<br/>";
	var_dump($_COOKIE);
}
?>
</body>
</html>

Hi,

$num_tape is out of scope. Try something like:

$numTape = isset($_COOKIE['num_tape']) ? $_COOKIE['num_tape'] : null;
echo $numTape;

Plus, always remember to sanitise variables from cookies, as they're easy to tamper with.

R.

Hi,

$num_tape is out of scope. Try something like:

$numTape = isset($_COOKIE['num_tape']) ? $_COOKIE['num_tape'] : null;
echo $numTape;

Plus, always remember to sanitise variables from cookies, as they're easy to tamper with.

R.

Ill try your suggestion, Im sorry for being a total noob but im unfaliliar with the syntax you laid out.. what is the "?" in the middle of that statement?

I used the code for testing and it all looked good. Its something to do with where i am setting the var.
heres my entire page:

<?php require("includes/header.php"); ?>
<?php//vars from form_1
	if(isset($_POST["submit"]) ){
		
		if(isset($_COOKIE["num_tape"]) ){
			$num_tape= $_COOKIE["num_tape"]; 
			$supplies= TRUE;
		}
		if(isset($_COOKIE["num_brushes"]) ){$num_brushes= $_COOKIE["num_brushes"]; $supplies= TRUE;}
		if(isset($_COOKIE["num_naps"]) ){$num_naps= $_COOKIE["num_naps"]; $supplies= TRUE; }
		if(isset($_COOKIE["num_plastic"]) ){$num_plastic= $_COOKIE["num_plastic"]; $supplies= TRUE;}
		if(isset($_COOKIE["puddy"]) ){$puddy= $_COOKIE["puddy"]; $supplies= TRUE;}
		if(isset($_COOKIE["rags"]) ){$rags= $_COOKIE["rags"]; $supplies= TRUE;}
		if(isset($_COOKIE["gas"]) ){$gas= $_COOKIE["gas"]; $supplies= TRUE;}
		
		//vars from form_2
		if(isset($_POST["prep"]) ){$prep= $_POST["prep"]; }
		if(isset($_POST["trim_primer"]) ){$trim_primer= $_POST["trim_primer"]; }
		if(isset($_POST["lin_ft"])  ){$lin_ft= $_POST["lin_ft"];
			}else{
			$errors= "Must fill in Linear Ft in order to continue";
			header("Location: build_form_2.php?e=$errors")  ;}

		if(isset($_POST["wall_len"]) ){$wall_len= $_POST["wall_len"]; }
		if(isset($_POST["wall_width"]) ){$wall_width= $_POST["wall_width"]; }
		if(isset($_POST["ceil_height"]) ){$ceil_height= $_POST["ceil_height"]; }
		if(isset($_POST["walls_primer"]) ){$walls_primer= $_POST["walls_primer"]; }
		if(isset($_POST["cathedral"]) ){$cathedral= $_POST["cathedral"]; }
		if(isset($_POST["tex_ceil"]) ){$tex_ceil= $_POST["tex_ceil"]; }
		if(isset($_POST["num_windows"]) ){$num_windows= $_POST["num_windows"]; }
		if(isset($_POST["num_doors"]) ){$num_doors= $_POST["num_doors"]; }
	
}
?>			
<h1>Step 3: Just a few more details.</h1>
<br />
<div id= "build_form_3">
				
	<form action= "" method= "post">
	<?php																																						
	
	echo $num_tape;
	
				
		if(isset($_POST["submit"]) ){																				 
			echo "<input type= \"submit\" name= \"submit\" 
			value= \"Submit\" /><br />";
		}
	?>	
	</form>
	<br />
																																																																																																																																						
</div> 
<?php require("includes/footer.php");?>

I noticed that if i assign the cookie to the var down where i am trying to echo it it works just fine. Its just the place where i am assigning the $var.. weird.. i dont get it. I use the $var in this same scope all the time for post and get vars, works fine, but not this time..

Hi,

$num_tape is out of scope. Try something like:

$numTape = isset($_COOKIE['num_tape']) ? $_COOKIE['num_tape'] : null;
echo $numTape;

Plus, always remember to sanitise variables from cookies, as they're easy to tamper with.

R.

that didnt work either. i appreciate your efforts so far though

I got it.. it was the stupid comment at the top.. i moved it down 1 line and now all works fine.. thanks for all your help guys.. i knew it had to be something really small.. lol thanks much everyone :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.