Search code examples
phphtmlauthenticationsessionregistration

Is there way to transfer login data from index page to another pages?


I've been working on this school project. It's project about Weather info web page. I've done 90% of project but i've been stuck on this part: I created index page with login and registration. Both login and registration have been connected to phpmyadmin/mysql and all works fine. After index.html user then goes to page where he will confirm he's data(data is pulled from mysql). Now problem is when i register new user, it always pulls data of last user in mysql table(eg. i log on with 1st user in table but i get data from last person in table), so i need something to "attach" to user who is logging in, so that page pulls his name and location, not from last user in table. I hope u will understand my problem. I've been using mostly PHP for retriving data then forwarding it to HTML to display data. code of index page with login code of page where u get after login


Solution

  • If I understood right your question, you need a php $_SESSION. You can create $_SESSION after your login form (if successful). And you can check data of this $_SESSION at another PHP pages.

    Example:

    <?php
    // Start the session (it must be in every PHP file, which you use $_SESSION)
    session_start();
    ?>
    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    // Create session
    $_SESSION["session_name"] = "test";
    $_SESSION["user_id"] = 1;
    $_SESSION["username"] = "test_user";
    
    //In other php pages
    //Check session and if exists, retrieve data
    if(isset($_SESSION["user_id"])){
      $user_id = $_SESSION["user_id"];
    }else{
      echo "There are not any logged user.";
    }
    ?>
    
    </body>
    </html>