0

so i have made a new login system that works but i need a little help with a few things. so i'm making a website where you can order stuff. there are 2 kind of buyers. one of them: regular buyers. they can't log in and buy their drinks for a regular price. the second one: store owners: they log in and buy their products for a discount BUT have to atleast buy 10 products before they can check out. then there is one other person that can log in and that is the admin. I have been struggeling with this this for days now but: how do I create this? how do I let my code know that if user == admin then ,for example, add a link in the navbar for the order list. or if user == store owner then put minimal of 10 items before checkout. and if user == not logged in then still allow user to buy products?

i just noticed that it might be important to tell that you can't register your own account. you have to send the admin a email and they will have to create a account for you with a random password. im still working on that one but because i don't know how to let the code know that certian users are different then i can't really work on that

this is my db with a test admin account: enter image description here

login script

<?php 
if (!isset($_SESSION)) { session_start(); }
include("config.php");

include "navbar.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <title>login</title>
</head>
<style>
#login{
    /* display: flex;
    justify-items: center; */
    padding: 10px;
}
input[type=text], input[type=password] {
  width: 50%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  box-sizing: border-box;
}
</style>
<body>
<div id="login">
    <!-- die form name shit moest er nog bij ik denk wegens de post ofzo in iedergeval werkt de knop nu -->
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    
     <h2>Login</h2>
     <br>
     <a>Dit is alleen voor bedrijven en admin</a>
     <br>
     <label>email</label><br>
    <input type="text" placeholder="email" name="email" class="email" required />
    <br>
    <label>wachtwoord</label><br>
    <input type="password" placeholder="wachtwoord" name="wachtwoord" class="wachtwoord1" required ><br>
    <input type="submit" value="Log In" class="log" name="log"/>
</form>
</div>
</body>
</html>
<?php
if ( !isset($_POST['email'], $_POST['wachtwoord']) ) {
    exit('Please fill both the username and password fields!');
}
if ($stmt = $conn->prepare('SELECT * FROM users WHERE email = ?')) {
    $email = $_POST['email'];
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $resultaat = $stmt->bind_result($id, $username, $email, $password, $rol);
        $stmt->fetch();
        if ($_POST['wachtwoord'] === $password) {
            session_regenerate_id();
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $_POST['email'];
            $_SESSION['id'] = $id;
            echo 'Welcome ' . $_SESSION['name'] . '!';
        } else {
            echo 'Incorrect email and/or password!';
        }
    } else {
        echo 'Incorrect email and/or password!';
    }


    $stmt->close();
}
?>

and this is my store page: I still have to do some work on that one such as creating a working way to order a few products and to put them in a shopping bag

<?php 
include "navbar.php";
?>
<?php
//if logged in 
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
    echo "Welkom Gast";
    exit;
}
?>
<html>

<head>
    <style>
    h1 {
        display: flex;
        justify-content: center;
    }
    table {
      display: flex;
      justify-content: center;
    }
    td {
      display: flex;
      justify-content: center;
    }
    #winkelmandje_knop{
      display: none;
    }
    #winkelmandje_knop + label{
      font-size: 3.5rem;
    }
    #winkelmandje{
      text-align: center;
      z-index: 3;
      position: fixed;
      bottom: 40%;
      right: 0;
      left: 0;
      height: 20%;
      visibility: hidden;
      width: 100%;
      background-color: #C2F98E;
      border-radius: 5%;
      opacity: 0.9;
      filter: alpha(opacity=9);
    }

    #rijen{
      visibility: hidden;
      padding: 0;
      margin: 0;
      width: 100%;
    }
    '
    form > .aantal{
      width: 30px;
      height: 20px;
      margin-left: auto;
      margin-right: auto;
      border: solid 1px gray;
    }
    </style>
</head>

<body>
    <div class="products">
        <div class="productContainer">
            <h1 id="banner">ALL ITEMS</h1>
            <h2 id="testing name remembering">
            <?php 
            if ($_SESSION['loggedin'] == false) {
              echo "welcome gast";
            }else{
            echo 'Welcome ' . $_SESSION['name'] . '!';
            }
            ?> 
            </h2>
            <?php
        include "config.php";
        $vraag = "SELECT Naam, prijs, voorraad FROM bier";
        $result = $conn->query($vraag);
        ?>
            <table>
                <?php
        $loop = 0;
        foreach ($result as $row) :
        ?>
                <tr>
                    <td>Naam: <?= $row["Naam"]; ?></td>
                    <td>Prijs: <?= $row["prijs"]; ?></td>
                    <td>voorraad: <?= $row["voorraad"]; ?></td>
                    <td>Aantal: <?php echo "<input class='aantal' type='text' style='width: 10%;' value='0' />";?>
                </tr>
                <?php 
                $loop++;
              endforeach; 
              echo "<input type='hidden' name='loop' id='loop' value='".$loop."'/>";
              ?>
            </table>

            <!--winkelmandje aan/uit -->
<input type="checkbox" name="winkelmandje_knop" id="winkelmandje_knop" onclick="javascript:schowWinkelmandje();" class="icon" value="&#128717;" />
<label for="winkelmandje_knop">&#128717; </label>
<br>
  <div class="icon_container">
          <input type="submit" class="icon" id="submit" name="submit" value="&rarr;" />
  </div>
<br>
</form>
</div>
<!-- winkelmandje -->
<div id="winkelmandje">
    <div id="rijen"></div>
    <!-- java word moeilijk want ik snao daar helemaal niks van -->
    <input type="checkbox" id="winkelmandje_knop" onclick="javascript:showWinkelmandje();" class="icon" value="X" />
    <label for="winkelmandje_knop" style="color: red;">&#8861;</label>
    </body>

</html>
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

Based on the fact that the admin knows who the owners are, I recommend you to have a file called owners.txt which will store IDs of all owners. Before every order you ask them for their ID (which was send in the mail from admin you mentioned). You scan the owners.txt and if it contains the ID. If yes then you just check whether their order is bigger than 10 pieces and give them discound.

  • Doesn't this mean that the admin has to manually check Them? – minoesje minoes Aug 05 '21 at 10:25
  • And I dont know if i was clear or i Just dont understand your anwser but the stores have to log in in order to get that discount. It's something that my product owner wants. – minoesje minoes Aug 05 '21 at 10:29
  • He would not check them manually. It would work like this: https://stackoverflow.com/questions/9059026/php-check-if-file-contains-a-string – Oliver Kurnava Aug 05 '21 at 10:38
  • The second comment you wrote. You understood correctly that they have to login (only write their ID) every time they buy something. Is it bad? – Oliver Kurnava Aug 05 '21 at 10:41
  • Well yes because i also have customers that can't log in – minoesje minoes Aug 05 '21 at 11:16
  • They always have to login/enter ID at least at the begining. Otherwise you cant find out if they really are owners. But once they logged in you can use LocalStorage or Cookies to remember them so they dont have to login again if they want to buy something. But remember that cookies and LocalStorage are LOCAL and if they order from different device they will have to login again. – Oliver Kurnava Aug 05 '21 at 12:57
  • Sorry if i ask a lot but what u are saying is that i should first focus on the customers that can log in and have the discount and later the average customers that Just wanna buy one drink without log in? Then i Will try that first – minoesje minoes Aug 05 '21 at 16:00
  • 1. Most shops work like this: A. You choose a product B. If you want a discount you enter a code. (ID in your option) If the ID is real (is in owners.txt) they buy it for lower price. If the entered ID is fake they don't get discount. If they do not want discount they do not select it => no need to ask for ID. Thats what I am suggesting. – Oliver Kurnava Aug 05 '21 at 16:46