0

Having some issues with php 5.6 on xampp.

I'm finding that some functions don't work. Namely mysqli_connect_error and mysqli_num_rows.

I am using Atom for my text editor and the colors are off on those particular functions.

Can somebody explain to me why? Is there a way to fix this?

Cheers

Tim

<?php

session_start();

$host = 'localhost';
$user = 'tim_williams';
$pass = 'baroness';
$db  = 'php_db05';

$link = mysqli_connect($host, $user, $pass, $db);

if(!$link) {
  die("Database connection failed: " . mysqli_connect_error());
}

//if click on log in and fields are populated
if(isset($_POST['login']) && !empty($_POST['username']) && !empty($_POST['password'])) {

    $username = ($_POST['username']);
    $password = ($_POST['password']);






// Check for matching row
    $sql = "SELECT  *
            FROM    registeredusers
            WHERE   UserName = '$username'
            AND     Password = '$password'";



  //run query
  $select = mysqli_query($link,$sql);
  //store result as array
  $result = mysqli_fetch_array($select);
  //count number of rows
  $count = mysqli_num_rows($select);



  //if count has a value of one returned row
  if($count === 1) {
  //set query
      $qry = "UPDATE  registeredusers
              SET     LastLogin = NOW(),
                      NumLogins = (NumLogins+1)
              WHERE   UserName = '$username'";

  //run query
      $update = mysqli_query($link, $qry);

// Set SESSION vars to store login
    $_SESSION['user']['login']      = true;
    $_SESSION['user']['name']       = $user['FirstName'].' '.$user['Surname'];
    $_SESSION['user']['username']   = $user['UserName'];
    $_SESSION['user']['id']         = $user['UserID'];

// Redirect

    header("Location: mysql-project-users-manage.php");
    exit;

    } else {
    echo "Please enter your details again, either your password or user name is incorrect.";
  }
}

mysqli_close($link);


?>




<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="./css/layout.css" media="screen" type="text/css" charset="utf-8">
    <link rel="stylesheet" href="./css/menu.css" media="screen" type="text/css" charset="utf-8">
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div class="holder">

      <div class="header"></div>
      <div class="navbar">
        <nav>
          <ul>
            <li><a href="mysql-project-users-login.php">Login</a></li>
            <li><a href="mysql-project-users-add.php">Register</a></li>
          </ul>
        </nav>
      </div>
      <div class="content">
        <div class="pageheading">
          <h1>User Log In</h1>
        </div>
        <div class="contentleft">
          <h2>Welcome back to my website</h2><br />
            <h6>Please enter your details to log in, or if you are a new user please register an account with us.</h6>
        </div>
        <div class="contentright">
          <form class="registerform" action="" method="post">
            <input class="styletxtfield" type="text" name="username" placeholder="Enter Username" value=""><br /><br />
            <input class="styletxtfield" type="password" name="password" placeholder="Enter Password"value=""><br /><br />
            <input type="submit" name="login" value="Log in">
            <input type="submit" name="register" value="Register"><?php if(isset($_POST['register'])) { header("Location: mysql-project-users-add.php");}?>
          </form>

        </div>

      </div>
      <div class="footer"></div>

    </div>

</body>
</html>
Tim Williams
  • 69
  • 1
  • 9
  • 1
    So do you get any errors or is this question just about the highlighting of your editor? – Rizier123 Jun 04 '16 at 14:13
  • Yes I am getting errors as well for them.... – Tim Williams Jun 04 '16 at 14:14
  • 2
    Then please add the relevant code into your question and add the full error messages. – Rizier123 Jun 04 '16 at 14:14
  • 1) Also add the error messages 2) "relevant" see what I mean with that: [mcve] – Rizier123 Jun 04 '16 at 14:24
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared statement and parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jun 04 '16 at 14:40
  • Have you checked that you have the `mysqli` extension activated in your `php.ini` file i.e. `extension=php_mysqli.dll` without a `#` comment in col 1. I cannot think why it would be turned off in PHP5.6 but it is worth checking – RiggsFolly Jun 04 '16 at 14:41
  • No how do you go about doing that? – Tim Williams Jun 04 '16 at 14:42
  • Can anyone see if there are any errors for this log in code? It's just not working.... – Tim Williams Jun 04 '16 at 14:43
  • Edit `\xampp\apache\php.ini` I am guessing that path as I have not used XAMPP in a few years now – RiggsFolly Jun 04 '16 at 14:43
  • You should not be storing _plain text passwords_ PHP provides [`password_hash()`](http://php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://php.net/manual/en/function.password-verify.php) please use them, I might want to use your site one day And here are some [good ideas about passwords](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) – RiggsFolly Jun 04 '16 at 14:46
  • Cheers mate I'm using the hash at the register stage of the website, should I be using it on the login area as well? – Tim Williams Jun 04 '16 at 14:47
  • @Tim - well the plaintext login password needs to check against the hashed password from the database, so you need to use password_verify() to compare the two – Mark Baker Jun 04 '16 at 14:56
  • Not sure how to verify a password that was hashed on a different php file...I'm new to php – Tim Williams Jun 04 '16 at 15:13
  • How do you hash the password in the other file – RiggsFolly Jun 04 '16 at 16:11
  • $password1 = password_hash("$password 1", PASSWORD_DEFAULT); – Tim Williams Jun 04 '16 at 16:25

0 Answers0