2

Here is my query and it's coming up with $results as NULL when I do a var_dump:

function connect($loginName) {
$password = (isset($_SESSION['password']));
$loginName = (isset($_SESSION['loginName']));

global $db;
$query = "SELECT email, level, password FROM members WHERE email   = '$loginName'";
$result = $db->query($query);
$results = $result->fetch(PDO::FETCH_ASSOC);
return $results;
}

After attempting changes suggested it is giving me these two errors in my welcome form:

Errors in WElcome section of my website

FUNCTIONS PAGE:

<?php 
require('database_connection.php');
//include('index.php')
//session_start();
if ((isset($_POST['loginName'])) && (isset($_POST['password']))){
  $_SESSION['loginName'] = $_POST['loginName'];
  $_SESSION['password'] = password_hash($_POST['password'], PASSWORD_BCRYPT);

$password = isset($_SESSION['password']) ? $_SESSION['password'] : "";
$loginName = isset($_SESSION['loginName']) ? $_SESSION['loginName'] : "";


// Connecting to the database
 }

function connect($loginName) {

 global $db;
$query = "SELECT email, level, password FROM members WHERE email = '$loginName'";
$result = $db->query($query);
$results = $result->fetch(PDO::FETCH_ASSOC);
return $results;
}


//Login

function login($loginName, $password) {
$results = connect($loginName);
if ((isset($_SESSION['loginName'])) === $results['email'] && (isset    ($_SESSION['password'])) === $results['password']) 
 {

if ( 'a' === $results['level'] )  {
$level = "Administrator";
 } 
elseif ( 'm' === $results['level'] ) {
 $level = "Member";
}
else $level = '?';

if ($level === "Administrator"){
        header('Location: /tires/admin/home.php');
        exit();
    }elseif ($level === "Member"){
        header('Location: /tires/member/home.php');
        exit();
    }                           
include('logoutform.php');}



else {

echo "Sorry. You are not in our database";

}};



//Trying to verify the password

 if ((isset($_POST['loginName'])) && (isset($_POST['password']))){
  $_SESSION['loginName'] = $_POST['loginName'];
  $_SESSION['password'] = password_hash($_POST['password'], PASSWORD_BCRYPT);
}
$password = (isset($_SESSION['password']));
 $loginName = (isset($_SESSION['loginName']));
 $hash = '';

global $db;
$h = $db->prepare("SELECT password FROM members WHERE email = '$loginName'");
$h->execute();
$hashing = $h->fetchAll();
foreach ($hashing as $hash) {
    return $hash['password'];
    if($hashing) // will return true if succefull else it will return false 
        { 
            echo 'Query is working';// code here for true
        };
 };

if (password_verify($password, $hash)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
 };




//Logout
function logout() {
$_SESSION = array();
 session_destroy();
 };


 function get_name($results) {
$name = preg_split("/@/", $results['email']);
$name = ucfirst($name[0]);
 return $name;
                        };



function allowed_get_params($allowed_params=[]) {
 $allowed_array = [];
    foreach($allowed_params as $param) {
        if(isset($_GET[$param])) {
            $allowed_array[$param] = $_GET[$param];
        } else {
            $allowed_array[$param] = NULL;
    }
    }
    return $allowed_array;
 };

 $get_params = allowed_get_params(['loginName', 'password']);


?>

AND, This is the login (logout really) form:

 <form method="post" id="logoutform.php">

 <fieldset>
  <legend>Logout</legend>  

                      <?php
 //include('includes/functions.php');
                       //if ( $_SESSION['loginName'] === $result['email']      && $_SESSION['password'] === $result['password']) {
               echo "Welcome, ";
               echo "$level, ";
               echo get_name($results); 
              // print_r($_POST);
               print '<br />';
               //}
              // else{
            //var_dump($result);
          ///}
                      ?>  <br />   <br />  <br />     
                        <input type="submit" name="action" value="logout" />

 </fieldset></form>

THe LOGIN FORM is this one:

<form action="home.php" method="post" id="loginform.inc.html">
<fieldset>

  <legend>Login</legend>    <label for="loginName" class="loginName">Username:</label>
      <input id="loginName" name="loginName" type="text" value="<?php echo     (isset($loginName['loginName'])); ?>"/>
    <label for="password" class="loginName">Password:</label>
      <input id="password" name="password" type="password" value="<?php echo (isset($password['password'])); ?>" />
    <input type="submit" name="submit" value="login" />
</fieldset>
 </form>
aynber
  • 22,380
  • 8
  • 50
  • 63
  • show your full code .. there is no level variable and get_name function in above code – Tariq hussain Feb 28 '15 at 05:09
  • Oh, in looking at your edits, it seems you have several problems. Forget the results coming back as null. You have all sorts of other issues here. post logoutform.php – Evan de la Cruz Feb 28 '15 at 05:11

2 Answers2

2

It appears that $results is empty because your query is not returning anything. And I suspect your query is not returning anything, because you're doing a search for the $loginName of TRUE (or maybe FALSE), because of this line:

$loginName = (isset($_SESSION['loginName']));

isset() returns true or false. I suspect that's not what you want.

It's also unclear what your global $db is about. I hope that's psuedo code. If that is your literal code, then you have a clear bug in that you aren't even attempting to connect to the database.

Also, you should be passing the login name using a paramaterized statement to avoid an obvious security hole.

Community
  • 1
  • 1
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
2

Change

$password = (isset($_SESSION['password']));
$loginName = (isset($_SESSION['loginName']));

to

$password = isset($_SESSION['password']) ? $_SESSION['password'] : "";
$loginName = isset($_SESSION['loginName']) ? $_SESSION['loginName'] : "";
Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33