1

Problem:

So when I login, a user can go to the browse.php to add images to the favorites.php, and the photos will appear on that page.

When I log out and log back in, the images are gone (not saved).

Question:

How do I save it after I logout so on next login I get to see the favorite'd images from that particular user using cookies or other methods (can't use database because I'm not allowed to)?

My favorites page

<?php
session_start();
require_once 'favoritesfunction.php';
if (isset($_GET["fileName"])) {
    $fileName = $_GET["fileName"];
    addToFavorites($fileName);
}
?>
<!DOCTYPE html>
<html>

<head>
    <?php
    $title = "Travel Photos - View Favorites";
    ?>
    <link rel="stylesheet" href="viewfavorites.css" />
</head>

<body>

    <main class="container">
        <h1 class="favheader">Favorites</h1>

        <div class="content">
            <?php
            if (isset($_SESSION['favorites']) && isset($_SESSION['email'])) {
                $favorites = $_SESSION['favorites'];
                if (count($favorites) > 0) {
                    ?> <ul class="ul-favorite"> <?php
                        foreach ($favorites as $f) {
                            echo '<img class="displayPic" src="https://storage.googleapis.com/assignment1_web2/square150/' . $f . '">';
                            }
                            ?>
                    </ul>
                    <p id="removeall"><button class="button"><span><a target="" href="services/removefavorites.php?remove=all">Remove All</a></span></button></p>
                <?php
                    }
                } else {

                    ?>
                <div class="nofavorites">
                    <p>No favorites found.</p>
                </div>
            <?php
            }
            ?>
        </div>
    </main>


</body>
<script type="text/javascript" src="main.js"></script>

</html>

My add to favorites function php

<?php

function addToFavorites($fileName)
{

    //Checks if the session favorites exists
    if (isset($_SESSION['favorites'])) {
        $favorites = $_SESSION['favorites'];
    } else {
        $favorites = array();
        $_SESSION['favorites'] = $favorites;
    }


    // add item to favourites
    $item = $fileName;
    $match = false;

    //Loop below checks for duplicates
    $i = 0;
    while ($i < count($favorites)) {
        if ($favorites[$i] == $item) {
            $favorites[$i] = $item;
            $match = true;
        }
        $i++;
    }

    //if match equals false, that means its not in the favorites list of the user
    //so it is added to the user's favorites array
    if ($match == false) {
        $favorites[] = $item;
    }
    $_SESSION['favorites'] = $favorites;



    $_SESSION['favorites'] = $favorites;
} 

My approach: I tried doing something like setting $name = $_SESSION['email'] and $value="<img src=...>" then setcookie($name, $value) and then if(isset($_COOKIE[$value])) echo $value

I know this is totally wrong, I tried looking everywhere online to try to solve this, if I could get some help that would be great thanks!

Community
  • 1
  • 1
  • Instead of saving the `$favorites` to the session, you should write that information to a persistent data store. MySQL is probably the most popular but you can use anything that can handle persistent storage. When your user is logged out, php does this by removing the session cookie file and all of the data with it. So, when your user adds images to the favorites, put each of these array elements in a table with a foreign key to the user id. Then when the user logs in, read that information back based on the same user id. – Alex Barker Dec 07 '19 at 00:38
  • 1
    Use a cookie https://www.php.net/manual/es/reserved.variables.cookies.php or a JS cookie – Diego Ponciano Dec 07 '19 at 00:38
  • @AlexBarker yeah I know that would be the easiest but my prof said I can't use database to store and have to use cookies – scoobidydoo Dec 07 '19 at 00:44
  • Follow the example provided by @DiegoPonciano. You need to use a browser cookie, not a server side "session" cookie. I know, it is a little confusing at first. – Alex Barker Dec 07 '19 at 00:45
  • @AlexBarker so i would have to use ```$HTTP_COOKIE_VARS``` instead of ```$_COOKIE```? – scoobidydoo Dec 07 '19 at 00:49
  • He can use any, server or client side: read https://www.php.net/manual/es/function.setcookie.php and/or https://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript – Diego Ponciano Dec 07 '19 at 00:51
  • 1
    No, no. You use $_COOKIE instead of $_SESSION. Cookies are stored on the computer with the web browser and the session is stored on the server. The session also uses a cookie in the browser for identification of the information stored on the server. So, to summarize, $_COOKIE wille write data to the bowser and $_SESSION will write data to the server. Logging out will destroy the server side data, but not the client side $_COOKIE. – Alex Barker Dec 07 '19 at 00:52
  • @AlexBarker oh, so for my code I should also include $_COOKIE where theres a $_SESSION? Or replace all my $_SESSION with $_COOKIE? – scoobidydoo Dec 07 '19 at 00:59
  • Only the data you need to save that is client related will be saved as a cookie the rest can be session. – Diego Ponciano Dec 07 '19 at 01:01
  • @AlexBarker ok I kinda get it, can you tell me which part in my code needs to be in cookies? That would be a great help, thanks! – scoobidydoo Dec 07 '19 at 01:04
  • That is what your prof wants you to understand :) – Diego Ponciano Dec 07 '19 at 01:08
  • `$_SESSION['favorites']` should be the `$_COOKIE['favorites']` all the rest is fine. – Diego Ponciano Dec 07 '19 at 01:15
  • @DiegoPonciano Do i have to use ```setcookie``` at all? I still can't get it to work, on my favorites page to view the images after i logout and login again do i do something like ```if(isset($_COOKIE['favorites']){echo images}```? – scoobidydoo Dec 07 '19 at 03:51
  • @scoobidydoo here is some more documentation, I can't explain this in 500 chars. https://www.php.net/manual/en/features.cookies.php You will need to create the cookie with setcookie. This will take the place of `session_start()` in you example. – Alex Barker Dec 08 '19 at 00:42

0 Answers0