skip to Main Content

I’m trying to solve a problem pertaining to PHP.
So far I have created an html login form login.html and a PHP file login-insert.php. I’m having trouble detecting if the username already exists in the database created using phpmyadmin. I’m very new to coding in PHP so any help is appreciated

Please refer to link for PHP and html code https://liveweave.com/rAE7Cy

2

Answers


  1. Step 1
    create a file config.php

    <?php
       define('DB_SERVER', 'localhost:3036');
       define('DB_USERNAME', 'root');
       define('DB_PASSWORD', 'rootpassword'); //modify it as per your password
       define('DB_DATABASE', 'database'); //modify it as per your databse
       $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
    ?>
    

    Step 2
    create another file login.php

    <?php
       include("config.php");
       session_start();
    
       if($_SERVER["REQUEST_METHOD"] == "POST") {
          // username and password sent from form 
    
          $myusername = mysqli_real_escape_string($db,$_POST['uname']);
          $mypassword = mysqli_real_escape_string($db,$_POST['psw']); 
    
          $sql = "SELECT id FROM admin WHERE username = '$myusername' and password = '$mypassword'";
          $result = mysqli_query($db,$sql);
          $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
          $active = $row['active'];
    
          $count = mysqli_num_rows($result);
    
          // If result matched $myusername and $mypassword, table row must be 1 row
    
          if($count == 1) {
             session_register("username");
             $_SESSION['login_user'] = $myusername;
    
             header("location: home.php");
          }else {
             $error = "Your Login Name or Password is invalid";
          }
       }
    ?>
    
    Login or Signup to reply.
  2. Change your file name with the extension login.php instead of login.html and then send the variable value through ajax in the same page

    <?php
    
        // starting the session
        session_start();
    
        // checking if username and password entered of not
        if(isset($_POST['userid']) && isset($_POST['userpass'])){
            $server = 'server_name';
            $user = 'server_username';
            $pass = 'server_password';
            $db = 'database_name';
    
            $conn = new mysqli($server, $user, $pass, $db);
    
            $userid = base64_encode($_POST['userid']);
            $userpass = base64_encode($_POST['userpass']);
            $useremail = $_POST['userid'];
    
            $q = "SELECT * FROM table_name WHERE UserID='$userid' AND UserPWD='$userpass'";  // Fetching data from table
            $d = mysqli_query($conn, $q);
            if(mysqli_num_rows($d) == 1){
                while ($t = mysqli_fetch_assoc($d)) {
                    $_SESSION['login_status'] = 1; // Creating A login status variable in case of logout make this variable value 0
                    echo '1';
                }
            }else{
                echo '2';
            }
            mysqli_close($conn);
        }else{  //if username and password not entered
            ?>
                <input type="text" class="userid" placeholder="Username">
                <input type="password" class="userpass" placeholder="Password">
                <a href="#" class="login_btn ">login</a>
    
                <script>
    
                    $('.login_btn').on('click',function(){
                        var userid = $('.userid').val();
                        var userpass = $('.userpass').val();
                        if(userid != ""){
                            if(userpass != ""){
                                $.ajax({
                                    type:'post',
                                    data:{
                                        userid:userid,
                                        userpass:userpass,
                                    },
                                    url:'login.php',  // Sending variable to same page
    
                                    success:function(e){
                                        if(e == 1){ // Userid and password is correct
                                            window.location = 'index.php';
                                        }else if(e == 2){
                                            alert("Invalid Username and password");
                                        }else{
                                            console.log(e);
                                        }
                                    }
                                })
                            }else{
                                alert("Password Required");
                            }
                        }else{
                            alert("Username Required");
                        }
                    })
                </script>
            <?php        
        }
    
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search