November 22, 20196 yr 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 November 22, 20196 yr by sash_oo7
November 22, 20196 yr It could be a number of things, but at a glance you seem to be inserting data into a table called "users2" and doing the user lookup on a table called "users".
Create an account or sign in to comment