Untitled

mail@pastecode.io avatar
unknown
php
a year ago
5.0 kB
2
Indexable
<html>
    <body>
    <form action="Seatwork5.php" method="post">
        <h2>Signup</h2>
        Name:
        <input type="text" name="lastname" placeholder="Last Name" required> 
        <input type="text" name="firstname" placeholder="First Name" required> 
        <input type="text" name="middlename" placeholder="Middle Name" required> <br>
        Email: <input type="text" name="email" placeholder="email@gmail.com" required> <br>
        Mobile No: <input type="text" name="mobile" placeholder="+63-915-123-4567" required> <br>
        Username: <input type="text" name="username" placeholder="Enter Username" required> <br>
        Password: <input type="password" name="password" placeholder="Enter Password" required> <br>
        Confirm Password: <input type="password" name="con_password" placeholder="Enter Password" required> <br>

        <input type="submit" name="signup" value="Sign up">
    </form>

    <?php if (isset($errors) && !empty($errors)) {
        echo "<h4>Error(s):</h4>";
        echo "<ul>";
        foreach ($errors as $error) {
            echo "<li>$error</li>";
        }
        echo "</ul>";
    } elseif (isset($signup_success)) {
        echo "<p>$signup_success</p>";
    }
    ?>

    <form action="Seatwork5.php" method="post">
        <h2>Login</h2>
        Username: <input type="text" name="login_username" placeholder="Enter Username"> <br>
        Password: <input type="password" name="login_password" placeholder="Enter Password"> <br>
        <input type="submit" name="login" value="Login">
    </form>

    <?php if (isset($login_error)) {
        echo "<p>$login_error</p>";
    } elseif (isset($welcome_message)) {
        echo "<p>$welcome_message</p>";
    }
    ?>
        

        <?php
        session_start();
        $errors = array();

        if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["signup"])) {
          $firstname = $_POST["firstname"];
          $middlename = $_POST["middlename"];
          $lastname = $_POST["lastname"];
          $mobile = $_POST["mobile"];
          $email = $_POST["email"];
          $username = $_POST["username"];
          $password = $_POST["password"];
          $confirm_password = $_POST["con_password"];
          $error_signup = "";

            // Validate Lastname, Firstname, M.I.
        $pattern_name = "/^[A-Za-z\s\-]+$/";
        if (!preg_match($pattern_name, $lastname) || !preg_match($pattern_name, $firstname) || !preg_match($pattern_name, $middlename)) {
            $errors[] = "Invalid name format. Please try again.";
        }

        // Validate Mobile Number
        $pattern_mobilenumber = "/\+63-\d{3}-\d{3}-\d{4}/";
        if (!preg_match($pattern_mobilenumber, $mobile)) {
            $errors[] = "Invalid Mobile Number Format. Please try again.";
        }

        // Validate Email
        $pattern_email = "/^\S+@\S+\.\S+$/";
        if (!preg_match($pattern_email, $email)) {
            $errors[] = "Invalid Email Format. Please try again.";
        }

        // Validate Username
        $pattern_username = "/^.{5,}$/";
        if (!preg_match($pattern_username, $username)) {
            $errors[] = "Please enter at least 5 characters for the username. Please try again.";
        }

        // Validate Password
        $pattern_password = "/^(?=.*[a-zA-Z0-9])(?=.*[\p{P}\p{S}]).{8,}$/";
        if (!preg_match($pattern_password, $password)) {
            $errors[] = "Please enter at least 8 characters with alphanumeric and special characters for the password. Please try again.";
        }

        // Validate confirm password
        if ($password !== $confirm_password) {
            $errors[] = "Password does not match. Please try again.";
        }

        if (empty($errors)) {
              $_SESSION["lastname"] = $lastname;
              $_SESSION["firstname"] = $firstname;
              $_SESSION["middlename"] = $middlename;
              $_SESSION["email"] = $email;
              $_SESSION["mobile"] = $mobile;
              $_SESSION["username"] = $username;
              $_SESSION["password"] = $password;
      
              $signup_success = "Registration successful!!!";
            }
        }

        // Login Form
        if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["login"])) {
          $username = ($_POST["login_username"]);
          $password = ($_POST["login_password"]);

          // Check if login credentials match
          if (isset($_SESSION["username"]) && isset($_SESSION["password"]) && $username === $_SESSION["username"] && $password === $_SESSION["password"]) {
              $welcome_message = "Welcome, Mr./Ms. " . $_SESSION["lastname"] . ".";
          } else {
              $login_error = "Invalid username or password. Please try again.";
          }
        }
        ?>


        <?php 
        if (isset($login_error)) {
          echo "<p>$login_error</p>";
        } elseif (isset($welcome_message)) {
           echo "<p>$welcome_message</p>";
          }
        ?>
    </body>
</html>