Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Two warnings show but I can hardly specify which made the following codes erroneous:
PHP
  1  <?php
  2  session_start();
  3  ?>
  4  <!DOCTYPE html>
  5      <head>
  6          <meta charset="UTF-8" />
  7          <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">  -->
  8          <title>LRMS Login</title>
  9          <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 10          <!--<link rel="shortcut icon" href="../favicon.ico">-->
 11          <link rel="stylesheet" type="text/css" href="css/login.css" />
 12  	<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
 13      </head>
 14      <body>
 15          <div class="container">
 16              <section>				
 17                  <div id="container_demo" >
 18                      <a class="hiddenanchor" id="toregister"></a>
 19                      <a class="hiddenanchor" id="tologin"></a>
 20                      <div id="wrapper">
 21                          <div id="login" class="animate form">
 22                              <form  action="filter.php" autocomplete="on" method="post"> 
 23                                  <h1>System Security</h1> 
 24                                  <p> 
 25                                      <label for="username" class="uname" data-icon="u" >Username </label>
 26                                      <input id="username" name="username" required="required" type="text" placeholder="John"/>
 27                                  </p>
 28                                  <p> 
 29                                      <label for="password" class="youpasswd" data-icon="p">Password </label>
 30                                      <input id="password" name="password" required="required" type="password" placeholder="secret01" /> 
 31                                  </p>
 32                                  <p class="keeplogin"> 
 33  									<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" /> 
 34  									<label for="loginkeeping">Keep me logged in</label>
 35  								</p>
 36                                  <p class="login button"> 
 37                                      <input type="submit" value="Login" /> 
 38  								</p>
 39                                  <p class="change_link">
 40  									Not a member yet ?
 41  									<a href="#toregister" class="to_register">Register</a>
 42  								</p>
 43                              </form>
 44                          </div>
 45  
 46                          <div id="register" class="animate form">
 47                              <form  action="filter.php" autocomplete="on" method="post"> 
 48                                  <h1>Register Here</h1> 
 49                                  <p> 
 50                                      <label for="firstnamesignup" class="fname" data-icon="u">Your firstname</label>
 51                                      <input id="firstnamesignup" name="firstnamesignup" required="required" type="text" placeholder="Admin Firstname" />
 52                                  </p>
 53                                  <p> 
 54                                      <label for="lastnamesignup" class="lname" data-icon="u">Your lastname</label>
 55                                      <input id="lastnamesignup" name="lastnamesignup" required="required" type="text" placeholder="Admin Lastname" />
 56                                  </p>
 57                                  <p> 
 58                                      <label for="emailsignup" class="email" data-icon="e">Your email</label>
 59                                      <input id="emailsignup" name="emailsignup" required="required" type="email" placeholder="Admin Email Address" />
 60                                  </p>
 61                                  <p> 
 62                                      <label for="usernamesignup" class="uname" data-icon="u">Your username</label>
 63                                      <input id="usernamesignup" name="usernamesignup" required="required" type="text" placeholder="Admin Username" />
 64                                  </p>
 65                                  <p> 
 66                                      <label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
 67                                      <input id="passwordsignup" name="passwordsignup" required="required" type="password" placeholder="Admin Password"/>
 68                                  </p>
 69                                  <p class="signin button"> 
 70  									<input type="submit" value="Sign up"/> 
 71  								</p>
 72                                  <p class="change_link">
 73  									Already a member ?
 74  									<a href="#tologin" class="to_register"> Go and log in </a>
 75  								</p>
 76                              </form>
 77                          </div>
 78  						
 79                      </div>
 80                  </div>  
 81              </section>
 82          </div>
 83          <?php
 84              include "db.php";
 85              
 86              function register($id,$uname,$pass,$fname,$lname,$email){
 87                  $q="insert into users values ('$id','$uname','$pass','$fname','$lname','$email')";
 88                  mysqli_query("alter table users auto_increment = 1");
 89                  if(mysqli_query($q)){
 90                      mkdir("$dir", 0700);
 91                      echo "<script language='javascript'>
 92  				alert('User Registered');
 93  				window.location = 'filter.php';
 94  				</script>
 95  				";
 96                      
 97                  }
 98                  else{
 99                       echo "<script language='javascript'>
100  				alert('Registration Failed Or User Already Registered');
101  				window.location = 'filter.php';
102  				</script>
103  				";
104                  }
105              }
106              if(isset($_POST['usernamesignup'])){
107                  $uname=$_POST['usernamesignup'];
108                  $pass=$_POST['passwordsignup'];
109                  $pass_crypt=md5($pass);
110  				$fname=$_POST['firstnamesignup'];
111  				$lname=$_POST['lastnamesignup'];
112  				$email=$_POST['emailsignup'];
113              
114                  register('',$uname,$pass_crypt,$fname,$lname,$email);
115              }
116              if(isset($_POST['username'])){
117                  include "db.php";
118                  $username=$_POST['username'];
119                  $password=$_POST['password'];
120                  $pass_crypt=md5($password);
121              
122                  $q ="SELECT * FROM users WHERE username = '$username' and password = '$pass_crypt'";
123                  $query=mysqli_query($config,$q);
124                  // Check username and password match
125                  if (mysqli_num_rows($query) == 1){
126                          // Set username session variable
127                          $_SESSION['username']=$_POST['username'];
128                          // Jump to secured page
129                          //header("Location: home.php");
130  						echo '<script type="text/javascript">
131  							location.replace("home.php");
132  							</script>';
133                  }
134                  else{
135                          echo "
136                          <script language='javascript'>
137                                  alert('Invalid Credentials');
138                          </script>
139                          ";
140                  }
141              }
142          ?>
143      </body>
144  </html>


What I have tried:

The error occur here:
PHP
 88  mysqli_query("alter table users auto_increment = 1");
 89  if(mysqli_query($q)){

I am so sorry. I don't know how to put line numbers (I don't know what it's called) :) .

I haven't tried any. But if anyone here who could share how to solve this (I guess its pretty simple to you) problem, I would be very grateful. Thanks all.
Posted
Updated 14-Nov-20 2:32am
v3
Comments
Richard Deeming 7-Aug-20 6:19am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
Richard Deeming 7-Aug-20 6:20am    
You're also storing an unsalted MD5 hash of your users' passwords. Don't do that. MD5 has not been considered "secure" for many decades now.

Use PHP's built-in functions to do the right thing when storing / validating passwords:
PHP: password_hash[^]
PHP: password_verify[^]

Read the error message:
mysqli_query() expects at least 2 parameters, 1 given in lines 88 and line 89
Now, I have no idea which lines are 88 and 89 - and I'm not about to count them - but your whole method of accessing the DB is wrong, and it's very, very likely that code like this is what is causing your problem:
PHP
$q="insert into users values ('$id','$uname','$pass','$fname','$lname','$email')";


Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that throughout your whole app - and if you miss one, someone will find it - and the problem you have noticed will likely disappear at the same time.
 
Share this answer
 
Comments
JeffLiteral 7-Aug-20 5:02am    
I don't know how to fill the line numbers for each line of codes. My apology. I am a newbie in this php world. Nothing to do but its a requirement for this new normal atmosphere. More power.
OriginalGriff 7-Aug-20 5:08am    
So don't dump the whole code on us, show us the relevant code fragment, and indicate which line(s) it is talking about.
It's on Line 89. mysqli_query() function requires two parameters - the connection and the query. Kindly refer to Line 123.

if (mysqli_query($connection, $query)){
...
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900