2

I have 3 pages, I am trying to create a simple member login system using session .

In my first page ( index.php) I have database connection, session setup and this following login from :

<form action="index.php" method="POST">
  <table>
    <tr>
        <td><label>Username</label></td>
        <td><input type="text" name="username" /></td>
    </tr>

    <tr>
        <td><label>Password</label></td>
        <td><input type="password" name="password" /></td>
    </tr>

    <tr>
        <td></td>
        <td><input type="submit" name="submitbtn" value="Login" /></td>
    </tr>

  </table>
</form>

In member's profile page (member.php), I have a table to fetch data from database of that specific member logged in :

<table>
  <?php $members=getMember(); ?>

  <?php  while($member = $members->fetch_assoc()) : ?>

  <tr><td><label>Name</label></td><td><?php echo $member['name'];?></td></tr>
  <tr><td><label>Age</label></td><td><?php echo $member['age'];?></td></tr>

  <?php endwhile; ?>
</table> 

and at dbconnection.php page I have this function :

<?php

function getMember(){
  $db_conn = getConnection();

  $username = isset($_POST['username']) ? $_POST['username'] : '';
  $password = isset($_POST['password']) ? $_POST['password'] : '';  

  if(!$db_conn) return false;

  $sql = "SELECT * FROM member WHERE username ='$username' AND password='$password'";

  $result = $db_conn->query($sql);

  $db_conn->close();

  return $result;
}

The code of session setup are :

<?php 
$username="";
$password="";
$success=true;

$_SESSION['username']=$username;

if(isset($_POST['username']) && isset($_POST['password']))
{
    $username=$_POST['username'];
    $password=$_POST['password'];       

    if(check_in_db($username,$password)){
        $_SESSION['logged_in']=1;
        $_SESSION['username']=$username;
        header("Location: adminPanel.php");
    }
    else{
        $success=false;
    }
}   

?>

But when I am logging in, data ( name and age ) is not fetching ( displaying) there in member.php page ( I can't add image, since my reputation is under 10 ).

Thank you for your time .

unclexo
  • 3,691
  • 2
  • 18
  • 26
Shishir
  • 187
  • 1
  • 8

1 Answers1

0

I would suggest you take a look at php type comparisons for how isset() works. To let you know how php session works and how users persist in different pages, you have to digg into php session. I would recommend you use PDO and its prepare method when you're dealing with user data. Here you would get a very simple example of it.

The following code is working. So please take a look at them how they are constructed:

dbconnection.php

<?php

function getConnection() {

    $servername = "localhost";
    $username = "root";
    $password = "12345";
    $dbname = "db_test";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
}

function check_in_db($username, $password) {
  $db_conn = getConnection();

  if (!$db_conn) {
    return false;
  }

  $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  $result = $db_conn->query($sql);

  return $result->num_rows > 0;
}

function getMember($username, $password) {
  $db_conn = getConnection();

  if (!$db_conn) {
    return false;
  }

  $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result = $db_conn->query($sql);

  return $result;
}

index.php

<?php

session_start();

require_once('./dbconnection.php');

$success = true;

if(isset($_POST['username']) && isset($_POST['password'])) {
  $username = $_POST['username'];
  $password = $_POST['password'];

  if(check_in_db($username, $password)) {
    $_SESSION['logged_in'] = 1;
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    header("Location: adminPanel.php");
  }
  else{
      $success=false;
  }
}
?>

<form action="index.php" method="POST">
  <table>
    <tr>
        <td><label>Username</label></td>
        <td><input type="text" name="username" /></td>
    </tr>

    <tr>
        <td><label>Password</label></td>
        <td><input type="password" name="password" /></td>
    </tr>

    <tr>
        <td></td>
        <td><input type="submit" name="submitbtn" value="Login" /></td>
    </tr>

  </table>
</form>

and member.php

<?php 
    session_start();

    require_once('./dbconnection.php');

    $username = $_SESSION['username'];      
    $password = $_SESSION['password'];
    $members = getMember($username, $password);     

?>

<table>
  <?php  while($member = $members->fetch_assoc()) : ?>
        <tr><td><label>Name</label></td><td><?php echo $member['name'];?></td></tr>
        <tr><td><label>Age</label></td><td><?php echo $member['age'];?></td></tr>
  <?php endwhile; ?>
</table> 
unclexo
  • 3,691
  • 2
  • 18
  • 26