Search code examples
phphtmleasyphp

Simple Login Application Using PHP


I created a Login App. When I enter user name and password it always displays the PHP code in the page it doesn't run the code, it just displays it why?

Second thing is that I created database using EasyPHP but I don't know if my site has access to the database or not. I placed the site inside the EasyPHP local web file, here is my code:

html file:

<!DOCTYPE html> 
<html>
    <head>
        <title>Library</title>
        <meta charset="UTF-8">
        <style type="text/css">
            html {
                font-family: Verdana, Geneva, sans-serif ;
            }
            h1 {
                font-size: 24px ;
                text-align: center ;
            }
            #wrapper {
                top: 30%
                margin: 0 auto
            }
            #form {
                margin: auto ;
                width: 200px ;
                heigh: 100px ;
            }
        </style>
    </head>

    <body>
        <div id = "wrapper">
            <h1>Library log in</h1>
            <form id = "form" action="login.php" method="post" >
                UserName: <input type="text" name="username"> <br>
                Password: <input type="password" name="password"> <br>
                <input type="submit" value="Login" name="submit" >
            </form>
        </div>
    </body>
</html>

php file:

<?php 
    session_start();

    $username = $_POST['username'];
    $password = $_POST['password']; 

    if ($username&&$password){

            $connect = mysql_connect("localhost","root",""); or die ("Couldn't connect") ;
            mysql_select_db("phplogin") or die ("Couldn't find db") ;
    }
    else {
        echo "invalid username or password";

    }

?>

The database name is phplogin

I am opening the file from my hard drive not from a web host.

The error is when I run the site, then fill the form and press the login button it displays the PHP code. It should send me to an empty page or display an error message if the data was wrong.

I already changed to session_start(); same error.


Solution

  • Try this one.

    <?php 
        session_start();
    
         $connect = mysql_connect("localhost","root",""); or die ("Couldn't connect") ;
         mysql_select_db("phplogin") or die ("Couldn't find db") ;
    
        $username = $_POST['username'];
        $password = $_POST['password']; 
    
        if ($username&&$password){
    
            //create a file success.php
            header("location:success.php");
    
        }
        else {
            echo "invalid username or password";
    
        }
    
    ?>