S. N
S. N

Reputation: 3949

Loggin page doesnt work

I am making a lo-gin page, where you should use a user_name and a password in order to lo-gin. before logging in, you need to register an account with your name. This is my code for logging in to the page:

 <?php
      // if already logged in
      if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
          if(isset($_POST['logout'])) {
              session_destroy();
              echo 'Logged Out!';
              showloginform();
          } 
          else {
              print("<p>Dear $_SESSION[username]</p>");
              print "<p>only a logged in user can see this</p>";
          }
      }
      //not logged in:
      else {
          //have login request:
          if(isset($_POST['username']) && isset($_POST['password'])) {
              include 'opendb.php';
              $username = $_POST['username'];
              $password = $_POST['password'];
              $sql = "SELECT Name, password FROM Restaurant_table WHERE Name = '$username' AND password = '$password'";
              $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
              //found username/password combination:
              if (mysql_num_rows($result) == 1) {
                  $_SESSION['logged'] = true;
                  $_SESSION['username'] = $_POST['username'];
                  $_SESSION['password'] = $_POST['password'];
                  echo 'logged in';
                  //exit;
              }
              else {
                echo 'Sorry, wrong user_id or password.';
              }
          }
          //have no login request:
          else {
            showloginform();
          }
      }

      function showloginform() {
          echo "\r\n please enter your login information to proceed with our site <br/><br/>";
          echo '<form action="start.html" method="post">';
          echo '<div class="input-group">';
          echo '<input type="text" placeholder="User ID" name="username" class="input-transparent" id="email" />';
          echo '<input type="password" placeholder="password" name="password" class="input-transparent"/>';
          echo '</div>';
          echo '<button id="login-submit" type="submit" class="login-button">Manager Login</button>';
          echo '</form>'; 
      }

      function checkpass() {
          include 'opendb';
          $sql = "select * from Restaurant_table where Name='$_POST[username]' and password='$_POST[password]'";
          $result = mysql_query($sql,$conn) or die(mysql_error());
          return  mysql_num_rows($result);
      }
      ?>

but it seems like it doesnt work, First of all it doesnt give me any error when i log-in with use wrong info it just moves to the next page. It is suppose to give me an error or omething, I am not sure what is going wrong.

Upvotes: 1

Views: 86

Answers (2)

Amar Banerjee
Amar Banerjee

Reputation: 5012

Yes first you have to post the values in a php page not html page.

 echo '<form action="<?php echo $_SERVER[\'PHP_SELF\']?>" method="post">';

Second mistake is

if (mysql_num_rows($result) == 1) {

instead of this it should be.

if (mysql_num_rows($result)> 0) {

Upvotes: 3

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

You are using action as start.html page for your <form>. Because of that it is redirecting to start.html rather than start.php. Change this

echo '<form action="start.html" method="post">';

to

echo '<form action="start.php" method="post">';

OR quote by Prisoner

echo '<form action="" method="post">';

Upvotes: 4

Related Questions