Question

In: Computer Science

Modify the code so that it provides a web form to collect the cookie values from...

Modify the code so that it provides a web form to collect the cookie values from the users. In other words, you will add two text boxes where users will enter the cookie name and the cookie value. You will also add a submit button, which lets users to send the cookie values to the server. The page should display the previous cookie values when it is loaded.

<!DOCTYPE html>
<?php
    $cookie_name = "user";
    $cookie_value = "John Doe";
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
   echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
   echo "Cookie '" . $cookie_name . "' is set!<br>";
   echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
</body>
</html>

Solutions

Expert Solution

Code:

<!DOCTYPE html>
<?php
        //check wheathe submit button is clicked or not. if clicked then it will set cookie name and value
        if (isset($_POST['name'])){
                $cookie_name = $_POST['name'];
                $cookie_value = $_POST['value'];
                setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
        }
 
?>
<html>
<body>
<!--here is the form to set cookie name and value manually-->
<form action="index.php" method="post">
 <p>Cookie name: <input type="text" name="name" /></p>
 <p>Cookie value: <input type="text" name="value" /></p>

 <p><input type="submit" name="Submit"/></p>
</form>

<?php

if(isset($cookie_name)) {
        //it display cookie name and value if cookie set
        if(!isset($_COOKIE[$cookie_name])) {
           echo "To see previous cookie reload the page.";
        } else {
           echo "Cookie '" . $cookie_name . "' is set!<br>";
           echo "Value is: " . $_COOKIE[$cookie_name];
        }
}else{
        echo "No any previous cookies";
} 
?>

<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
</body>
</html>

Output:

After clicking submit button.

After refreshing a page.


Related Solutions

How to validate Javascript form data? Here is the code. Can someone modify it so that...
How to validate Javascript form data? Here is the code. Can someone modify it so that all the information is validated? Thanks. <!DOCTYPE html> <html lang="en"> <head>    <title>Music Survey</title>    <meta charset="utf-8"> </head> <style>    legend { font-weight:bold;    }    </style> <body> <h1>Music Survey</h1> <form method="post" action=""> <label for="myname"><b>Name:</b></label>        <input type="text" name="myname" id="myname">        <br><br> <label for="myemail"><b>Email:</b></label>        <input type="email" name="myemail" id="myemail">        <br><br>   <fieldset> <legend>Select Your Favorite Types of Music:</legend> <input type="checkbox"...
Create a JavaScript function that will collect the information from the form and verify that it...
Create a JavaScript function that will collect the information from the form and verify that it is the correct type and that there are no blank textboxes. Save and test the file to ensure that the textbox information is collected and the script is working correctly. Use the onclick event within the submit button to call this function. Output validation error messages by writing directly to the with the id of "results." (You may want to use alert boxes for...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item on 1st column, price on 2nd) Stewing beef,15.45 Ground beef,11.29 Pork chops,11.72 Chicken,7.29 Bacon,7.12 Wieners,4.33 Canned salmon,5.68 Homogenized milk,5.79 Partly skimmed milk,5.20 Butter,4.99 Processed cheese slices,2.53 Evaporated milk,1.89 Eggs,3.11 Bread,2.74 Soda crackers,3.27 Macaroni,1.45 Flour,4.54 Corn flakes,5.72 Apples,4.71 Bananas,1.56 Oranges,3.70 ... a. In the function createPricesDict(), create a dictionary of each product mapped to its price. b. Suppose we have another dictionary for our cart...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
Modify the AlienDirection program from this chapter so that the image is not allowed to move...
Modify the AlienDirection program from this chapter so that the image is not allowed to move out of the visible area of the window. Ignore any key that would allow this to happen. *Ask if you have any questions about the assignment I will try to clarify Textbook - JAVA FOUNDATIONS: INTRODUCTION TO PROGRAM DESIGN AND DATA STRUCTURES 5TH EDITION Starter Code Provided : import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.stage.Stage;...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
A) Lymphatic capillaries collect ______ from the tissue. They then merge to form lymphatic ________________ which...
A) Lymphatic capillaries collect ______ from the tissue. They then merge to form lymphatic ________________ which then merge to form lymphatic __________________. These larger lymphatic vessels merge to form one of two ducts: the __________________ and the ________________________. B) What part of the body does the right lymphatic duct collect from? What part of the body does the thoracic duct collect from? Which blood vessels do the lymphatic ducts empty into? What are lymphatic nodules? Clusters of lymphatic nodules can...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line … System.exit(0); skeleton code: /**...
Modify the following source code so that five subsequent threads print the message “Hello World number,”...
Modify the following source code so that five subsequent threads print the message “Hello World number,” where number indicates the unique thread created; e.g. “Hello World 1” // Your_name_goes_here #include <pthread.h> #include <semaphore.h> sem_t semaphore; // also a global variable just like mutexes void *thread_function( void *arg ) { sem_wait( &semaphore ); // perform some task pthread_exit( NULL ); } int main() { int tmp; tmp = sem_init( &semaphore, 0, 0 ); // initialize pthread_create( &thread[i], NULL, thread_function, NULL );...
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT