redirect – Refreshing page at php-ThrowExceptions

Exception or error:

I have a stupid and simple php page containing authors and their books. There is one text file which lists authors and for each author there is a text file listing his/her books. The books I have read are marked with ‘*’ in the beginning of the line. Already read books are shown with different color on the page. I use this in library so that I do not borrow same books again.

Each book name is a link, by hitting the link the php page toggles the reading status of the clicked book. This works fine, the asterisk is added to the line or removed from the line, no problem with that.

But, the page is not refreshed. I have tried several different ways to do it, no success. After setting a book as read, manual refresh will call the “set read” action again, so there will be two asterisks in the beginning of the line ‘**’. When asterisk is being removed then the re-call does not harm, however I need to manually refresh the page to see the correct status.

I have found several similar topics, but none of them helps. What I’m doing wrong, or is there a better way to do this? I try keep this short and simple, there is always an option of manually updating the text files.

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<title>BOOKS</title>

<style>
   ... style part removed to shorten the code
</style>

</head>
<body>

<?php

$authorFile = fopen ("authors.txt", "r") or die("Author file not found");

while (!feof($authorFile)) {
    $authorName = fgets($authorFile);

    echo '<p class="author">' . $authorName . "</p>";

    $bookFileName = trim($authorName) . ".txt";
    $bookFile = fopen ($bookFileName, "r") or die("Book file not found");

    while (!feof($bookFile)) {
        $bookName = fgets($bookFile);

        echo '<div>';
        if (substr($bookName, 0, 1) == '*' ) {
            echo '<p class="read"><a href="?SetNotRead&Book=' . substr($bookName, 1) . '&File=' . $bookFileName .'">' ;
            echo substr($bookName, 1) . '</a></p>';
        }
        else {

            echo '<p class="notread"><a href="?SetRead&Book=' . $bookName . '&File=' . $bookFileName .'">' ;
            echo $bookName . '</p>';
        }
        echo '</div>';
    }
    fclose($bookFile);
    echo '<hr class = "newauthor">';
}
fclose ($authorFile);

if (isset($_GET['SetNotRead'])) {

    $strToFind = "*" . $_GET['Book'];

    $fileStr = implode ("", file($_GET['File']));

    $bFile = fopen($_GET['File'], 'w');

    $fileStr = str_replace($strToFind, $_GET['Book'], $fileStr);
    fwrite($bFile, $fileStr, strlen($fileStr));
    fclose($bFile);

    header ("Refresh");
}

if (isset($_GET['SetRead'])) {

    $strToFind = $_GET['Book'];

    $fileStr = implode ("", file($_GET['File']));

    $bFile = fopen($_GET['File'], 'w');

    $fileStr = str_replace($strToFind, '*' . $_GET['Book'], $fileStr);
    fwrite($bFile, $fileStr, strlen($fileStr));
    fclose($bFile);

    header ("Refresh");
}

?>
</body>
</html>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *