Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Registration works but somehow login not working

Featured Replies

I am new to PHP and was following a login_registration script the registration process works fine and the user is added to the database, however, when I try to log in it is not working

registration.php

<!DOCTYPE html>
<html>
<head>
<meta charset ="utf-8">
<title>Registartion</title>
<link rel ="stylesheet"href="style.css"/>
</head>
<body>
 <?php
 require ('connect.inc.php');
 
  if(isset($_REQUEST['username'])){
     
      $username =stripslashes($_REQUEST['username']);
      $username =mysqli_real_escape_string($conn,$username);
      $email =stripslashes($_REQUEST['email']);
      $email =mysqli_real_escape_string($conn,$email);
      $password =stripslashes($_REQUEST['password']);
      $password =mysqli_real_escape_string($conn,$password);
      $trn_date = date("Y-m-d H:i:s");
           
           $query ="INSERT INTO `users2` (username,password,email,trn_date) VALUES('$username','".md5($password)."','$email','$trn_date')";
           $result = mysqli_query($conn,$query);
           if($result){
               echo "<div class='form'>
               <h3>You are registered succesfully</h3><br>
               Click here to <a href='login.php'>Log in</a>
               </div>";
           }
  }else{
 ?>
 
 <div class="form">
   <h1>Registration</h1>
   <form name="registration" action ="registration.php" method="post">
       <input type="text" name ="username" placeholder ="Username" required/>
       <input type="email" name ="email" placeholder ="Email" required/>
       <input type="password" name ="password" placeholder ="Password" required/>
       <input type="submit" type="submit" value="Register"/>
   </form>
 </div><!--form-->
 <?php } ?>
</body>
</html>

login.php

 

<!DOCTYPE html>
<html>
<head>
<meta charset ="utf-8">
<title>Login</title>
<link rel ="stylesheet"href="style.css"/>
</head>
<body>
 <?php
//ini_set('display_errors','1');
//error_reporting(E_ALL);
  require ('connect.inc.php');
  session_start();
  if(isset($_POST['username'])){
      $username = stripslashes($_REQUEST['username']);
      $username = mysqli_real_escape_string($conn,$username);
 
      $password = stripslashes($_REQUEST['password']);
      $password = mysqli_real_escape_string($conn,$password);
     
      $query ="SELECT * FROM `users` WHERE username ='$username' and password ='".md5($password)."' ";
     
      $result =mysqli_query($conn,$query) or die(mysql_error());
      $rows = mysqli_num_rows($result);
     
      if($rows==1){
        $_SESSION['username'] = $username;
        header("Location: index.php");
      }else{
          echo "<div class='form'>
           <h3>Username/password is incorrect</h3><br>
           Click here to <a href='login.php'>Login</a>
          </div>";
      }
  }else{
 ?>
 <div class="form">
  <h1>Login </h1>
  <form action="login.php" method="post" name="login">
       <input type="text" name ="username" placeholder ="Username" required/>
       <input type="password" name ="password" placeholder ="Password" required/>
       <input type="submit" name="submit" value="Login"/>
  </form>
  <p>Not registered yet? <a href='registration.php'>Register here</a>
 </div>
 <?php } ?>
</body>
</html>

auth.php

 

<?php
  session_start();
  if(!isset($_SESSION['username'])){
      header("Location:login.php");
      exit();
  }
?>

connect.inc.php

<?php
//$conn_error = "could not connect";
$mysql_host= "localhost";
$mysql_user = "root";
$mysql_pass ="";
$mysql_db ="a_database";
 
$conn = mysqli_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db);
 
 /*if(!mysqli_connect($mysql_host,$mysql_user,$mysql_pass) && !mysqli_select_db($mysql_db)){
    die($conn_error);
 }
 */
 if(!$conn){
     die("Connection failed: ". mysqli_connect_error());
     
 }
 
?>

index.php

?php
  include('auth.php');
?>
 
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>Welcome home</title>
 
 </head>
 <body>
 <div class="form">
      <p>Welcome <?php echo $_SESSION['username'];?></p>
      <p>This is secure area</p>
      <a href="logout.php">Logout</a>
  </div>
 </body>
</html>

 

the registration works but login not working

Any help will be highly appreciated. Thanks.(looking for mysqli procedural soultion only)

Do note this is just for learning the purpose. I know there are flaws but please be easy.

Edited by sash_oo7

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.