If Cookie is disabled by browser session do not work.
The most common case is mobile phone browser. In mobile phone browser cookie is disabled by default.
But Session and Cookie is strongly co-related. Because using cookie value server can recall which user is currently requesting.
Solution:
To make the solution understandable I have made three php file.
play.php
play2.php
play3.php
creating session
Expand|Select|Wrap|Line Numbers
- <?php
- //play.php
- session_start();
- ?>
- <form action="play2.php" method="post">
- <input type=text name="name">
- <input type=hidden value=<?php echo (session_name());?> name="session_name">
- <input type=hidden value=<?php echo (session_id());?> name="session_id">
- <input type=submit>
- </form>
After creating the session name and id is stored in hidden field.
storing two hidden field is not necessary. necessary part is session_id(). There is also a text field which will be stored through the play2.php in session
//save a text in the session which was created in play.php
Expand|Select|Wrap|Line Numbers
- <?php
- //play2.php
- $_COOKIE[$_POST['session_name']]=$_POST['session_id'];
- session_start();
- $_SESSION['myname']=$_POST['name'];
- ?>
- Some informatoin
- <br>
- <br>
- <a href="play3.php?session_name=<?php echo (session_name());?>&session_id=<?php echo (session_id());?>">go to next page</>
Interestingly session_start() function take the session id from $_SESSION['PHPSESSID']. What we did is create a cookie name PHPSESSID and store the session value from POST data. After that we called session_stat() function. and next thing the value from the text field to session.
//restoring value from session which is saved in play2.php
Expand|Select|Wrap|Line Numbers
- <?php
- //play3.php
- $_COOKIE[$_GET['session_name']]=$_GET['session_id'];
- session_start();
- echo $_SESSION['myname'];
- ?>