Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5.
- <?php
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $n = $_POST['n'];
- $s1 = $_POST['s1'];
- $s2 = $_POST['s2'];
- $s3 = $_POST['s3'];
- setcookie('n', $n);
- setcookie('s1', $s1);
- setcookie('s2', $s2);
- setcookie('s3', $s3);
- header("Location: next_page.php");
- exit;
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Student Information</title>
- </head>
- <body>
- <h1>Student Information</h1>
- <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
- <label for="n">Name:</label>
- <input type="text" name="n" id="n" required><br><br>
- <label for="s1">Subject 1:</label>
- <input type="number" name="s1" id="s1" required><br><br>
- <label for="s2">Subject 2:</label>
- <input type="number" name="s2" id="s2" required><br><br>
- <label for="s3">Subject 3:</label>
- <input type="number" name="s3" id="s3" required><br><br>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- next_page.php
- <?php
- $cookies = ['n', 's1', 's2', 's3'];
- foreach ($cookies as $cookie) {
- if (isset($_COOKIE[$cookie])) {
- echo $_COOKIE[$cookie] . "<br>";
- }
- }
- ?>
- =====================================================================================
- 6.
- <?php
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $input = $_POST['input'];
- $numbers = explode(',', $input);
- $sum = 0;
- $product = 1;
- $primeCount = 0;
- foreach ($numbers as $number) {
- $number = intval($number);
- if ($number % 2 === 0) {
- $sum += $number;
- }
- if ($number % 2 !== 0) {
- $product *= $number;
- }
- if (isPrime($number)) {
- $primeCount++;
- }
- }
- setcookie('sum', $sum);
- setcookie('product', $product);
- setcookie('primeCount', $primeCount);
- header('Location: p6.php');
- exit;
- }
- function isPrime($number) {
- if ($number < 2) {
- return false;
- }
- for ($i = 2; $i <= sqrt($number); $i++) {
- if ($number % $i === 0) {
- return false;
- }
- }
- return true;
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Variable Length Arguments</title>
- </head>
- <body>
- <h1>Variable Length Arguments</h1>
- <form method="POST" action="">
- <label for="input">Enter a comma separated string:</label>
- <input type="text" name="input" id="input" required>
- <button type="submit">Submit</button>
- </form>
- <?php
- if (isset($_COOKIE['sum']) && isset($_COOKIE['product']) && isset($_COOKIE['primeCount'])) {
- echo '<h2>Previous Results:</h2>';
- echo 'Sum: ' . $_COOKIE['sum'] . '<br>';
- echo 'Product: ' . $_COOKIE['product'] . '<br>';
- echo 'Prime Count: ' . $_COOKIE['primeCount'] . '<br>';
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment