Home > Back-end >  Passing variables from one php file to another with sessions and resetting the session
Passing variables from one php file to another with sessions and resetting the session

Time:05-10

So I have these two pages: one page has a form that will take some text, and it is supposed to show it on another page. So I have a php script that will catch the value from the input and through session and a php file is suppose to show it on the other page; So here is the code and you can try it online here. Right now it doesn't work at all. I have started the sessions on both pages

You can view these pages online at:

http://www.canonseverywhere.net/test/admin/beta/index.php main page(index)
http://www.canonseverywhere.net/test/admin/beta/content.php the content page
Page 1 that displays the form to catch values; this is index.php:

<html><body>
       <div>
            <ul>
                <li>
                     <div>
                         <form action="enter-content.php"  method="post" enctype="multipart/form-data" > 
                                 <span>
                                        <textarea  name="text"></textarea>
                                 </span>
                                 <br>
                                 <input type="submit" value="submit">
                         </form>
                     </div>
                 </li>
             </ul>
         </div>
</body></html>

Then there is "enter-content.php":

<?php
session_start();


         if(isset($_POST["submit"])) { 
            $_SESSION['text1'] = $_POST['text'];
    
            return false;
            }

?>

and then there is the "content.php" where the passed text content will be displayed

<html>
    <body>
        <div>
            TEST PAGE           
            <p>     
            <?php 
             session_start();
            
             include "enter-content.php"; 
             echo $_SESSION['text1'];
            ?>

            </p>
        </div>
    </body>
</html>

And these is the error I receive when using it on localhost:

That's the error I receive: Notice: Undefined variable: _SESSION in /home/philippe/public_html/canonseverywhere.net/test/admin/beta/content.php on line 18

So I set a simple variable into enter-content.php and then I could use it allright in the content.php page by using sessions. So it seems the problem is with assigning the $_POST[] value to a $_SESSION variable; I did it inside an if sentence and I returned value false and that seems to be incorrect. Thank you!

The cause was: using include instead of session_start OR using include and session_start at the same time;
Solution: using only session_start()

CodePudding user response:

Remove the include 'enter-content' on the 'content' page.

The include calls session_start(), that would be the second call on the same page.

  • Related