0

I am supposed to assign "Hi stranger" as a session cookie. Am I doing this correctly? I see assigning a string to a cookie and assigning a value to a variable as the same thing.

Also, how do I print $_SESSION['sess_var'] in the same line as "Hi stranger"?

<?php
    $sess_thanksgiving = "Turkey, gobble gobble";
    $sess_christmas = "Santa Clause";
    session_start();
?>

<html>
    <head>
        <title></title>
    </head>
    <body>
        <?php
            $_SESSION['sess_var'] = "Hi stranger";
            $_SESSION['sess_christmas'] = "Santa Clause";
            $_SESSION['sess_thanksgiving'] = "Turkey, gobble gobble";

            echo "The session ID is ".session_id(); 
            echo "<br>Page 1 <br>";
            echo "The content of the session variable is " .$_SESSION['sess_var']."<br>";
            echo "Two or more elements were added to <br>";  
        ?>

        <a href="part2.php">Next Page</a>
    </body>
</html>
Leon Adler
  • 2,993
  • 1
  • 29
  • 42
agentmg123
  • 17
  • 7
  • 1
    Have you tried running this? Did it run ? Break ? Any error ? What is it that you want to achieve ? – Captain Red Feb 04 '16 at 21:23
  • 1
    Where is `Hello stranger`? – Barmar Feb 04 '16 at 21:25
  • 2
    What are the `$sess_thanksgiving` and `$sess_christmas` variables for? Why aren't you using them when you assign to the session variables? – Barmar Feb 04 '16 at 21:26
  • You seem to have a basic misunderstanding. Session variables are not the same thing as cookies. Session variables are saved on the server, cookies are on the client. The only relationship is that there's a `PHPSESSID` cookie that's sent to the client, so that the server can find the session data the next time the client connects. – Barmar Feb 04 '16 at 21:28
  • See http://stackoverflow.com/questions/359434/differences-between-cookies-and-sessions – Barmar Feb 04 '16 at 21:28
  • I tried running it and it would only output "The content of is Hello stranger". I don't know how to make it **The content of $_SESSION['sess_var'] is Hello stranger**. Don't worry about the sess_christmas and sess_Thanksgiving. I will do something to them later on. – agentmg123 Feb 04 '16 at 21:46
  • thats because you are not asking it to output any other session data. only the session var, if you want to out put the rest you need to code them into the output block. if you just want to know if they are set, print_r($_SESSION); – CodingInTheUK Feb 04 '16 at 21:50
  • How do I turn that into a session cookie? Isn't "$_SESSION['sess_var'] = "Hi stranger";" a session cookie already? – agentmg123 Feb 04 '16 at 21:53
  • No, its a super variable, a session cookie holds a session id and is set automatically when the session is started and its expiration time updated with each subsequent page load. – CodingInTheUK Feb 04 '16 at 22:19

1 Answers1

0

Per your comment, this is how to make it print out the way you want.

echo 'The content of $_SESSION[\'sess_var\'] is ' . $_SESSION['sess_var'];

CodingInTheUK
  • 930
  • 7
  • 16
  • How do I turn that session variable to a session cookie? – agentmg123 Feb 04 '16 at 22:12
  • You dont store data like that to a session cookie. the session itself holds the data, the cookie just holds the unique id that you then validate before the session data is used with the user. – CodingInTheUK Feb 04 '16 at 22:17
  • I need to assign the string 'Hi stranger' as a session cookie. And i get to pick the key element. And then I need to use the $_SESSION array to display the string in this latter cookie. Oh no, I am doing this all wrong – agentmg123 Feb 04 '16 at 22:28
  • You are not, i think you are just confusing the terminology. Cookies can be used to hold data (although its considered insecure) but what you need to be doing is storing the data to the session. The session cookie as stated earlier is just a part of the session setup. – CodingInTheUK Feb 05 '16 at 09:13
  • does this mean I am assigning the string "hi stranger" to a session cookie with the key "greeting"? `setcookie("greeting", "Hi stranger!");` – agentmg123 Feb 07 '16 at 03:34
  • not a session cookie, just a cookie named greeting that holds your greeting – CodingInTheUK Feb 07 '16 at 20:06
  • I think you need to adjust your thinking, all cookies are technically session cookies in that they all expire after a set time (EG a set session.) You can set an extraordinarily long expiry time which would for all intent and purpose make them permanent or at least until the browsers cache was cleared or no longer existed – CodingInTheUK Feb 07 '16 at 20:08
  • I still don't understand it :( how do I assign "hi stranger" as a session cookie to the key "greeting"? – agentmg123 Feb 07 '16 at 21:13
  • http://www.tutorialspoint.com/php/php_cookies.htm this should help you understand cookies, as i said there is no "value" to your setting anything to a session cookie, if you use php sessions then the cookie is set automatically and it is not altered (usually) by you specifically. You are confusing the appropriate usage of cookies and session cookies and you need to research this further. I have tried several times to get the point across and im sorry but i seem to lack that ability. Please look on php.net and the link i just posted and you should get a better grasp if things. – CodingInTheUK Feb 08 '16 at 13:41