index.php
<?php session_start(); $name = $_SESSION['words']['name'] ?? ''; $noun1 = $_SESSION['words']['noun1'] ?? ''; $verb = $_SESSION['words']['verb'] ?? ''; $adjective = $_SESSION['words']['adjective'] ?? ''; $noun2 = $_SESSION['words']['noun2'] ?? ''; ?> <!DOCTYPE html> <html <head> <meta charset="UTF-8" <title>MadLibs</title> </head> <body> <h1>Choose your words</h1> <form action = "story.php" method="post"> <div> <label for="name">Name:</label> <input type="text" name="name" id="name" value="<?php echo $name;?>"> </div> <div> <label for="noun1">Noun:</label> <input type="text" name="noun1" id="noun1" value="<?php echo $noun1;?>"> </div> <div> <label for="verb">Verb:</label> <input type="text" name="verb" id="verb" value="<?php echo $verb;?>"> </div> <div> <label for="adjective">Adjective:</label> <input type="text" name="adjective" id="adjective" value="<?php echo $adjective;?>"> </div> <div> <label for="noun2">Noun:</label> <input type="text" name="noun2" id="noun2" value="<?php echo $noun2;?>"> </div> <div> <input type="submit" name="submit" value="Submit"> </div> </form> </body> </html> <?php // php --ri session ?>
story.php
<?php session_start(); $name = $_POST['name'] ?? ''; $noun1 = $_POST['noun1'] ?? ''; $verb = $_POST['verb'] ?? ''; $adjective = $_POST['adjective'] ?? ''; $noun2 = $_POST['noun2'] ?? ''; $_SESSION['words'] = array( 'name' => $name, 'noun1' => $noun1, 'verb' => $verb, 'adjective' => $adjective, 'noun2' => $noun2 ) ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" <title>MadLibs</title> </head> <body> <h1>Here's Your Story</h1> <p>Yesterday, <?php echo $name;?> decided to buy a <?php echo $adjective;?> <?php echo $noun1;?>. After using it to <?php echo $verb;?> with the <?php echo $noun2;?> they decided to give the <?php echo $noun1;?> to their friend. </p> <p><a href="index.php">Edit the story</a></p> </body> </html>
When you type php –ri session in the terminal you get this screen
in index.php we added the line:
<?php session_start() ?>
a line above the session.save.handler => files => files points the directory on your file system where the session are stored. in my case “sessions.save_path” has no value because I am using mamp pro. So my default folder is /tmp.