0

For an exercise I am trying to create a login page. This is my first experience with php. I have been looking at many guides and looked at many questions from stackoverflow. So this is what I came up with.(I am using XAMPP for the Database)

Folder php: login.php, session.php, welcome.php, Connection.php

Folder pages: login.html

DB Name: leftoveryouth

Table Name: clients

Colums:id, FirstName, lastname, birthdate, street, streetnr, city, plz, username, email, Password

Relevant HTML Code:

<div class="placeholder">
                    <h1 class="logo"><a href="/index.html"class="alogo">Leftover Youth</a></h1>
                    <img class="logoo" src="../img/logoo.png" alt="firstimage">
                <form class="form" action = "" method = "post">
                    <hr class="verticalline">
                    <input id="email" type="text" value="Email@address.com" 
                            onblur="this.value'Email@address.com:this.value;"
                            onfocus="this.select()"
                            onclick="if (this.value=='Email@address.com'){this.value=''; this.type='Email@address.com'}"/>
                    <br>
                    <br>
                    <br>
                    <input id="password" value="Password"
                            onblur="this.value'Password:this.value;"
                            onfocus="this.select()"
                            onclick="if (this.value=='Password'){this.value=''; this.type='password'}"/>
                    <br>
                    <br>
                    <br>
                    <input id="button" type="submit" value="Log In">
                    <div class="links">
                        <a href="forgotpd.html" class="forpassword">Forgot Password?</a>
                        <a href="register.html" class="wannaregister">Create an Account?</a>
                    </div>
                </form>
        </div>

connection.php:

<?php
   define('DB_SERVER', 'localhost');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', '');
   define('DB_DATABASE', 'leftoveryouth');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

session.php:

<?php
   include('connection.php');
   session_start();

   $user_check = $_SESSION['login_user'];

   $ses_sql = mysqli_query($db,"select username from clients where username = '$user_check' ");

   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

   $login_session = $row['username'];

   if(!isset($_SESSION['login_user'])){
      header("location:login.php");
   }
?>

login.php:

<?php
   include("connection.php");
   include("/pages/login.html");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT id FROM clients 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("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>

welcome.php:

<?php
   include('session.php');
?>

    <html>
       <head>
          <title>Welcome </title>
       </head>

       <body>
          <h1>Welcome <?php echo $login_session; ?></h1> 
          <h2><a href = "logout.php">Sign Out</a></h2>
       </body>

    </html>

I always get this Error code:

 Cannot POST /pages/login.html

I don't care about Security at the moment. I will care about that after the basic login works:) So does somebody know what I have done wrong?

-------------EDIT:-------------------- Errors:Warning: include(/left_over_youth_website/php/connection.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 2

Warning: include(): Failed opening '/left_over_youth_website/php/connection.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 2

Warning: include(/left_over_youth_website/pages/login.html): failed to open stream: No such file or directory in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 3

Warning: include(): Failed opening '/left_over_youth_website/pages/login.html' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 3

Notice: Undefined variable: db in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 9

Notice: Undefined index: username in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 9

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 9

Notice: Undefined variable: db in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 10

Notice: Undefined index: password in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 10

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 10

Notice: Undefined variable: db in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 13

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 13

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 14

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\Left_over_youth_website\php\logins.php on line 17

Hazaki
  • 27
  • 5
  • 2
    You need to send your form to a PHP script, not to a static HTML page - your server is configured not to allow POST requests to the latter to begin with (because that usually doesn’t make much sense.) – CBroe Mar 16 '18 at 09:29
  • @CBroe is right. Alternatively you can edit your htaccess to treat HTML pages as PHP https://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – Saad Suri Mar 16 '18 at 09:32

1 Answers1

1
 <form class="form" action = "login.php" method = "post">

You need to "point" the form to your php script.

Also use .php file to write html inside of it. It is better for what you are trying to do (write php inside html)

Edit based on comment question:

So considering you want to use only php and html the way to redirect where you want through php is to use a header. To elaborate a bit more, when you click the form, you get send to a php script. In there you "control" the outcome so you can create an if to redirect the user where you want based on password response.

if($password=="myPass"){
header("Location: http://localhost/welcome_page.php");
}
else{
header("Location: http://localhost/wrong_pass.php");
}

Just a small example to get the idea of the header.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • done action ="/php/login.php" I still get the same error though – Hazaki Mar 16 '18 at 09:36
  • Change your file extension to .php from .html please and ensure you are giving the right path for the php script you want to point – pr1nc3 Mar 16 '18 at 09:37
  • alright done now i get object not found. it links me now to http://localhost:81/php/logins.php. It does that always. But when I enter the correct login it should link me to php/welcome.php not to logins.php – Hazaki Mar 16 '18 at 09:48
  • Why ist that a different question i just want a working login page;) But thanks for your answer still doesent work but ill work on it – Hazaki Mar 16 '18 at 10:19
  • Because i had to update my answer (which i did) in order to answer your question for redirect. If you have further questions let me know, i will update my answer again if i can solve your question. – pr1nc3 Mar 16 '18 at 10:25
  • sorry for my unclear comment. The thing is if you look at my login.php code you will realise that I already have a header that should redirect me to the welcome page if my Password and username is correct. – Hazaki Mar 16 '18 at 10:53
  • Try to set absolute path like the one i added in my answer. – pr1nc3 Mar 16 '18 at 11:07
  • thanks for all your help. I solved this problem more or less. xampps document root /htdocs was the Problem. Now i only have one full site of Errors -_-. I added the Errors in the question if you are interested. – Hazaki Mar 16 '18 at 11:16