Jump to content

PASSING/ACCESSING VARIABLE THRU COOKIE PROBLEM


king94@

Recommended Posts

Hi there,

          I need some help again. This is a continuation of a question i asked earlier. Initially the question stated:

 

write a program that formats  ablock of text (to be input by the user) based on preferences chosen by the user. Give your user options for color of text, font choice, and size. Display the output on a new page. DID THAT ALL.

 

Now HERE is the part i am stuck on: it says: now allow the user the option of saving the information for the next time they visit, and if they choose "yes", save the information in a COOKIE!

 

PAGE 1 (FORM INPUT):

 

<?php

session_unset();

 

?>

 

<html>

<head>

<title>CHAPTER 2, QUESTION 4</title>

</head>

<body>

 

<form method = "post" action = "chap2q5endresult.php">

<p>Enter you font chice:

<select name="font">

<option value="Verdana">Verdana</option>

<option value="Arial">Arial</option>

<option value="Times New Roman">Times New Roman</option>

</select>

</p>

 

<p>Enter your font size:

<select name="size">

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

<option value="4">4</option>

</select>

</p>

 

<p>Enter your font colour:

<select name="color">

<option value="black">Black</option>

<option value="red">Red</option>

<option value="green">Green</option>

<option value="purple">Purple</option>

</select>

</p>

 

<p>Enter your text here:

<input type = "textarea" font ="font" size="size" name ="user">

</p>

 

<p>Do you want to save these preferences for the next time you log in?  //THIS IS WHAT I ADDED FOR PAGE 1.<<<<<--------------

<input type="checkbox" name="pref" value="y">

</p>

 

<input type="submit" name="Submit" value="Submit">

</form>

</body>

</html>

 

PAGE 2: DISPLAYING INPUT FROM PAGE 1: (PROBLEM PAGE). Error message keeps saying parse error for line 17 which is setcookie(.........) group.

 

<?php

//OK, SO ALL THE INPUT INFO WAS SENT BY POST TO THIS SECOND PAGE

 

