0

I work a small project to understand the superglobal variable $_SESSION. The current part im at is on page two under the comment " //- Use a foreach loop to display all session cookie names and values.". I don't understand the array to string conversion notice, that I am getting. Also, on my end, I am getting multiple values that are not in the code as well. I've seen a few other questions, but I'm new and I'm not connecting the dots on why this is happening.

I appreciate the help.

Joshua

screen of results

enter image description here

PAGE ONE:

<?php

session_start();

?>

<!DOCTYPE html>
<html lang='en' dir='ltr'>

<head>
    <meta charset='utf-8'>
    <title>Module 7 -- Page One</title>
    <link rel='stylesheet' href='main.css'>
</head>

<body>
    <main>
        <?php
        echo "<h1 class='text_align_center'> Page One:</h1>";
        echo "<hr><br>";
        //TODO:

        //- Start a session and display the session ID.
        $lifetime = 0;
        session_set_cookie_params($lifetime, '/');

        $id = session_id();
        echo 'Your session ID: ' . $id . '.';

        //- Assign the string 'Hello world' as a session cookie. You pick the key for this element.
        $_SESSION['greeting'] = "Hello World";
        echo "<br> <br>";

        //- Then use the $_SESSION array to display the string in this latter cookie.
        echo 'The content of $_SESSION[\'greeting\'] is "' . $_SESSION['greeting'] . '".';

        //- Assign two more ssession cookies(You make up the keys and values), dont display.        
        $_SESSION['greeting2'] = "Hello World the second time!";
        $_SESSION['greeting3'] = "Hello World the third time!";
        echo "<br> <br>";
        echo 'Two more elements were added to $_SESSION!';
        echo "<br> <br>";
        ?>

        <!-- //- Add an HTML hyperlink that will excute page2.php when clicked. -->
        Procede to <a href="page2.php">Page Two</a>.

    </main>
    <footer class='text_align_center'>
        <hr> <small> SomeWebsite &copy; -- <?php echo date('M.jS.Y'); ?> </small>
    </footer>
</body>

</html>

PAGE TWO:

<?php

session_start();

?>

<!DOCTYPE html>
<html lang='en' dir='ltr'>

<head>
    <meta charset='utf-8'>
    <title>Module 7 -- </title>
    <link rel='stylesheet' href='main.css'>
</head>

<body>
    <main>
        <?php
        echo "<h1 class='text_align_center'> Page Two:</h1>";
        echo "<hr><br>";
        //TODO:
        //- Assign 'Murach' to another element of $_SESSION. Use the key 'publisher'.
        $_SESSION['Publisher'] = 'Murach';

        //- Delete the cookie that stored 'Hello world'.
        unset($_SESSION['greeting']);

        //- Use a foreach loop to display all session cookie names and values.        
        foreach ($_SESSION as $key => $value) {
            echo 'The current key is: ' . $key;
            echo '<br>';
            echo 'The current value is: ' . $value;
            echo'<br> <br>';
        }

        echo '<br><br>';
        ?>
        <!-- //- Add another hyperlink that will execute page3.php when clicked. -->
        Procede to <a href="page3.php">Page Three</a>.
    </main>

    <footer class='text_align_center'>
        <hr> <small> SomeWebsite &copy; -- <?php echo date('M.jS.Y'); ?> </small>
    </footer>
</body>

</html>
Joshua Wolfe
  • 160
  • 1
  • 11
  • 1
    Have you looked here? https://stackoverflow.com/questions/20017409/how-to-solve-php-error-notice-array-to-string-conversion-in and something like this: https://stackoverflow.com/questions/4719326/echo-a-multidimensional-array-in-php – Andreas Feb 28 '20 at 21:39
  • 1
    From what you're showing, there shouldn't be any notice. Please share the result of these `echo`s. – Jeto Feb 28 '20 at 21:44
  • Not sure why it's working now... I've been having a strange problem it seems like the server isn't updating my changes. >.> Thanks, gentleman. – Joshua Wolfe Feb 28 '20 at 22:10

0 Answers0