Search code examples
phpinputfieldremember-me

Remember fields across pages?


I have this: main.php:

ID: <input name="id" id="id" type="text" size="20" value="<?php echo $_POST['id']; ?>"><br>
Password: <input name="password" id="password" type="password" value="<?php echo $_POST['password']; ?>" size="20">

File main.php is index page.

File main.php is a form with action="main.php"

When i go from main.php to console.php, i dont get the values id and password.

When i go from main.php to plugins.php, i dont get the values id and password.

From main.php, you can get into plugins.php and console.php. But you don't get the values then.

I know why it happens.

How to fix this? How can i make, that when i go to console.php, or plugins.php, the fields will stay remembered?

Sessions are too hard for me to learn. Is there any other solution?


Solution

  • Too hard? Put simply, at the top of every page you want to use sessions with, use session_start(). Now, you want to save something to a session?

    #page1
    session_start();
    $_SESSION['foo'] = 'bar';
    
    #page2
    session_start();
    echo $_SESSION['foo'] #echoes bar
    
    unset($_SESSION['foo']); #destroy foo
    #if you want to discard the entire session, use
    session_destroy(); #going down! 
    

    So, in your case, maybe something like:

    #main.php
    session_start();
    if (isset($_POST['id'], $_POST['password'])) {
        $_SESSION['id'] = $_POST['id'];
        $_SESSION['password'] = $_POST['password'];
    }
    #rest of main.php
    
    #console.php and plugins.php
    session_start();
    if (isset($_SESSION['id'], $_SESSION['password']))
        #do stuff
    

    I feel bad for spoon-feeding you with it, but basic session functionality is really all that's needed in the case you're describing, and it's probably the easiest way to pass around sensitive info, and is basically what sessions were made for.

    For more info: