I want to store some data into a session so I can transfer it between pages. I will have a lot of images present and need an efficient way of storing data. So I've done
<a href="create2.php?" ><?php?$_SESSION["choice_1"]=2;?><img src="pictures/laptops/reebok.png" height="150"></td></a>;
However the when I call the session in another page, it says it is undefined. I could do it this way or when the image is clicked, I could store some data into a variable, call a function, save the variable into the session and then load the new page. However I do not know how I could do this. Any help will be appreciated Thanks
1st, your html is broken, there's a </td>
that should not be there.
About your question, you don't have to pass the value in the URL (which is not done that way however), you only need the session id if you don't want to use cookies, and use it to start the session. Your link:
<a href="create2.php?<?php echo session_name().'='.session_id(); ?>">
<img src="pictures/laptops/reebok.png" alt="reebok" height="150">
</a>
In create2.php call session_start()
before everything. Of course you don't need to pass the session id in the URL if you use cookies.
Make sure you're calling session_start()
at the top of each of your PHP files before you try to access/modify session variables.
First of all, your php init tag is wrong <?php?
should be <?php
. Second, you should be initializing the session in your php:
<?php
// nothing before here, session_start must be the very first thing on the page
session_start();
And finally, you're not creating any php link that way, you are only defining the variable but it has nothing to do wint the a
tag.
To send the 'choice_1' which is stored in the SESSION global variable trough URL you should do:
<a href="create2.php?choice_1=<?=$_SESSION['choice_1']?>">
<img src="pictures/laptops/reebok.png" height="150">
</a>;