if ($_POST['pref']=='y') {        //THE IF STATEMENT IS TO VERIFY IF CHECKBOX FOR SAVING PREFERENCES WAS CHECKED.

 

 

//DO NOT THINK I NEED THIS STUFF BELOW:

$_COOKIE['inputtext'] = $_POST['user'];

$_COOKIE['font'] = $_POST['font'];

$_COOKIE['size'] = $_POST['size'];

$_COOKIE['color'] = $_POST['color'];

//POSSIBLY DELETE 4 LINES ABOVE!!!

 

 

//BELOW I AM ASSIGNING THE VALUES FROM PREVIOUS FORM, GIVING THEM NAMES: font, size, color, inputtext,

//AND STORING IN COOKIE

setcookie('font', '$_POST['font'], time()+60);

setcookie('size', '$_POST['size'], time()+60);

setcookie('color', '$_POST['color'], time()+60);

setcookie('inputtext', '$_POST['inputtext'], time()+60); //NOT SURE IF I NEED TO STORE INPUTTEXT IN COOKIE, I THINK NOT?!

 

//BELOW I AM ACCRESSING THE INFO STORED IN THE COOKIE:

//I AM NOT SURE IF I NEED TO PRE-DEFINE THE $_COOKIE VARIABLE, WHICH IS WHY

//I EQUATED THE $_COOKIE VARIABLES TO $_POST AT THE TOP OF PAGE (UP TOP)

echo "<font face='";

echo $_COOKIE['font'];

echo "' size='";

echo $_COOKIE['size'];

echo "' color='";

echo $_COOKIE['color'];

echo "'>";

echo $_COOKIE['inputtext'];

echo "</font>";

 

}

 

//IF THE IF STATEMENT IS NOT TRUE DO THE FOLLOWING:

else{

 

//PASS AND ACCESS THE VARIABLES THROUGH THE SESSION NORMALLY (NO COOKIE USE HERE):

session_start();

$_SESSION['inputtext'] = $_POST['user'];

$_SESSION['font'] = $_POST['font'];

$_SESSION['size'] = $_POST['size'];

$_SESSION['color'] = $_POST['color'];

 

echo "<font face='";

echo $_SESSION['font'];

echo "' size='";

echo $_SESSION['size'];

echo "' color='";

echo $_SESSION['color'];

echo "'>";

echo $_SESSION['inputtext'];

echo "</font>";

 

}

 

?>

 

 

THANK YOU FOR ANY HELP, THANK YOU FOR YOUR TIME.

 

 

Link to comment
Share on other sites

Your code might work, you did a simple mistake on using quotes.

setcookie('font', '$_POST['font'], time()+60);
setcookie('size', '$_POST['size'], time()+60);
setcookie('color', '$_POST['color'], time()+60);
setcookie('inputtext', '$_POST['inputtext'], time()+60); 

Should be:

setcookie('font', $_POST['font'], time()+60);
setcookie('size', $_POST['size'], time()+60);
setcookie('color', $_POST['color'], time()+60);
setcookie('inputtext', $_POST['inputtext'], time()+60); 

Note your +60 is only 60 seconds before expiring, if you're sure that's the length you want.

Link to comment
Share on other sites

Thank you Oni for your post. There is a problem:

 

Problem is: the script works but it is not using the cookie as it is supposed to, here is how i know this:

 

First i deleted below:

 

$_COOKIE['inputtext'] = $_POST['user'];

$_COOKIE['font'] = $_POST['font'];

$_COOKIE['size'] = $_POST['size'];

$_COOKIE['color'] = $_POST['color'];

 

I wanted to check if it would work without it, it DID NOT WORK. So i kept it.

 

Then I started deleting individual lines from:

 

setcookie('font', '$_POST['font'], time()+60);

setcookie('size', '$_POST['size'], time()+60);

setcookie('color', '$_POST['color'], time()+60);  <---DELETED THIS LINE

setcookie('inputtext', '$_POST['inputtext'], time()+60);  <--- DELETED THIS LINE

 

The script still worked despite deleting the cookie entries. Please correct me if i am wrong but doesn't that mean that the following lines:

 

$_COOKIE['inputtext'] = $_POST['user'];

$_COOKIE['font'] = $_POST['font'];

$_COOKIE['size'] = $_POST['size'];

$_COOKIE['color'] = $_POST['color'];

 

are acting like sessions and not cookies? I understand $_COOKIE to be a variable just like $_SESSION[.....]

 

BOTTOM LINE: I think the input is being accessed through sessions regardless of whether the box is checked on page 1.

 

Link to comment
Share on other sites

I don't know why you're using the else with sessions.. now that I look over your code, yes, you do not need to use '$_COOKIE['font'] = $_POST['font']'.. setcookie() does that for you.

 

When you set the cookie, note you cannot display it on the same page as you're doing. You must use header() or similar to redirect the page saying that the preferences are updated, then you can list the cookie.

Link to comment
Share on other sites

Thanks for the advice. This is so confusing. No idea how to fix. Will keep trying stuff.

 

So if i take out:

 

$_COOKIE['inputtext'] = $_POST['user'];

$_COOKIE['font'] = $_POST['font'];

$_COOKIE['size'] = $_POST['size'];

$_COOKIE['color'] = $_POST['color'];

 

And take out:

 

echo "<font face='";

echo $_COOKIE['font'];

echo "' size='";

echo $_COOKIE['size'];

echo "' color='";

echo $_COOKIE['color'];

echo "'>";

echo $_COOKIE['inputtext'];

echo "</font>";

 

Then create a link to a third page, and basically put:

 

echo "<font face='";

echo $_COOKIE['font'];

echo "' size='";

echo $_COOKIE['size'];

echo "' color='";

echo $_COOKIE['color'];

echo "'>";

echo $_COOKIE['inputtext'];

echo "</font>";

 

It should work??? Am I on the right track?

Link to comment
Share on other sites

Try this for your page two..

<?php
ob_start(); //Start output buffer for the header() to work.
if ($_POST['pref']=='y') {
setcookie('font', $_POST['font'], time()+60*20);
setcookie('size', $_POST['size'], time()+60*20);
setcookie('color', $_POST['color'], time()+60*20);
setcookie('inputtext', $_POST['inputtext'], time()+60*20);
header('page3.php');
}

And then make a page 3 just to test your cookies out:

<?php 
echo "<font face='";
echo $_COOKIE['font'];
echo "' size='";
echo $_COOKIE['size'];
echo "' color='";
echo $_COOKIE['color'];
echo "'>";
echo $_COOKIE['inputtext'];
echo "</font>";
?>

 

Link to comment
Share on other sites

My new PAGE 2 :

 

<?php

ob_start(); //Start output buffer for the header() to work.

if ($_POST['pref']=='y') {

setcookie('font', $_POST['font'], time()+60*20);

setcookie('size', $_POST['size'], time()+60*20);

setcookie('color', $_POST['color'], time()+60*20);

setcookie('inputtext', $_POST['inputtext'], time()+60*20);

header('page3c2q5.php');

}

 

 

//PASS AND ACCESS THE VARIABLES THROUGH THE SESSION NORMALLY (NO COOKIE USE HERE):

session_start();

$_SESSION['inputtext'] = $_POST['user'];

$_SESSION['font'] = $_POST['font'];

$_SESSION['size'] = $_POST['size'];

$_SESSION['color'] = $_POST['color'];

 

echo "<font face='";

echo $_SESSION['font'];

echo "' size='";

echo $_SESSION['size'];

echo "' color='";

echo $_SESSION['color'];

echo "'>";

echo $_SESSION['inputtext'];

echo "</font>";

 

?>

 

When i get to page 2, it outputs everything correctly, but bypasses the if statement!

 

 

PAGE 3 (I called it 3c2q5): ( I DID NOT create link from page 2 to page 3, is it necessary?

 

<?php

echo "<font face='";

echo $_COOKIE['font'];

echo "' size='";

echo $_COOKIE['size'];

echo "' color='";

echo $_COOKIE['color'];

echo "'>";

echo $_COOKIE['inputtext'];

echo "</font>";

?>

 

Page 3 is blank, there is no output. Was worth a try.

Link to comment
Share on other sites

NEW PAGE 2 WITH LINK TO PAGE 3:

 

When it displays page 2, the link text and the inputtext both appear (correctly) but both as a link to page 3?????

So that means it is going through the if statement but also going outside the if statement????

I cannot understand how it is doing that.

 

<?php

ob_start(); //Start output buffer for the header() to work.

if ($_POST['pref']=='y') {

setcookie('font', $_POST['font'], time()+60*20);

setcookie('size', $_POST['size'], time()+60*20);

setcookie('color', $_POST['color'], time()+60*20);

setcookie('inputtext', $_POST['inputtext'], time()+60*20);

header('page3c2q5.php');

 

//LINK TO PAGE 3:

echo "<a href='page3c2q5.php'>";

echo "Click here to see information";

}

?>

<?php

 

//PASS AND ACCESS THE VARIABLES THROUGH THE SESSION NORMALLY (NO COOKIE USE HERE):

session_start();

$_SESSION['inputtext'] = $_POST['user'];

$_SESSION['font'] = $_POST['font'];

$_SESSION['size'] = $_POST['size'];

$_SESSION['color'] = $_POST['color'];

 

echo "<font face='";

echo $_SESSION['font'];

echo "' size='";

echo $_SESSION['size'];

echo "' color='";

echo $_SESSION['color'];

echo "'>";

echo $_SESSION['inputtext'];

echo "</font>";

 

?>

 

 

 

Page 3 is still blank.

Link to comment
Share on other sites

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.