回到页面时显示已经存在的“财富”

"Once a fortune has been generated, if they return to the site they should be shown their existing fortune for the next year from the date it was generated.

If the user wants to generate a new fortune, give them a link to do so."

My problem is I can't figure out how to make it stay on the generated fortune page when going back to the website, I tried searching it up but I did not find anything. I think i already made a reset link but I am unsure. What kind of code do I need to make it stay on the fortune page?

I searched it up online, I've tried somehow making it check that cookies were applied but we were not taught that.

Main PHP Page (index.php)

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Week 4 Tutorial - Fortune Teller</title>
</head>

<body>    
    <img src="fortune-teller.jpg" alt="The Fortune Teller" style="float:right;" />
    <h1>Fortune Teller</h1>
    <p>The fortune teller has looked deep in your soul, and has a sense of who you are and what will come in your life.</p>
    <p>What is your fate? How long will you live? Will you be successful or struggle in life? Find out below...</p>
    <form method="post" action="fortune.php">
        <label for="fname">First Name</label>
        <input type="text" id="fname" name="fname" />
        <br />
        <label for="colour">Favourite Colour</label>
        <input type="text" id="colour" name="colour" />
        <br />
        <label for="birthmonth">Birth Month</label>
        <select id="birthmonth" name="birthmonth">
            <option value="1">January</option>
            <option value="2">February</option>
            <option value="3">March</option>
            <option value="4">April</option>
            <option value="5">May</option>
            <option value="6">June</option>
            <option value="7">July</option>
            <option value="8">August</option>
            <option value="9">September</option>
            <option value="10">October</option>
            <option value="11">November</option>
            <option value="12">December</option>
        </select>
        <br />
        <input type="submit" id="submit" name="submit" value="Tell my fortune" />
    </form>
</body>
</html>

Page after you complete the form

<?php
if(isset($_COOKIE['lastVisit']))
{
$visit = $_COOKIE['lastVisit'];
echo "Your last fortune was told at ". $visit;
}
//set to expire in a year
$year = +31556926 + time();
setcookie('lastVisit', date("G:i - m/d/y"), $year);
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Week 4 Tutorial - Fortune Teller</title>
</head>

<body>    
    <img src="fortune-teller.jpg" alt="The Fortune Teller" style="float:right;" />
    <h1>Fortune Teller</h1>
    <p>The fortune teller has looked deep in your soul, and has a sense of who you are and what will come in your life.</p>
    <p>What is your fate? How long will you live? Will you be successful or struggle in life? Find out below...</p>

    <h2>Fortune for <?php echo $_POST['fname']; ?></h2>
    <p>
    <?php
        if($_POST['fname']=="Tony"){
                echo "You like to control everyone within your influence, to shape things to your own liking. ";
        }
        else if($_POST['fname']=="Ebonie"){
            echo "You're' fine when in harmony but changeable when in discord, because many of your best qualities are then reversed. ";
        }
        else if($_POST['fname']=="Eric"){
            echo "You have a vivid imagination who can bring inspired messages to the world. ";
        }
        else if($_POST['fname']=="Kristina"){
            echo "EXTREMES in fortune, health and spirituality. You are very versatile, idealistic and intuitive. ";
        }
        else if($_POST['fname']=="Jamie"){
            echo "You're' honest, benevolent, brilliant and often inventive, full of high inspirations. ";
        }else{
            echo "You're a decent person at best. ";
        }

        //Birthmonth - something to do with the season they were born in
        if($_POST['birthmonth']<4){
            //January, February, March - Q1 (Winter)
            echo "You are independent, analytic, and a born leader. ";
        }
        else if($_POST['birthmonth']<6){
            //April, May, June - Q2 (Spring)
            echo "You may be perceived as being stubborn, bossy, and impulsive. ";
        }
        else if($_POST['birthmonth']<9){
            //July, August, September - Q3 (Summer)
            echo "You are a sincere, candid, and sympathetic individual.. ";
        }
        else if($_POST['birthmonth']<12){
            //October, November, December - Q4 (Fall)
            echo "You are a practical philosopher who values a stable lifestyle. ";
        }

        //favourite colour - something about your job
        if($_POST['colour']=="red"){
            echo "You will find success in your career this month.";
        }
        else if($_POST['colour']=="blue"){
            echo "You need to be careful as you're on the verge of your employment being termenated.";
        }
        else if($_POST['colour']=="green"){
            echo "Show up to work early, keep your nose clean and your head down and you'll get a raise next month.";
        }
        else if($_POST['colour']=="yellow"){
            echo "A management position is in your future.";
        }
        else if($_POST['colour']=="black"){
            echo "Your difficulty working with others will cause tensions in the workplace in the near future.";
        }
        else{
            echo "Keep doing your job as well as you do now, good things will happen.";
        }
    ?>
    </p>
    <?php
      $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
      echo "<a href='$url'>Reset</a>"; 
    ?>
</body>
</html>

The expected outcome is when you complete the form it will save the last time you got to the page, displaying the date of your last fortune which I got it to do, but it also needs to have you remain on that page the next time you enter the page and with a reset button which takes you back to the form.

I think the problem you are having is that you have the concept of when to check the cookie incorrectly. Seeing as this sounds like a homework problem I will try and give you an answer that guides you in the right direction.

What you would need to do is upon submitting the form this would do a post to the page which causes the cookie to be set. However the PHP logic that checks the cookie should be on any page a visitor can land on (generally this is the main index page). It should check the cookie and display the HTML content appropriate for that user's detected cookie. If no cookie is detected then it can display your base index page. (This can be done by displaying dynamic html elements or simply by redirecting the user to another page)

your index.php is just missing the redirect to the result page.

<?php
// at you index.php
ob_start();
if(isset($_COOKIE['lastVisit'])){
    header("Location: yoursite.com/fortune.php"); 
    ob_flush();
} else { 
?>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Week 4 Tutorial - Fortune Teller</title>
</head>

<body>    
    <img src="fortune-teller.jpg" alt="The Fortune Teller" style="float:right;" />
    <h1>Fortune Teller</h1>
    <p>The fortune teller has looked deep in your soul, and has a sense of who you are and what will come in your life.</p>
    <p>What is your fate? How long will you live? Will you be successful or struggle in life? Find out below...</p>
    <form method="post" action="fortune.php">
    ...
<?php
    ob_flush();
}

But this is kind of dirty. is it possible to use a database? since if you're only using cookies, it will not be permanent ( user can just delete the cookie from their browser, and it will be gone).

Also, in your fortune.php

<?php
if(isset($_COOKIE['lastVisit']))
{
    $visit = $_COOKIE['lastVisit'];
    echo "Your last fortune was told at ". $visit;
}
//set to expire in a year
$year = +31556926 + time(); // i think you're missing something here
setcookie('lastVisit', date("G:i - m/d/y"), $year);
?>

$year = +31556926 + time(); is that a typo? because i don't think $year= +31556926 won't run. anyways, if you do that, it won't end in a year, because everytime the user visits that page (fortune.php), his/her cookie will be set to today (since you're using php's time()) + 31556926. which will never end (the cookie's expiry will always be refreshed.

hope this helps! would be happy to answer if you've got any more questions!