-2

I have created a login so that a user can log into a site, I am using php and phphmyadmin to create the login, i used the same code for another project I am doing and it worked fine but it won't work now and doesn't seem to like line 15, what am I doing wrong.

Here is my code

<?php
    session_start();
?>

<header id="page_header">

<?php

  include "connect.php";

  if (isset($_POST['username']) and isset($_POST['password'])){

$username = $_POST['username'];

$password = $_POST['password'];

$query = "SELECT * FROM users WHERE username='$username' and password='$password'";

$result = mysqli_query($con, $query) or die(mysqli_error());

$count = mysqli_num_rows($result);

if ($count == 1){

$_SESSION['username'] = $username;

}else {

echo "Invalid Login Credentials.";

}


} 

?>




<div id = "menu">
            <nav>
          <ul>
            <li><img src="../img/buzz_party.png"></li>
              <li><a href="index.php">Home</a></li> &nbsp; &nbsp; &nbsp;
              <li><a href="aboutus.php">About Us</a></li> &nbsp; &nbsp; &nbsp;
              <li><a href="advertising.php">Supplies</a></li> &nbsp; &nbsp; &nbsp;
              <li><a href="items.php">Party Supplies</a></li> &nbsp; &nbsp; &nbsp;
              <li><a href="contact.php">Contact Us</a></li> &nbsp; &nbsp; &nbsp;

            </nav>

        </div>

                  <div id = "login_details">

            <?php      

       if (isset($_SESSION['username'])){

$username = $_SESSION['username'];

echo "Hello " . $username . " ";


}

echo "<a href='logout.php'>Logout</a>";

?>

     </div>

    <div id="login">

<form action="index.php" method="post">

          <label for="username" class="uname" data-icon="u" >Username:</label>
          <input id="username" name="username" required="required" type="text" size="10" placeholder="Username"/>


          <label for="password" class="youpasswd" data-icon="p">Password:</label>
          <input id="password" name="password" required="required" type="password" size="10" placeholder="Password" /> 

          <input type="submit" value="Login" /> 

        </form>

      </div>

</header>
John Conde
  • 217,595
  • 99
  • 455
  • 496
Rebekah
  • 77
  • 1
  • 2
  • 11

3 Answers3

4

mysqli_error() requires that the connection be passed as a parameter

mysqli_error($con)

Procedural style

string mysqli_error ( mysqli $link )

Regarding passwords

I noticed that you may be storing passwords in plain text.

  • This is not recommended and is an unsafe method.

Use one of the following:

Other links:


Sidenote: Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.


Edit:

"would anyone have any idea how to make the login form disappear after the user has logged in?"

Bonus answer:

You can redirect to another page with header().

However, you already have output using <header id="page_header"> therefore you will need to place that below your present PHP codes. Otherwise, it will throw a warning of headers already sent. Using ob_start(); sometimes works, but not always.

Where you presently have:

if ($count == 1){

$_SESSION['username'] = $username;

}

Add a header and an exit:

if ($count == 1){

$_SESSION['username'] = $username;

    header("Location: http://www.example.com");
    exit;

}

Or, you can simply use exit; if you don't want to redirect.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

You need to have the connection parameter passed into mysqli_error(), like so:

$result = mysqli_query($con, $query) or die(mysqli_error($con));

Also, you're not sanitizing foreign data from $_POST. You're vulnerable to SQL injection.

David Wyly
  • 1,671
  • 1
  • 11
  • 19
  • 2
    Perhaps give some tips on how to secure against SQLi, like [this answer](http://stackoverflow.com/a/29654448/3000179) – ʰᵈˑ Apr 15 '15 at 15:42
0

You need to give a parameter to mysqli_error():

mysqli_error(connection);

See here: http://www.w3schools.com/php/func_mysqli_error.asp

flo
  • 11
  • 1
  • 7