skip to Main Content

I am trying out a php sample code given here: https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php

This gives a log in form, where you can register username password and then log in with a registered user. A welcome page is only visible after you have logged in, and the welcome page shows the specific username of the currently logged in account.

I am trying to modify the welcome.php given in the above link, to add a data entry form that will save some personal data like name and age to a mariadb database. Here is my version of the welcome.php file:

<?php
// Initialize the session
session_start();
 
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}

$uname=htmlspecialchars($_SESSION["username"]);
$name = "";
$age = 0;

if($_SERVER["REQUEST_METHOD"] == "POST"){
    // /*
    echo '<script language="javascript">';
    echo 'alert("submit button clicked")';
    echo '</script>';
    // */
    // /*
    $tempvar = trim($_POST["name"]);
    // $tempvar='sdsd';
    var_dump($tempvar);
    if($tempvar == "")
        echo $tempvar.' found';
    // */
}

?>

 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        body{ font: 14px sans-serif; text-align: center; }
    </style>
</head>
<body>
    <div class="page-header">
        <h1>Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome to our site.</h1>
    </div>
    <p>
        <a href="reset-password.php" class="btn btn-warning">Reset Your Password</a>
        <a href="logout.php" class="btn btn-danger">Sign Out of Your Account</a>
    </p>
    <p>Enter your data here:</p>
    <!-- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> -->
    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
        Name: <input type="text" name="name"><br>
        Age: <input type="text" name="age"><br>
        <input type="submit" name="save" value="submit">
        <!-- <input type="submit" class="btn btn-primary" value="submit"> -->
    </form>
    
</body>
</html>

If I understand this correctly clicking the submit button should generate a post message which should be captured by the php script at the beginning of the file. This much is happening, but I cannot display the content of the text box given by <input type="text" name="name">. The var_dump($tempvar); in the php code at the beginning comes up with String(0) "". I have tried moving the php code to a separate file (as given here in insert.php) instead of attempting to process the post message in the same file, but I am getting the same result. I am not getting any errors.

How do I access the contents of the text box in the post message handler? I am testing this in XAMPP on Windows 10.

3

Answers


  1. Your code works fine for me. I ran it in my system, it shows the submitted name with the var_dump i.e. string(18) "Md Shabbir Hossain".

    Login or Signup to reply.
  2. Did the example with the login work correctly? This would prove that POSTing data works.

    Which version of PHP are you using? There was a feature called register_globals up to 5.4.0 which allowed accessing POST data via named variables. Since you are setting $name = "" this could overwrite your data. I would take it out at that position anyway (use an else clause if necessary). If you have register_globals active either update PHP or turn it off to avoid confusion.

    The next step to debug the issue is to print the whole array of $_POST like mentioned here but more pretty

    if($_SERVER["REQUEST_METHOD"] == "POST"){
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
        die();
    

    This will show you what values were actually POSTed.

    Same can be done with the $_SERVER array like this

    echo '<pre>';
    print_r($_SERVER);
    echo '</pre>';
    die();
    $uname=htmlspecialchars($_SESSION["username"]);
    

    The die() command will halt execution so you need to remove it when you want the script to continue.

    Login or Signup to reply.
  3. There are some flaws that I would fix.

    Initial user get to Welcome.php.

    <?php
    // Initialize the session
    session_start();
     
    
    // Check if the user is logged in, if not then redirect him to login page
    if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
        header("location: login.php");
        exit;
    }
    
    $uname=htmlspecialchars($_SESSION["username"]); //username is not defined or it does not exists yet.
    $name = "";
    $age = 0;
    
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        // /*
        echo '<script language="javascript">';
        echo 'alert("submit button clicked")';
        echo '</script>';
        // */
        // /*
        $tempvar = trim($_POST["name"]);
        // $tempvar='sdsd';
        var_dump($tempvar);
        if($tempvar == "")
            echo $tempvar.' found';
        // */
    }
    

    I would do this:

    <?php
    // Initialize the session
    session_start();
     //Check if the user already logged.
    if(!isset($_SESSION["loggedin"])){
        //Redirect
        header("location: login.php");
        exit;
    }
    
    // Check if post to login is submitted
    if(isset($_POST['save'])){
      // /*
        echo '<script language="javascript">';
        echo 'alert("submit button clicked")';
        echo '</script>';
      $uname = '';
      //Check if Username is submitted
      if(isset($_POST['username'])){
         $_SESSION["username"] = $_POST['username'];
         $uname=htmlspecialchars($_SESSION["username"]);
      }
      $name = "";
      $age = 0;
      
    
      //For test
      var_dump($_POST[]);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search