Jump to content

cookies


helpmehelpmehelpme

Recommended Posts

I'm trying to write a script that allows the user to select their personal settings for the site.  They can pick the color of their background, link color, link hover color and the header color.  the first file is supposed to use the defaults, blue for the links, orange for the link hover color red for the background and blue for the header.  the second file is supposed to configure the cookie variables to store the values entered by the user and the third file i'm supposed to modify the internal stylesheet to utilize the values stored in the session variables. I tried to set each of the values in file two but i think i messed it up.  I dont know how to modify the stylesheet to accept and change to the values of the cookies...Please help

here are the files

file1.php

<?php
//Handle the form if it has been submitted:
if (isset($_POST['background_color'], $_POST['link_color'], $_POST['link_hover_color'],
$_POST['header_color'])){

//send the cookies:
setcookie('background_color', $_POST['background_color'], time()+10000000);
setcookie('link_color', $_POST['link_color'], time()+10000000);
setcookie('link_hover_color', $_POST['link_hover_color'], time()+10000000);
setcookie('header_color', $_POST['header_color'], time()=10000000);

//Message to be printed later:
$msg = '<p>Your settings have been entered! Click <a href="file2.php">here</a> to see them in action.</p>';


}
?>

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File1</title>

	 <style type="text/css">

	 <!-- These are the styles the session (if it exists) should configure -->

	 a:link {color:blue;}
	 a:hover {color:orange;}
	 body {background-color:red}
	 h1 {color:blue; text-align:center;}
   
 </style>

</head>
<body>
<?php
//if the cookies were sent print a message
if (isset($msg)){
print $msg;
}

?>

<div><p>Please complete this form to configure the appearance of this website:</p>

<form action="File2.php" method="post">
<select name="background_color">
<option value="">Background Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="link_color">
<option value="">Link Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="link_hover_color">
<option value="">Link Hover Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="header_color">
<option value="">Header Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>


<input type="submit" name="submit" value="Configure!" />

</form>
</div>
</body>
</html>

file2.php

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File2</title>
</head>
<body>
<div>

<?php
include('file1.php');
//check for a background color
if (isset($_COOKIE['background_color'])){
print "\body: #" . htmlentities($_COOKIE['background_color']) . ";\n";
}else{
print "\body: #c00";
}

//check for a link_color
if (isset($_COOKIE['link_color'])){
print "\a:link: #" . htmlentities($_COOKIE['link_color']) . ";\n";
}else{
print "\a:link: #00f";
}

//check for a link_hover color:
if (isset($_COOKIE['link_hover_color'])){
print "a\:hover: #" . htmlentities($_COOKIE['link_hover_color']) . ";\n";
}

//Check for a header color
if (isset($_COOKIE['header_color'])){
print "\h1: #" . htmlentities($_COOKIE['header_color']). ";\n";
}
?>

Your settings have been updated. <a href="File3.php">Click here</a> to continue.

<br /><br />

</div>
</body>
</html>

file3.php

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File3</title>

 <style type="text/css">

	 <!-- These are the styles the session should configure -->

	 a:link {color:blue;}
	 a:hover {color:orange;}
	 body {background-color:red}
	 h1 {color:blue; text-align:center;}
   
 </style>

</head>
<body>
<div>


<h1>Welcome to YOUR pretty website</h1>

<a href = "File1.php">Click here</a> to go back and start over!</a>



</div>
</body>
</html>

 

I'm pretty sure i did in file2 what i was supposed to do in file3

im totally confused

Link to comment
Share on other sites

Hmm, this is just a rough idea, but I am fairly certain that all file2 does is print out the data in the cookie.

 

But in file3 something like this should work:

 

<?php
$linkcolor = $_COOKIE["link_color"]; // Define the variable ?>

