Storing Form Data as a Session Variable

前端 未结 4 1729
小蘑菇
小蘑菇 2020-11-27 03:58

So I was wondering if it would be possible to store data coming in from a form as a session variable.

Heres what I have so far, but I don\'t know what to put for th

相关标签:
4条回答
  • 2020-11-27 04:37

    To use session variables it's necessary to start the session by using the session_start function, this will allowed you to store your data in the global variable $_SESSION in a persistent way.

    so your code will finally looks like this :

    <strong>Test Form</strong>
    <form action="" method"post">
    <input type="text" name="picturenum"/>
    <input type="submit" name="Submit" value="Submit!" />
    </form>
    
    <?php 
    
     // starting the session
     session_start();
    
    
     if (isset($_POST['Submit'])) { 
     $_SESSION['picturenum'] = $_POST['picturenum'];
     } 
    ?> 
    
    <strong><?php echo $_SESSION['picturenum'];?></strong>
    

    to make it easy to use and to avoid forgetting it again, you can create a session_file.php which will be include in all your codes and will start the session for you

    session_start.php

     <?php
       session_start();
     ?> 
    

    and then include it wherever you like :

    <strong>Test Form</strong>
    <form action="" method"post">
    <input type="text" name="picturenum"/>
    <input type="submit" name="Submit" value="Submit!" />
    </form>
    
    <?php 
    
     // including the session file
     require_once("session_start.php");
    
    
     if (isset($_POST['Submit'])) { 
     $_SESSION['picturenum'] = $_POST['picturenum'];
     } 
    ?> 
    

    that's is the more portable and easy way to maintain in the future.

    other remarks

    • if you are using Apache version 2 or more, be carefull instead of
      <?
      to open php's tags, use <?php, otherwise your code will not be interpreted

    • variables names in php are case-sensitives instead of write $_session, write $_SESSION in capital letters

    good work !

    0 讨论(0)
  • 2020-11-27 04:43

    You can solve this problem using this code:

    if(!empty($_GET['variable from which you get'])) 
    {
    $_SESSION['something']= $_GET['variable from which you get'];
    }
    

    So you get the variable from a GET form, you will store in the $_SESSION['whatever'] variable just once when $_GET['variable from which you get']is set and if it is empty $_SESSION['something'] will store the old parameter

    0 讨论(0)
  • 2020-11-27 04:47

    That's perfectly fine and will work. But to use sessions you have to put session_start(); on the first line of the php code. So basically

    <?php
    session_start();
    
    //rest of stuff
    
    ?>
    
    0 讨论(0)
  • 2020-11-27 04:52

    Yes this is possible. kizzie is correct with the session_start(); having to go first.

    another observation I made is that you need to filter your form data using:

    strip_tags($value);
    

    and/or

    stripslashes($value);
    
    0 讨论(0)
提交回复
热议问题