<style type="text/css">

	 <!-- These are the styles the session should configure -->

	 a:link {color:#<?php $linkcolor; ?>} //Use the variable in the style 
	 a:hover {color:orange;}
	 body {background-color:red}
	 h1 {color:blue; text-align:center;}
   
 </style>

 

Just do the same for all of them. (Also see if it works with and without file2)

Link to comment
Share on other sites

I tried that instead of it applying the changes to the styles, it prints returns

\body: #c00\a:link: #00f Your settings have been updated. Click here to continue.  on the page.  I don't know, there must be an error somewhere

heres the updated code

file1.php

<?php
//Handle the form if it has been submitted:
if (isset($_POST['background_color'], $_POST['link_color'], $_POST['link_hover_color'],
$_POST['header_color'])){

//send the cookies:
setcookie('background_color', $_POST['background_color'], time()+10000000);
setcookie('link_color', $_POST['link_color'], time()+10000000);
setcookie('link_hover_color', $_POST['link_hover_color'], time()+10000000);
setcookie('header_color', $_POST['header_color'], time()+10000000);

//Message to be printed later:
$msg = '<p>Your settings have been entered! Click <a href="file2.php">here</a> to see them in action.</p>';


}
?>

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File1</title>

	 <style type="text/css">

	 <!-- These are the styles the session (if it exists) should configure -->

	 a:link {color:blue;}
	 a:hover {color:orange;}
	 body {background-color:red}
	 h1 {color:blue; text-align:center;}
   
 </style>

</head>
<body>
<?php
//if the cookies were sent print a message
if (isset($msg)){
print $msg;
}

?>

<div><p>Please complete this form to configure the appearance of this website:</p>

<form action="File2.php" method="post">
<select name="background_color">
<option value="">Background Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="link_color">
<option value="">Link Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="link_hover_color">
<option value="">Link Hover Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="header_color">
<option value="">Header Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>


<input type="submit" name="submit" value="Configure!" />

</form>
</div>
</body>
</html>

file3.php

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File3</title>

<?php
$backgroundcolor = $_COOKIE['background_color'];
$linkcolor = $_COOKIE['link_color']; // Define the variable 
$linkhovercolor = $_COOKIE['link_hover_color'];
$headercolor = $_COOKIE['header_color'];
?>

<style type="text/css">

	 <!-- These are the styles the session should configure -->

	 a:link {color:#<?php $linkcolor; ?>} //Use the variable in the style 
	 a:hover {color:#<?php $linkhovercolor; ?>}
	 body {background-color:#<?php $backgroudcolor; ?>}
	 h1 {color:#<?php $headercolor; ?> text-align:center;}
   
 </style>



</head>
<body>
<div>


<h1>Welcome to YOUR pretty website</h1>



<a href = "File1.php">Click here</a> to go back and start over!</a>



</div>
</body>
</html>

Link to comment
Share on other sites

sorry, file 2 returned that but when you click click here to see it in action which goes to file3, it says

Notice: Undefined index: background_color in C:\xampp\htdocs\xampp\PHP\file3.php on line 10

 

Notice: Undefined index: link_color in C:\xampp\htdocs\xampp\PHP\file3.php on line 11

 

Notice: Undefined index: link_hover_color in C:\xampp\htdocs\xampp\PHP\file3.php on line 12

 

Notice: Undefined index: header_color in C:\xampp\htdocs\xampp\PHP\file3.php on line 13

Welcome to YOUR pretty website

Click here to go back and start over!

 

and the original styles are displayed

 

Link to comment
Share on other sites

like this?

<style type="text/css">

	 <!-- These are the styles the session should configure -->

	 a:link {color:#<? $_COOKIE['link_color']; ?>} //Use the variable in the style 
	 a:hover {color:#<? $_COOKIE['link_hover_color']; ?>}
	 body {background-color:#<? $_COOKIE['background_color']; ?>}
	 h1 {color:#<? $_COOKIE['header_color']; ?> text-align:center;}
   
 </style>

it still doesnt change the styles on the page....im confused

:(

Link to comment
Share on other sites

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File3</title>

<?php


$backgroundcolor = $_COOKIE["background_color"];
$linkcolor = $_COOKIE["link_color"]; // Define the variable 
$linkhovercolor = $_COOKIE["link_hover_color"];
$headercolor = $_COOKIE["header_color"];
?>

<style type="text/css">

	 <!-- These are the styles the session should configure -->

	 a:link {color:#<?php $linkcolor; ?>}
	 a:hover {color:#<?php $linkhovercolor; ?>}
	 body {background-color:#<?php $backgroudcolor; ?>}
	 h1 {color:#<?php $headercolor; ?> text-align:center;}
   
 </style>



</head>
<body>
<div>


<h1>Welcome to YOUR pretty website</h1>



<a href = "File1.php">Click here</a> to go back and start over!</a>



</div>
</body>
</html>

 

Like this. Just copy and paste and post any errors if you get any

Link to comment
Share on other sites

Slight changes to file1.php. If this does not work, then I am sorry, do not know what to do:

 

<?php
//Handle the form if it has been submitted:
if (isset($_POST['submit'])){

//send the cookies:
setcookie('background_color', $_POST['background_color'], time()+10000000);
setcookie('link_color', $_POST['link_color'], time()+10000000);
setcookie('link_hover_color', $_POST['link_hover_color'], time()+10000000);
setcookie('header_color', $_POST['header_color'], time()+10000000);

//Message to be printed later:
$msg = '<p>Your settings have been entered! Click <a href="file3.php">here</a> to see them in action.</p>';


}
?>

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File1</title>

	 <style type="text/css">

	 <!-- These are the styles the session (if it exists) should configure -->

	 a:link {color:blue;}
	 a:hover {color:orange;}
	 body {background-color:red}
	 h1 {color:blue; text-align:center;}
   
 </style>

</head>
<body>
<?php
//if the cookies were sent print a message
if (isset($msg)){
print $msg;
}

?>

<div><p>Please complete this form to configure the appearance of this website:</p>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<select name="background_color">
<option value="">Background Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="link_color">
<option value="">Link Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="link_hover_color">
<option value="">Link Hover Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>
<select name="header_color">
<option value="">Header Color</option>
<option value="FF6600">Orange</option>
<option value="CC00CC">Purple</option>
<option value="FFFF00">Yellow</option>
<option value="00FF00">Green</option>
<option value="FF0066">Pink</option>
<option value="000099">Blue</option>
</select>


<input type="submit" name="submit" value="Configure!" />

</form>
</div>
</body>
</html>

Link to comment
Share on other sites

It should work if you copy and paste this for file3.php:

		<?php
$backgroundcolor = $_COOKIE["background_color"];
$linkcolor = $_COOKIE["link_color"]; // Define the variable 
$linkhovercolor = $_COOKIE["link_hover_color"];
$headercolor = $_COOKIE["header_color"];
?>

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File3</title>



<style type="text/css">

	 <!-- These are the styles the session should configure -->

	 a:link {color:#<?php echo "$linkcolor"; ?>;}
	 a:hover {color:#<?php echo "$linkhovercolor"; ?>;}
	 body {background-color:#<?php echo "$backgroud_color"; ?>;}
	 h1 {color:#<?php echo "$headercolor"; ?>; text-align:center;}
   
 </style>



</head>
<body>
<div>


<h1>Welcome to YOUR pretty website</h1>



<a href = "File1.php">Click here</a> to go back and start over!</a>



</div>
</body>
</html>

Link to comment
Share on other sites

okay, it looks like they all work except for the background color and the link color.

 

Sorry stupid spelling mistake by me, here, should all work now:

 

	<?php
$background_color = $_COOKIE["background_color"];
$linkcolor = $_COOKIE["link_color"]; // Define the variable 
$linkhovercolor = $_COOKIE["link_hover_color"];
$headercolor = $_COOKIE["header_color"];
?>

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>File3</title>



<style type="text/css">

	 <!-- These are the styles the session should configure -->

	 a:link {color:#<?php echo "$linkcolor"; ?>;}
	 a:hover {color:#<?php echo "$linkhovercolor"; ?>;}
	 body {background-color:#<?php echo "$background_color"; ?>;}
	 h1 {color:#<?php echo "$headercolor"; ?>; text-align:center;}
   
 </style>



</head>
<body>
<div>


<h1>Welcome to YOUR pretty website</h1>



<a href = "File1.php">Click here</a> to go back and start over!</a>



</div>
</body>
</html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.